pythonpyqt

How to make one window to block another without using .setModal(True)


If main window right-clicked a QInputDialog shows up. I want QInputDialog to block main window while it is open. How to achieve this?

enter image description here

from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])
class AppWindow(QtGui.QMainWindow):
    def __init__(self):
        super(AppWindow, self).__init__()
        mainWidget=QtGui.QWidget()
        self.setCentralWidget(mainWidget)
        mainLayout = QtGui.QVBoxLayout()
        mainWidget.setLayout(mainLayout)
        frame=QtGui.QFrame()
        frame.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        frame.connect(frame, QtCore.SIGNAL("customContextMenuRequested(QPoint)" ), self.up)
        mainLayout.addWidget(frame)

        self.modal=QtGui.QInputDialog()       

    def up(self, QPos):
        self.modal.move(QtGui.QCursor.pos())
        self.modal.show()
        self.modal.raise_()

window=AppWindow()
window.show()
sys.exit(app.exec_())

Solution

  • OK, This solution can be solve by use method QWidget.setWindowModality (self, Qt.WindowModality windowModality) . A modal window is one that blocks input to other windows. Note that windows that are children of a modal window are not blocked.

    Add this line in your initial method;

    self.modal.setWindowModality(QtCore.Qt.ApplicationModal)

    Completed code is;

    import sys
    from PyQt4 import QtCore, QtGui
    class AppWindow (QtGui.QMainWindow):
        def __init__ (self):
            super(AppWindow, self).__init__()
            mainWidget = QtGui.QWidget(self)
            self.setCentralWidget(mainWidget)
            mainLayout = QtGui.QVBoxLayout()
            mainWidget.setLayout(mainLayout)
            frame = QtGui.QFrame()
            frame.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
            frame.connect(frame, QtCore.SIGNAL("customContextMenuRequested(QPoint)" ), self.up)
            mainLayout.addWidget(frame)
            self.modal = QtGui.QInputDialog(self)
            self.modal.setWindowModality(QtCore.Qt.ApplicationModal)
    
        def up (self, QPos):
            self.modal.move(QtGui.QCursor.pos())
            self.modal.show()
            self.modal.raise_()
    
    app = QtGui.QApplication([])
    window = AppWindow()
    window.show()
    sys.exit(app.exec_())
    

    Reference method : http://pyqt.sourceforge.net/Docs/PyQt4/qwidget.html#setWindowModality

    Reference enum : http://pyqt.sourceforge.net/Docs/PyQt4/qt.html#WindowModality-enum


    Regards,