Issue
I want to open a child window in the MainWindow
of my app. I want to disallow the user to interact with the parent window, and only allow him to open that child window once. For example, the 'About' window in many applications.
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)
Here is how I call it in the 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()
...
...
Solution
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
is set to None
in setupUi
of class Ui_MainWindow
.
I think while using self.AboutDialog.setModal(True)
we may go without self.AboutDialog
as follows:
def openAbout(self, MainWindow):
AboutDialog = QtGui.QDialog(MainWindow)
aboutUi = Ui_AboutDialog()
aboutUi.setupUi(AboutDialog)
AboutDialog.setModal(True)
AboutDialog.show()
Solution
Though not explicitly stated, I assume you are asking how to make the QDialog
only appear once? If so, how about implementing a variable in your Ui_MainWindow
class which tracks whether the child window has already been opened once before. You could easily modify your openAbout
function to check this variable and only open the child window if it hasn't been opened before.
Answered By - estebro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.