[PyQt] How to get the event of child widget

Jothy jothybasu at gmail.com
Thu Apr 22 10:19:05 BST 2010


Thanks,

Here is another example which describes how to ignore the close event
of the subwindow, but the problem is it doesn't close even if the main
window closes.How to tell to the MainWindow close event to close all
the subWindows,is that by child.close().


import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class SubWindow(QWidget):
    def __init__(self, parent = None):
        super(SubWindow, self).__init__(parent)
        label = QLabel("Sub Window",  self)

    def closeEvent(self, event):
        event.ignore()

class MainWindow(QWidget):
    def __init__(self, parent = None):
        super(MainWindow, self).__init__(parent)
        openButton = QPushButton("Open Sub Window",  self)
        self.connect(openButton, SIGNAL("clicked()"), self.openSub)


    def openSub(self):
        self.sub = SubWindow()
        self.sub.show()


    def closeEvent(self,event):
        widgetList = QApplication.topLevelWidgets()
        numWindows = len(widgetList)
        if numWindows > 1:
            event.accept()
        else:
            event.ignore()

app = QApplication(sys.argv)
mainWin =MainWindow()
mainWin.show()
sys.exit(app.exec_())




Jothy


On Thu, Apr 22, 2010 at 1:40 AM, David Boddie <david at boddie.org.uk> wrote:
> On Wed Apr 21 11:43:24 BST 2010, Jothybasu K Selvaraj wrote:
>
>> I am trying to ignore the close event of a subwindow, but I don't know how
>> to do that.
>
> [...]
>
>> class App(QMainWindow,MW.Ui_Form):
>>     def __init__(self,parent=None):
>>         super(App,self).__init__(parent)
>>         self.setupUi(self)
>>         flags=QtCore.Qt.Popup
>>         flags |= QtCore.Qt.WindowTitleHint
>>         self.subwindow.setWindowFlags(flags)
>>         self.mdiArea.addSubWindow(self.subwindow)
>>         self.subwindow.show()
>>         self.connect(self, Qt.SIGNAL('triggered()'),self.closeEvent)
>>
>>     def closeEvent(subwindow,event):
>>         subwindow.event.ignore()


         Jothy
>
> This won't work because the closeEvent() handler is only called for the
> main window itself - calling the first parameter, "subwindow", just means
> that you access the QMainWindow instance using that name.
>
> I would have thought that it would be possible to set some option to manage
> subwindow behaviour, but maybe it isn't. Anyway, I see you have already
> investigated the use of window flags to prevent the user from closing the
> subwindows using their close buttons.
>
> What you may want to do is to install an event filter on each subwindow as
> you create them:
>
>  http://doc.qt.nokia.com/4.7-snapshot/eventsandfilters.html
>
> You could write a simple eventFilter() method in your QMainWindow subclass
> that handles events for all the subwindows, filtering out the Close event
> type so that subwindows can never be closed.
>
> David
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>


More information about the PyQt mailing list