开发者

how to catch close,minimize and other events in this program(pyqt)?

Hai friends,

The problem here is i want to catch the minimize and close event of QTabWidget(by default this widget do not have any slot named close or minimize) to hide the widget.. so the only way the user can quit the application is by clicking exit from the 开发者_高级运维tray icon menu..

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.translate("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()'),self.menuExit)

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


    def menuExit(self):
        app.exit()

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

app = QtGui.QApplication(sys.argv)
cd=Trayicon()
cd.show()
sys.exit(app.exec_())

Your help would be much appreciated!!!!! If possible can you show me any tutorial that teaches me secondary level programming in Qt like this..


I am not sure what you want to achieve.

Do you want to hide the tab widget using some public method of that class? If so you can simply call close() method.

If your requirement is such that you do NOT want to close the tab widget when the application is running then the following is applicable ---

You wrote: "The problem here is i want to catch the minimize and close event of QTabWidget(by default this widget do not have any slot named close or minimize) to hide the widget .. "

QTabWidget inherits QWidget and QWidget implements 'close()' .

So, in a class that inherits QTabWidget, you can reimplement the method "closeEvent()" to capture the close event

In your code you can try this:

class MyTabWidget(QtGui.QtabWidget):
    def closeEvent(self):
        # Let the Exit button handle tab closing
        print "close event captured. Do nothing."

        # Alternatively, if you want to EXIT the application when the close event  
        #occurs,  you can implement that code  here!


# In the TrayIcon class

self.TabWidget = MyTabWidget() 

The above code will make sure that widget will be closed only when the exit button in system tray is clicked (and 'X' button of the tab widget won't close/hide that widget. )


You can implement your own event filter.

class custom(QWidget):
    def __init__(self):
        super(custom, self).__init__()
        self.installEventFilter(self)

    def eventFilter(self, qobject, qevent):
        qtype = qevent.type()
        if qtype == QEvent.Close or qtype == QEvent.WindowStateChange:
            .. hide logic
            return True
        # parents event handler for all other events
        return super(custom,self).eventFilter(qobject, qevent)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜