Results 1 to 9 of 9

Thread: Implementing event for Qt Designer MainWindow

  1. #1
    Join Date
    Jun 2009
    Posts
    21
    Qt products
    Platforms
    Unix/X11 Windows

    Default Implementing event for Qt Designer MainWindow

    Hi!

    I've got MainWindow form build in Qt Designer. I want to implement some events for this window. I tried to reimplement closeEvent method using code like this:

    Qt Code:
    1. from PySide import QtCore
    2. from PySide import QtGui
    3. from PySide import QtUiTools
    4.  
    5. import sys
    6.  
    7. class TestWin(QtGui.QMainWindow):
    8.  
    9. def __init__(self, parent=None):
    10.  
    11. super(TestWin, self).__init__(parent)
    12.  
    13. loader = QtUiTools.QUiLoader()
    14. uiFile = QtCore.QFile('testForm.ui')
    15. uiFile.open(QtCore.QFile.ReadOnly)
    16.  
    17. self.mainWindow = loader.load(uiFile)
    18. self.mainWindow.show()
    19.  
    20. self.mainWindow.closeEvent = self.myCloseEvent
    21.  
    22. def myCloseEvent(self, event):
    23.  
    24. print 'Close'
    25. event.accept()
    26.  
    27. if __name__ == '__main__':
    28.  
    29. app = QtGui.QApplication(sys.argv)
    30. testApp = TestWin()
    31. app.exec_()
    To copy to clipboard, switch view to plain text mode 

    but this code doesn't work. How should I reimplement methods for Qt Designer forms ?

    Thanks,
    Marcin

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Implementing event for Qt Designer MainWindow

    Is there any particular reason why you are using QtUiTools? This is a very nonstandard approach. Apart that your code looks ok (however a bit unorthodox). What do you mean by that it doesn't work?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2009
    Posts
    21
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Implementing event for Qt Designer MainWindow

    I use similar code to load ui as those shown in pySide docs. Should I do this in different way ?
    For me code looks correct, but when I close the window myCloseEvent isn't invoked.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Implementing event for Qt Designer MainWindow

    You should use pyside-uic to generate python class from the UI file and then use that class from your code. For example like here:
    https://github.com/PySide/Examples/t...calculatorform
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jun 2009
    Posts
    21
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Implementing event for Qt Designer MainWindow

    I know that pyside-uic code will be better for this, but I was curious how to do the same using dynamically loaded ui.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Implementing event for Qt Designer MainWindow

    You have a strange construction. Your class inherits QMainWindow but you show the object loaded with QtUiTools. Are you sure this is what you want?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jun 2009
    Posts
    21
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Implementing event for Qt Designer MainWindow

    That's my mistake. I shouldn't inherit QMainWindow in this case. But I don't understand why closeEvent isn't invoked. I overwrite self.mainWindow closeEvent method,but nothing happens when I close main window.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Implementing event for Qt Designer MainWindow

    Maybe you shouldn't rely on print()? What if you invoke event.ignore()? It should prevent the window from being closed. It could also be that you are not overriding the method properly.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jun 2009
    Posts
    21
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Implementing event for Qt Designer MainWindow

    If I invoke event.ignore() I can still close window. It looks like closeEvent was not replaced with my custom close event. I think there's something wrong with my way of overriding the event method.

Similar Threads

  1. Replies: 0
    Last Post: 6th November 2011, 09:22
  2. Replies: 1
    Last Post: 12th April 2011, 09:53
  3. Replies: 5
    Last Post: 3rd April 2010, 04:07
  4. Creating a form.ui by a Designer + implementing to .cpp file
    By thewooferbbk in forum Qt Programming
    Replies: 1
    Last Post: 25th March 2009, 08:25
  5. Notifying Mainwindow of an event..
    By MrGarbage in forum Qt Programming
    Replies: 1
    Last Post: 9th November 2007, 21:29

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.