如何将面向对象的概念应用于2个类似的qt-designer页面

时间:2019-10-01 00:23:26

标签: python qt-designer pyside2

如何通过Qt-designer创建的UI页面在python中应用面向对象的概念?

我使用Qt-designer创建了Dog and Cat的2个屏幕。它们非常相似,除了每页的图像显示狗或猫,而“狗”页上有1个额外的按钮。然后,我使用pyside2-uic将用户界面构建到python文件中。通过将UI对象添加到Dog或Cat的class参数中,我可以成功地分别运行每个对象。但是,这不是代码可重用。我想创建一个常见的“动物”对象,并根据其配置加载UI页面。

from PySide2 import QtWidgets, QtGui
from demo.ui import page1


class Dog(page1.Ui_MainWindow, QtWidgets.QMainWindow):
    def __init__(self):
        super(Dog, self).__init__()
        self.setupUi(self)

        self.button1.clicked.connect(self.func1)
        self.button2.clicked.connect(self.func2)
        self.buttonDogOnly.clicked.connect(self.dogOnly)
        self.cancelButton.clicked.connect(self.cancelfunc)

    def func1(self):
        self.textEdit.append("This is button 1")
        QtWidgets.QMessageBox.about(self, "popup", "BUTTON 1")
        QtGui.qApp.processEvents()
        self.update()

    def func2(self):
        self.textEdit.append("This is button 2")
        QtWidgets.QMessageBox.about(self, "popup", "BUTTON 2")
        QtGui.qApp.processEvents()
        self.update()

    def dogOnly(self):
        self.textEdit.append("Dog only button")
        QtWidgets.QMessageBox.about(self, "popup", "Dog only button")
        QtGui.qApp.processEvents()
        self.update()

    def cancelfunc(self):
        self.textEdit.append("This is CANCEL BUTTON")
        QtWidgets.QMessageBox.about(self, "popup", "CANCEL BUTTON")
        QtGui.qApp.processEvents()
        self.update()

if __name__ == '__main__':
    app = QtWidgets.QApplication()
    homepage = Dog()
    homepage.show()
    app.exec_()

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'demo/ui/page1.ui',
# licensing of 'demo/ui/page1.ui' applies.
#
# Created: Mon Sep 30 16:38:55 2019
#      by: pyside2-uic  running on PySide2 5.12.1
#
# WARNING! All changes made in this file will be lost!

from PySide2 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(890, 755)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.frame = QtWidgets.QFrame(self.centralwidget)
        self.frame.setGeometry(QtCore.QRect(0, 0, 861, 701))
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.frame_2 = QtWidgets.QFrame(self.frame)
        self.frame_2.setGeometry(QtCore.QRect(10, 310, 321, 381))
        self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame_2.setObjectName("frame_2")
        self.button1 = QtWidgets.QPushButton(self.frame_2)
        self.button1.setGeometry(QtCore.QRect(20, 20, 121, 31))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(11)
        font.setWeight(75)
        font.setBold(True)
        self.button1.setFont(font)
        self.button1.setAutoFillBackground(True)
        self.button1.setStyleSheet("")
        self.button1.setObjectName("button1")
        self.button2 = QtWidgets.QPushButton(self.frame_2)
        self.button2.setGeometry(QtCore.QRect(170, 20, 121, 31))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(11)
        font.setWeight(75)
        font.setBold(True)
        self.button2.setFont(font)
        self.button2.setAutoFillBackground(True)
        self.button2.setStyleSheet("")
        self.button2.setObjectName("button2")
        self.cancelButton = QtWidgets.QPushButton(self.frame_2)
        self.cancelButton.setGeometry(QtCore.QRect(20, 70, 121, 31))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(11)
        font.setWeight(75)
        font.setBold(True)
        self.cancelButton.setFont(font)
        self.cancelButton.setAutoFillBackground(True)
        self.cancelButton.setStyleSheet("")
        self.cancelButton.setObjectName("cancelButton")
        self.buttonDogOnly = QtWidgets.QPushButton(self.frame_2)
        self.buttonDogOnly.setGeometry(QtCore.QRect(170, 70, 121, 31))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(11)
        font.setWeight(75)
        font.setBold(True)
        self.buttonDogOnly.setFont(font)
        self.buttonDogOnly.setAutoFillBackground(True)
        self.buttonDogOnly.setStyleSheet("")
        self.buttonDogOnly.setObjectName("buttonDogOnly")
        self.frame_3 = QtWidgets.QFrame(self.frame)
        self.frame_3.setGeometry(QtCore.QRect(340, 10, 511, 681))
        self.frame_3.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame_3.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame_3.setObjectName("frame_3")
        self.textEdit = QtWidgets.QTextEdit(self.frame_3)
        self.textEdit.setGeometry(QtCore.QRect(10, 10, 491, 661))
        self.textEdit.setObjectName("textEdit")
        self.frame_4 = QtWidgets.QFrame(self.frame)
        self.frame_4.setGeometry(QtCore.QRect(10, 10, 321, 291))
        self.frame_4.setAutoFillBackground(False)
        self.frame_4.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame_4.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame_4.setObjectName("frame_4")
        self.dogLabel = QtWidgets.QLabel(self.frame_4)
        self.dogLabel.setGeometry(QtCore.QRect(10, 10, 301, 271))
        self.dogLabel.setAutoFillBackground(False)
        self.dogLabel.setStyleSheet("image:url(:/animals/dog.png)")
        self.dogLabel.setText("")
        self.dogLabel.setObjectName("dogLabel")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar()
        self.menubar.setGeometry(QtCore.QRect(0, 0, 890, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtWidgets.QApplication.translate("MainWindow", "MainWindow", None, -1))
        self.button1.setText(QtWidgets.QApplication.translate("MainWindow", "Button 1", None, -1))
        self.button2.setText(QtWidgets.QApplication.translate("MainWindow", "Button 2", None, -1))
        self.cancelButton.setText(QtWidgets.QApplication.translate("MainWindow", "Cancel", None, -1))
        self.buttonDogOnly.setText(QtWidgets.QApplication.translate("MainWindow", "Dog only", None, -1))

import demo.images_rc

0 个答案:

没有答案