Results 1 to 16 of 16

Thread: how to catch close event in this program? [pyqt]

  1. #1
    Join Date
    Dec 2009
    Posts
    20
    Thanks
    1
    Qt products
    Platforms
    Unix/X11

    Default how to catch close event in this program? [pyqt]

    Qt Code:
    1. from PyQt4 import QtCore, QtGui
    2. import sys
    3.  
    4. class Ui_TabWidget(object):
    5. def setupUi(self, TabWidget):
    6. TabWidget.setObjectName("TabWidget")
    7. TabWidget.resize(400, 300)
    8. self.tab = QtGui.QWidget()
    9. self.tab.setObjectName("tab")
    10. TabWidget.addTab(self.tab, "")
    11. self.tab1 = QtGui.QWidget()
    12. self.tab1.setObjectName("tab1")
    13. TabWidget.addTab(self.tab1, "")
    14.  
    15. self.retranslateUi(TabWidget)
    16. QtCore.QMetaObject.connectSlotsByName(TabWidget)
    17.  
    18. def retranslateUi(self, TabWidget):
    19. TabWidget.setWindowTitle(QtGui.QApplication.translate("TabWidget", "TabWidget", None, QtGui.QApplication.UnicodeUTF8))
    20. TabWidget.setTabText(TabWidget.indexOf(self.tab), QtGui.QApplication.translate("TabWidget", "Tab 1", None, QtGui.QApplication.UnicodeUTF8))
    21. TabWidget.setTabText(TabWidget.indexOf(self.tab1), QtGui.QApplication.translate("TabWidget", "Tab 2", None, QtGui.QApplication.UnicodeUTF8))
    22.  
    23. class Trayicon(QtGui.QSystemTrayIcon):
    24. def __init__(self,parent=None):
    25. QtGui.QSystemTrayIcon.__init__(self,parent)
    26. self.setIcon(QtGui.QIcon("icons/blockattack32.xpm"))
    27. self.show()
    28. self.menu=QtGui.QMenu()
    29. preference=self.menu.addAction("Preferences")
    30. exit=self.menu.addAction("Exit")
    31.  
    32. self.setContextMenu(self.menu)
    33.  
    34. self.TabWidget = QtGui.QTabWidget()
    35. ui = Ui_TabWidget()
    36. ui.setupUi(self.TabWidget)
    37.  
    38. self.connect(exit,QtCore.SIGNAL('triggered()'),self.menuExit)
    39.  
    40. self.connect(preference,QtCore.SIGNAL('triggered()'),self.showWidget)
    41.  
    42. #clos=QtGui.QAction(self.TabWidget)
    43. #self.connect(clos,QtCore.SIGNAL('triggered()'),self.TabWidget,QtCore.SLOT('close()'))
    44.  
    45. def menuExit(self):
    46. app.exit()
    47.  
    48. def showWidget(self):
    49. self.TabWidget.show()
    50.  
    51. #def closeEvent(self,event):
    52. #print('Hello')
    53.  
    54. app = QtGui.QApplication(sys.argv)
    55.  
    56. cd=Trayicon()
    57. cd.show()
    58. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 
    As you can see it's designed in QtDesigner.. I could not override the close event? Yet another ques. can you tell me the slot for minimize event?? Your help is much awaited.. Thanks..

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to catch close event in this program? [pyqt]

    reimplement QWidget::closeEvent. for minimization of a widget use QWidget::showMinimized.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. The following user says thank you to spirit for this useful post:

    pyqt123 (14th December 2009)

  4. #3
    Join Date
    Dec 2009
    Posts
    20
    Thanks
    1
    Qt products
    Platforms
    Unix/X11

    Default Re: how to catch close event in this program? [pyqt]

    Thanks spirit..
    • I've reimplemented the closeEvent.. But it's not connecting.. Might be some problem with signal and slot connection(Plz. go through the code once again). Can you tell me what is the problem with the program???
    • For reimplementing the the minimize event should i use showMinimized() like below.. Will it work??
      Qt Code:
      1. self.connect(clos,QtCore.SIGNAL('triggered()'),self.TabWidget,QtCore.SLOT('showMinimized()'))
      To copy to clipboard, switch view to plain text mode 
      I'm new to pyqt.Thanks.

  5. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to catch close event in this program? [pyqt]

    Quote Originally Posted by pyqt123 View Post
    Thanks spirit..
    • I've reimplemented the closeEvent.. But it's not connecting.. Might be some problem with signal and slot connection(Plz. go through the code once again). Can you tell me what is the problem with the program???
    closeEvent is not a slot. if you make as Qt Assistant says then reimplementation of closeEvent should work.

    Quote Originally Posted by pyqt123 View Post
    • For reimplementing the the minimize event should i use showMinimized() like below.. Will it work??
      Qt Code:
      1. self.connect(clos,QtCore.SIGNAL('triggered()'),self.TabWidget,QtCore.SLOT('showMinimized()'))
      To copy to clipboard, switch view to plain text mode 
      I'm new to pyqt.Thanks.
    you can't minimize child widget, you should minimize parent widget only, i.e.
    Qt Code:
    1. self.connect(clos,QtCore.SIGNAL('triggered()'),self,QtCore.SLOT('showMinimized()'))
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #5
    Join Date
    Dec 2009
    Posts
    20
    Thanks
    1
    Qt products
    Platforms
    Unix/X11

    Default Re: how to catch close event in this program? [pyqt]

    Thanks spirit for your help..
    • But the code below worked..
      Qt Code:
      1. import sys
      2. from PyQt4 import QtGui,QtCore
      3.  
      4. class min_close_evt(QtGui.QTabWidget):
      5. def __init__(self,parent=None):
      6. QtGui.QTabWidget.__init__(self,parent)
      7. self.resize(626, 511)
      8.  
      9. self.tab = QtGui.QWidget()
      10. self.addTab(self.tab,QtGui.QIcon("icons/apple-green.png"),"Tab")
      11.  
      12. exit=QtGui.QAction(self)
      13. exit.setShortcut('Ctrl+Q')
      14.  
      15. self.connect(exit,QtCore.SIGNAL('triggered()'),QtCore.SLOT('close()'))
      16.  
      17. def closeEvent(self,event):
      18. reply=QtGui.QMessageBox.question(self,'Message',"Are you sure to quit?",QtGui.QMessageBox.Yes,QtGui.QMessageBox.No)
      19. if reply==QtGui.QMessageBox.Yes:
      20. event.accept()
      21. else:
      22. event.ignore()
      23.  
      24. app=QtGui.QApplication(sys.argv)
      25. ob=min_close_evt()
      26. ob.show()
      27. app.exec_()
      To copy to clipboard, switch view to plain text mode 
      So the problem should be the inability to have the instance of QTabWidget as a receiver.. What do you think??
    • The other thing is here the parent is QSystemTrayIcon.. But i want to reimplement the minimize event for the QTabWidget!!!!! Thanks..

  7. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to catch close event in this program? [pyqt]

    you should call showMinimized for ob object.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #7
    Join Date
    Dec 2009
    Posts
    20
    Thanks
    1
    Qt products
    Platforms
    Unix/X11

    Default Re: how to catch close event in this program? [pyqt]

    Quote Originally Posted by spirit View Post
    you should call showMinimized for ob object.
    Sorry i can't understand.. Could you explain me in detail(with respect to my program code) by spending your valuable time??? Sorry for being dumb.

  9. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to catch close event in this program? [pyqt]

    ok, as I understand min_close_evt derives QTabWidget.
    according to this
    Qt Code:
    1. app=QtGui.QApplication(sys.argv)
    2. ob=min_close_evt()
    3. ob.show()
    4. app.exec_()
    To copy to clipboard, switch view to plain text mode 

    ob it is an object of min_close_evt, i.e. this widget is on top of Qt's object hierarchy, that means if you need to show your widget minimized you should do like this:

    Qt Code:
    1. app=QtGui.QApplication(sys.argv)
    2. ob=min_close_evt()
    3. ob.showMinimized()
    4. app.exec_()
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  10. #9
    Join Date
    Dec 2009
    Posts
    20
    Thanks
    1
    Qt products
    Platforms
    Unix/X11

    Default Re: how to catch close event in this program? [pyqt]

    Thanks again spirit..
    • But here i want to reimplement the minimize event for which i need to connect my own method as a substitute to the predefined method like this..
      Qt Code:
      1. exit=QtGui.QAction(self)
      2. self.connect(exit,QtCore.SIGNAL('triggered()'),QtCore.SLOT('close()'))
      To copy to clipboard, switch view to plain text mode 
      Reimplemented method:
      Qt Code:
      1. def closeEvent(self,event):
      2. reply=QtGui.QMessageBox.question(self,'Message',"Are you sure to quit?",QtGui.QMessageBox.Yes,QtGui.QMessageBox.No)
      3. if reply==QtGui.QMessageBox.Yes:
      4. event.accept()
      5. else:
      6. event.ignore()
      To copy to clipboard, switch view to plain text mode 
    • In the same manner as above, is there any way of doing the same job for minimize event.. In showMinimized method we are calling this purposely to minimize the window which can be attained in the way as you described (as well as by self.showMinimized() in it's __init__ method..
    • My ultimate aim is to hide the QTabWidget() if minimize or close event is encountered.. The only way to quit from the application is to select exit from the tray icon's context menu..

    Thanks for all your help..

  11. #10
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to catch close event in this program? [pyqt]

    there is no such event for minimization.
    can you describe in details what you need?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  12. #11
    Join Date
    Dec 2009
    Posts
    20
    Thanks
    1
    Qt products
    Platforms
    Unix/X11

    Default Re: how to catch close event in this program? [pyqt]

    Thanks spirit for your patience..
    • I want to start my application from the system tray icon.. When the user clicks preferences from the system tray context menu the tabwidget will appear.. If the user closes or minimizes the tabwidget the application will continue to run in the background(the widget will be hidden and only the tray icon will be visible).. When the user clicks the exit from the context menu only, the application will quit, otherwise it'll keep on running..(Can i say like bit torrent???)
    • I've used qtdesigner to design the Tabs in the widget(and used pyuic4 for converting it to python).. That's why i've given a minimal example of a simple tabwidget designed in qt designer.. If i could make this example to run i can easily redo my original code.
    • I couldn't find much online stuffs in this regard especially for pyqt.. Can you suggest me the best possible solution??? Thanks..

  13. #12
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to catch close event in this program? [pyqt]

    Quote Originally Posted by pyqt123 View Post
    Thanks spirit for your patience..
    • I want to start my application from the system tray icon.. When the user clicks preferences from the system tray context menu the tabwidget will appear.. If the user closes or minimizes the tabwidget the application will continue to run in the background(the widget will be hidden and only the tray icon will be visible).. When the user clicks the exit from the context menu only, the application will quit, otherwise it'll keep on running..(Can i say like bit torrent???)
    ok, in this case you should reimplement (and you have already done this) closeEvent and just hide a widget using QWidget::hide.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  14. #13
    Join Date
    Dec 2009
    Posts
    20
    Thanks
    1
    Qt products
    Platforms
    Unix/X11

    Default Re: how to catch close event in this program? [pyqt]

    Quote Originally Posted by spirit View Post
    ok, in this case you should reimplement (and you have already done this) closeEvent and just hide a widget using QWidget::hide.
    But can you tell me how the same be implemented for minimize event.. I've seen a function called isMinimized().. Could that be used in any way?? Thanks.

  15. #14
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to catch close event in this program? [pyqt]

    reimplement QWidget::event and catch QWindowStateChangeEvent then compare QWindowStateChangeEvent::oldState with Qt::WindowMinimized.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  16. #15
    Join Date
    Dec 2009
    Posts
    20
    Thanks
    1
    Qt products
    Platforms
    Unix/X11

    Default Re: how to catch close event in this program? [pyqt]

    Quote Originally Posted by spirit View Post
    reimplement QWidget::event and catch QWindowStateChangeEvent then compare QWindowStateChangeEvent::oldState with Qt::WindowMinimized.
    Yes that is one of the best solutions.. Thank you so much.. I'll try to implement this and get back to you..

  17. #16
    Join Date
    Dec 2009
    Posts
    20
    Thanks
    1
    Qt products
    Platforms
    Unix/X11

    Default Re: how to catch close event in this program? [pyqt]

    def changeEvent(self,event):
    if(event.type()==QtCore.QEvent.WindowStateChange):
    if self.isMinimized():
    <<<Enter your code here>>>>
    Atlast found the solution. Thanks spirit.

Similar Threads

  1. Replies: 1
    Last Post: 26th November 2009, 22:57
  2. Replies: 17
    Last Post: 2nd June 2009, 10:18
  3. Catch a row selection event in QTableView
    By Windsoarer in forum Qt Programming
    Replies: 1
    Last Post: 2nd March 2009, 22:44
  4. Replies: 4
    Last Post: 19th February 2009, 11:10
  5. QT MySQL
    By sabeeshcs in forum Newbie
    Replies: 6
    Last Post: 12th January 2007, 04:19

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.