如果我为QMainWindow使用QUiLoader,resizeEvent不起作用

时间:2013-12-01 13:47:49

标签: pyqt pyside

我从ui文件加载主窗口: main_1.py

from PySide import QtCore, QtGui, QtUiTools
import sys

class ControlMainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        loader = QtUiTools.QUiLoader()
        file = QtCore.QFile("lat.ui")
        file.open(QtCore.QFile.ReadOnly)
        self.ui = loader.load(file)
        file.close

    def show(self):
        self.ui.show()

    def resizeEvent(self, event):
        print event

app = QtGui.QApplication(sys.argv)
MW = ControlMainWindow()
MW.show()
sys.exit(app.exec_())

lat.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>708</width>
    <height>488</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLabel" name="label">
    <property name="geometry">
     <rect>
      <x>310</x>
      <y>170</y>
      <width>56</width>
      <height>13</height>
     </rect>
    </property>
    <property name="text">
     <string>TextLabel</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>708</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

在这种情况下,resizeEvent不会获得任何事件。但是,如果我在pyside-uic编译后加载我的设计ui它运行良好 main_2.py:

from PySide import QtCore, QtGui
import sys
import ui_lat

class ControlMainWindow(QtGui.QMainWindow, ui_lat.Ui_MainWindow):
    def __init__(self, parent=None):
        super(ControlMainWindow, self).__init__(parent)
        self.setupUi(self)

    def resizeEvent(self, event):
        print event

app = QtGui.QApplication(sys.argv)
MW = ControlMainWindow()
MW.show()
sys.exit(app.exec_())

ui_lat.py

from PySide import QtCore, QtGui

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(708, 488)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(310, 170, 56, 13))
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar()
        self.menubar.setGeometry(QtCore.QRect(0, 0, 708, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("MainWindow", "TextLabel", None, QtGui.QApplication.UnicodeUTF8))

如何通过第一种方式访问​​主窗口的事件(例如resizeEvent)?

1 个答案:

答案 0 :(得分:1)

您的问题是因为当您使用* .ui文件时,实际上是在实例化2 QMainWindow。一旦您实例化您的类ControlMainWindow,一次加载UI文件。

不幸的是,PySide在这方面有点不足,解决方案基本上是将PySide UI加载器子类化来修复它。您应该可以关注https://stackoverflow.com/a/14894550/1994235来获得解决方案。请注意,链接到该帖子的loadUI代码值得了解,并且可能会更改(例如我不同意使用QMetaObject.connectSlotsByName(widget)