resizeEvent doesn't work if I use QUiLoader for QMainWindow

萝らか妹 提交于 2021-02-11 14:57:28

问题


I load main window from ui file: 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>

in this case resizeEvent doesn't get any events. However, if I load my design ui after pyside-uic compilation it works well 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))

How I can get access to events of main window (example resizeEvent) by the first way?


回答1:


Your problem is because when you use the *.ui file, you are actually instantiating 2 QMainWindows. Once when you instantiate your class ControlMainWindow and once when you load the UI file.

Unfortunately PySide is a little deficient in this regard, and the solution is basically to subclass the PySide UI loader to fix it. You should be able to follow https://stackoverflow.com/a/14894550/1994235 to get your solution. Note that the loadUI code linked to in that post is worth getting to know, and possibly change (for instance I don't agree with the use of QMetaObject.connectSlotsByName(widget))



来源:https://stackoverflow.com/questions/20312808/resizeevent-doesnt-work-if-i-use-quiloader-for-qmainwindow

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!