Question

If my structure looks like this...

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()

QUESTIONS:

  1. How can I get application loading complete event from PyQt in AppWindow class, so that I know its contructor has finished running?
  2. How can I get application resizing event in AppWindow class?
    I can get this in the main function and tweak it, but if the AppWindow class is capable of listening and handling it: What is best way of doing it? Should this be ideally done as above?
Était-ce utile?

La solution

ANSWERS:

  1. Just start a one-shot timer with your QApplication that calls the right method on AppWindows.
  2. Just put the code of onResize in AppWindows.resizeEvent.

Example:

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()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top