PySide子窗口

时间:2014-05-19 23:01:57

标签: python qt5 pyside

我想在我的应用的MainWindow中打开一个子窗口。我想禁止用户与父窗口交互并允许他只打开一次子窗口...例如许多应用程序中的about窗口

class Ui_AboutDialog(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(500, 300)
        MainWindow.setMinimumSize(QtCore.QSize(500, 300))
        MainWindow.setMaximumSize(QtCore.QSize(500, 300))
        MainWindow.setLocale(QtCore.QLocale(QtCore.QLocale.English, QtCore.QLocale.UnitedStates))
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setLocale(QtCore.QLocale(QtCore.QLocale.English, QtCore.QLocale.UnitedStates))
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(10, 10, 481, 181))
        self.label.setObjectName("label")
        #MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)

以及我在MainWindow中如何调用它

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        ...
        ...
        self.actionAbout.triggered.connect(lambda: self.openAbout(MainWindow))

    def openAbout(self, MainWindow):
        aboutDialog = QtGui.QDialog(MainWindow)
        aboutUi = Ui_AboutDialog()
        aboutUi.setupUi(aboutDialog)
        aboutDialog.show()

       ...
       ...

解决方案:

def openAbout(self, MainWindow):
    if self.AboutDialog is None:
        self.AboutDialog = QtGui.QDialog(MainWindow)
        aboutUi = Ui_AboutDialog()
        aboutUi.setupUi(self.AboutDialog)
    self.AboutDialog.setModal(True)
    self.AboutDialog.show()

self.AboutDialog设置为None setupUi中的class Ui_MainWindow

我认为在使用self.AboutDialog.setModal(True)时,我们可能没有self.AboutDialog,如下所示

def openAbout(self, MainWindow):
    AboutDialog = QtGui.QDialog(MainWindow)
    aboutUi = Ui_AboutDialog()
    aboutUi.setupUi(AboutDialog)
    AboutDialog.setModal(True)
    AboutDialog.show()

1 个答案:

答案 0 :(得分:0)

虽然没有明确说明,但我认为你问的是如何让QDialog只出现一次?如果是这样,如何在Ui_MainWindow类中实现一个跟踪子窗口是否已经打开过一次的变量。您可以轻松修改openAbout函数以检查此变量,只有在以前没有打开子窗口时才打开它。