PySide window with designer ui doesn't generate closeEvent

PySide window with designer ui doesn't generate closeEvent

Anonymous
Not applicable
2,060 Views
2 Replies
Message 1 of 3

PySide window with designer ui doesn't generate closeEvent

Anonymous
Not applicable

I can't create a ui from a designer file and then have closeEvent  called when I close the ui.  I think I need to load the ui file differently, but I can't figure out how to do that in PySide.   I've tried many variations on this, and no matter what I do, the closeEvent doesn't seem to get sent when I close the window (clicking on the x) when using the ui file.  What am I missing?

 

Here's my ui file, (test.ui)

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
 </widget>
</ui>

 

 

And here's my code:

class testClass(QtWidgets.QDialog):
    def __init__(self, useUiFile):
        super(TestClass, self).__init__()
        if useUiFile:
            self.ui = QtUiTools.QUiLoader().load("test.ui")
            self.ui.show()
        else:
            self.show()

    def closeEvent(self, event):
        print("closeEvent!!!!!")

 

 

If I don't use the UI file, my closeEvent gets called when I close the window:

 

ui = testClass(0) # calls closeEvent when closed

 

 

If I use the UI file, a closeEvent doesn't seem to be generated:

 

ui = testClass(1) # doesn't call closeEvent when closed

 

 

0 Likes
Accepted solutions (1)
2,061 Views
2 Replies
Replies (2)
Message 2 of 3

cheng_xi_li
Autodesk Support
Autodesk Support
Accepted solution

Hi,

 

Because you've created a widget with QUiLoader and its closeEvent wasn't overriden.

 

To make the closeEvent in your testclass work, you'll need to add that widget created from QUiLoader to testclass as a child.

 

e.g.

import PySide.QtGui as QtWidgets
import PySide.QtUiTools as QtUiTools
import PySide.QtCore as QtCore

class testClass(QtWidgets.QDialog):
    def __init__(self, useUiFile, parent=None):
        super(testClass, self).__init__(parent)
        if useUiFile:           
            self.ui = QtUiTools.QUiLoader().load("e:/test.ui")            
            mylayout = QtWidgets.QVBoxLayout()
            mylayout.addWidget(self.ui)
            self.setLayout(mylayout)    
            #It will be empty because there weren't any widgets in the sample ui file.
            self.show()
        else:
            self.show()

    def closeEvent(self, event):
        print("closeEvent!!!!!")
        
ui = testClass(1)

Yours,

Li

0 Likes
Message 3 of 3

Anonymous
Not applicable

Thanks, that worked perfectly.  And now I know...

0 Likes