PyQt应用程序加载完成事件

时间:2014-02-20 03:11:26

标签: events pyqt loading

如果我的结构看起来像这样......

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import QDialog,QImage,QPixmap
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from appView import Ui_View 
# this is designer .ui file converted to .py via pyuic4 cmd

class AppWindow(QDialog, Ui_View):
    def __init__(self):
        QDialog.__init__(self)
        # Set up the user interface from Designer.
        self.setupUi(self)
        self.setupEvents()
    def setupEvents():
        print ("setting up events")

def onResize(event):
        print event
def main():
    app = QtGui.QApplication(sys.argv)
    myapp = AppWindow()
    myapp.resizeEvent = onResize
    myapp.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

问题:

  1. 如何从PyQt获取应用程序加载完成事件 AppWindow类,以便我知道它的构造函数已经运行完了?
  2. 如何在AppWindow课程中获取应用程序调整大小事件?我能得到 这在main函数中并调整它,但是如果AppWindow类能够 倾听和处理它:最好的方法是什么?应该 理想情况下如上所述完成了吗?

1 个答案:

答案 0 :(得分:3)

解答:

  1. 只需使用QApplication启动一次性定时器即可在AppWindows上调用正确的方法。
  2. 只需将onResize的代码放入AppWindows.resizeEvent
  3. 示例:

    from PyQt4 import QtCore, QtGui
    from PyQt4.QtGui import QDialog,QImage,QPixmap
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    import sys
    
    class AppWindow(QDialog):
    
        def __init__(self):
            QDialog.__init__(self)
            # Set up the user interface from Designer.
            #self.setupUi(self)
            self.setupEvents()
    
        def setupEvents(self):
            print ("setting up events")
    
        def resizeEvent(self,event):
            print event
    
        def onQApplicationStarted(self):
            print 'started'
    
    def main():
        app = QtGui.QApplication(sys.argv)
        myapp = AppWindow()
        myapp.show()
        t = QtCore.QTimer()
        t.singleShot(0,myapp.onQApplicationStarted)
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
        main()