1
没有那么多经验PyQt的所有..调整大小QDialog的单选按钮时,被经过的PyQt
我设计了Qt设计的UI有2个单选按钮。
取决于所选择的RadioButton,一些小部件是可见的而其他小部件不可见。我没有试图做的是自动调整对话框的布局,具体取决于UI中可见的部件数量。
这个问题与this one非常相似。这里的代码,可能有助于了解的问题(我知道这只是一块的代码,但整个对话是很长)的提取物:
class MyDialog(QDialog, FORM_CLASS):
..........
# connect the radioButton with a function that reloads the UI
self.radioSingle.toggled.connect(self.refreshWidgets)
.....
# dictionary with all the widgets and the values
self.widgetType = {
self.cmbModelName: ['all'],
self.cmbGridLayer: ['all'],
self.cmbRiverLayer: ['all'],
self.lineNewLayerEdit: ['all'],
self.lineLayerNumber: ['single']
}
# function that refresh the UI with the correct widgets depending on the radiobutton selected
def refreshWidgets(self, idx):
'''
refresh UI widgets depending on the radiobutton chosen
'''
tp = self.radioSingle.isChecked()
for k, v in self.widgetType.items():
if tp:
if 'all' or 'single' in v:
active = True
k.setEnabled(active)
k.setVisible(active)
else:
active = 'all' in v
k.setEnabled(active)
k.setVisible(active)
# attempt to resize the Dialog
self.setSizeConstraint()
肯定的代码可以改善(不如此熟练的代码编写)...
另请参阅我正在使用paintEvent来保持游戏非常好,因为paintEvent在主线程中,它在不断执行。更好的方法是拥有自己的定制单选按钮并覆盖它的“改变”方法。如果它改变了,你会调整你的对话框。像这样的^^ – yurisnm
嗨..谢谢你的回答,非常完整..但是,如果我理解正确,你建议设置对话框大小“手动”吧?我正在寻找一种知道布局的新维度的方法,当一些小部件不再可见时。 – matteo
是的,你是正确的,只是让我的代码,并让它运行。取出setFixedSize。如果你手动调整它的大小,当你标记大的收音机时它仍然会增长,而小的收音机仍然会减少。我做的只是众多例子之一。 “) – yurisnm