PDA

View Full Version : how to catch close event in this program? [pyqt]



pyqt123
14th December 2009, 07:44
from PyQt4 import QtCore, QtGui
import sys

class Ui_TabWidget(object):
def setupUi(self, TabWidget):
TabWidget.setObjectName("TabWidget")
TabWidget.resize(400, 300)
self.tab = QtGui.QWidget()
self.tab.setObjectName("tab")
TabWidget.addTab(self.tab, "")
self.tab1 = QtGui.QWidget()
self.tab1.setObjectName("tab1")
TabWidget.addTab(self.tab1, "")

self.retranslateUi(TabWidget)
QtCore.QMetaObject.connectSlotsByName(TabWidget)

def retranslateUi(self, TabWidget):
TabWidget.setWindowTitle(QtGui.QApplication.transl ate("TabWidget", "TabWidget", None, QtGui.QApplication.UnicodeUTF8))
TabWidget.setTabText(TabWidget.indexOf(self.tab), QtGui.QApplication.translate("TabWidget", "Tab 1", None, QtGui.QApplication.UnicodeUTF8))
TabWidget.setTabText(TabWidget.indexOf(self.tab1), QtGui.QApplication.translate("TabWidget", "Tab 2", None, QtGui.QApplication.UnicodeUTF8))

class Trayicon(QtGui.QSystemTrayIcon):
def __init__(self,parent=None):
QtGui.QSystemTrayIcon.__init__(self,parent)
self.setIcon(QtGui.QIcon("icons/blockattack32.xpm"))
self.show()
self.menu=QtGui.QMenu()
preference=self.menu.addAction("Preferences")
exit=self.menu.addAction("Exit")

self.setContextMenu(self.menu)

self.TabWidget = QtGui.QTabWidget()
ui = Ui_TabWidget()
ui.setupUi(self.TabWidget)

self.connect(exit,QtCore.SIGNAL('triggered()'),sel f.menuExit)

self.connect(preference,QtCore.SIGNAL('triggered() '),self.showWidget)

#clos=QtGui.QAction(self.TabWidget)
#self.connect(clos,QtCore.SIGNAL('triggered()'),se lf.TabWidget,QtCore.SLOT('close()'))

def menuExit(self):
app.exit()

def showWidget(self):
self.TabWidget.show()

#def closeEvent(self,event):
#print('Hello')

app = QtGui.QApplication(sys.argv)

cd=Trayicon()
cd.show()
sys.exit(app.exec_())
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..

spirit
14th December 2009, 07:46
reimplement QWidget::closeEvent. for minimization of a widget use QWidget::showMinimized.

pyqt123
14th December 2009, 08:00
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??

self.connect(clos,QtCore.SIGNAL('triggered()'),sel f.TabWidget,QtCore.SLOT('showMinimized()')) I'm new to pyqt.Thanks.

spirit
14th December 2009, 08:07
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.




For reimplementing the the minimize event should i use showMinimized() like below.. Will it work??

self.connect(clos,QtCore.SIGNAL('triggered()'),sel f.TabWidget,QtCore.SLOT('showMinimized()')) I'm new to pyqt.Thanks.

you can't minimize child widget, you should minimize parent widget only, i.e.

self.connect(clos,QtCore.SIGNAL('triggered()'),sel f,QtCore.SLOT('showMinimized()'))

pyqt123
14th December 2009, 08:32
Thanks spirit for your help..

But the code below worked..

import sys
from PyQt4 import QtGui,QtCore

class min_close_evt(QtGui.QTabWidget):
def __init__(self,parent=None):
QtGui.QTabWidget.__init__(self,parent)
self.resize(626, 511)

self.tab = QtGui.QWidget()
self.addTab(self.tab,QtGui.QIcon("icons/apple-green.png"),"Tab")

exit=QtGui.QAction(self)
exit.setShortcut('Ctrl+Q')

self.connect(exit,QtCore.SIGNAL('triggered()'),QtC ore.SLOT('close()'))

def closeEvent(self,event):
reply=QtGui.QMessageBox.question(self,'Message',"Are you sure to quit?",QtGui.QMessageBox.Yes,QtGui.QMessageBox.No)
if reply==QtGui.QMessageBox.Yes:
event.accept()
else:
event.ignore()

app=QtGui.QApplication(sys.argv)
ob=min_close_evt()
ob.show()
app.exec_()
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..

spirit
14th December 2009, 08:38
you should call showMinimized for ob object.

pyqt123
14th December 2009, 08:44
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:confused:.

spirit
14th December 2009, 08:53
ok, as I understand min_close_evt derives QTabWidget.
according to this


app=QtGui.QApplication(sys.argv)
ob=min_close_evt()
ob.show()
app.exec_()


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:



app=QtGui.QApplication(sys.argv)
ob=min_close_evt()
ob.showMinimized()
app.exec_()

pyqt123
14th December 2009, 09:19
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..

exit=QtGui.QAction(self)
self.connect(exit,QtCore.SIGNAL('triggered()'),QtC ore.SLOT('close()'))
Reimplemented method:

def closeEvent(self,event):
reply=QtGui.QMessageBox.question(self,'Message',"Are you sure to quit?",QtGui.QMessageBox.Yes,QtGui.QMessageBox.No)
if reply==QtGui.QMessageBox.Yes:
event.accept()
else:
event.ignore()
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..

spirit
14th December 2009, 09:24
there is no such event for minimization.
can you describe in details what you need?

pyqt123
14th December 2009, 09:54
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..

spirit
14th December 2009, 10:00
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.

pyqt123
14th December 2009, 10:24
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.

spirit
14th December 2009, 10:30
reimplement QWidget::event and catch QWindowStateChangeEvent then compare QWindowStateChangeEvent::oldState with Qt::WindowMinimized.

pyqt123
14th December 2009, 10:55
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..

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