在qt4中单击按钮时从另一个窗口生成一个窗口 [英] Spawning one window from another window on button click in qt4

查看:41
本文介绍了在qt4中单击按钮时从另一个窗口生成一个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 qt4 设计器创建了两个窗口,并希望将它们链接在一起.我将它们都放在一个文件夹中,并在目录外创建了一个文件,我将使用该文件导入它们.

I've created two windows using qt4 designer, and would like to link them together. I put them both in a folder and created a file outside the directory, which I'm importing them with.

我可以同时打开两个窗口,但这不是我想要做的.我想让它在一个窗口上按下一个按钮时,它会打开另一个窗口,然后关闭第一个窗口,所以第二个窗口是唯一打开的.

I can open both windows at the same time, but that's not what I would like to do. I would like to make it so that when a button is pushed on one window, it opens the other window, then closes the first window, so the second window is the only one left open.

这是第一个窗口的代码,我想打开另一个窗口的按钮旁边有注释.

This is the code for the first window, with comments next to the button that I would like to have open the other window.

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(893, 591)
        MainWindow.setMinimumSize(QtCore.QSize(893, 591))
        MainWindow.setMaximumSize(QtCore.QSize(893, 591))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8("images/turbulence.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        MainWindow.setStyleSheet(_fromUtf8(""))
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.label_2 = QtGui.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(-10, 50, 911, 581))
        self.label_2.setStyleSheet(_fromUtf8("background-color: rgb(0, 0, 0);"))
        self.label_2.setText(_fromUtf8(""))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(-20, -10, 921, 71))
        self.label.setStyleSheet(_fromUtf8("background-color: rgb(0, 37, 37);"))
        self.label.setText(_fromUtf8(""))
        self.label.setPixmap(QtGui.QPixmap(_fromUtf8("images/header.png")))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_3 = QtGui.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(60, 0, 351, 51))
        self.label_3.setStyleSheet(_fromUtf8("font: 20pt \"Sans Serif\";\n"
"color: rgb(0, 221, 221);"))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.label_4 = QtGui.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(-4, 73, 901, 41))
        self.label_4.setStyleSheet(_fromUtf8("background-color: rgba(13, 247, 255, 72);"))
        self.label_4.setText(_fromUtf8(""))
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.label_6 = QtGui.QLabel(self.centralwidget)
        self.label_6.setGeometry(QtCore.QRect(110, 80, 31, 31))
        self.label_6.setPixmap(QtGui.QPixmap(_fromUtf8("images/arrow.png")))
        self.label_6.setObjectName(_fromUtf8("label_6"))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(10, 80, 101, 31))
        self.pushButton.setStyleSheet(_fromUtf8("font: 13pt \"Sans Serif\";\n"
"color: rgb(0, 255, 255);"))
        self.pushButton.setFlat(True)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(140, 80, 81, 31))
        self.pushButton_2.setStyleSheet(_fromUtf8("font: 12pt \"Sans Serif\";\n"
"color: rgb(0, 255, 255);"))
        self.pushButton_2.setFlat(True)
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.label_5 = QtGui.QLabel(self.centralwidget)
        self.label_5.setGeometry(QtCore.QRect(6, 3, 51, 51))
        self.label_5.setText(_fromUtf8(""))
        self.label_5.setPixmap(QtGui.QPixmap(_fromUtf8("images/turbulence.png")))
        self.label_5.setObjectName(_fromUtf8("label_5"))
        self.label_7 = QtGui.QLabel(self.centralwidget)
        self.label_7.setGeometry(QtCore.QRect(20, 150, 491, 91))
        self.label_7.setStyleSheet(_fromUtf8("font: 30pt \"Sans Serif\";\n"
"color: rgb(0, 191, 191);"))
        self.label_7.setObjectName(_fromUtf8("label_7"))
        self.label_8 = QtGui.QLabel(self.centralwidget)
        self.label_8.setGeometry(QtCore.QRect(60, 240, 831, 271))
        self.label_8.setStyleSheet(_fromUtf8("color: rgb(0, 191, 191);\n"
"font: 10pt \"Sans Serif\";"))
        self.label_8.setObjectName(_fromUtf8("label_8"))
        self.label_9 = QtGui.QLabel(self.centralwidget)
        self.label_9.setGeometry(QtCore.QRect(60, 350, 21, 21))
        self.label_9.setPixmap(QtGui.QPixmap(_fromUtf8("images/bullet.png")))
        self.label_9.setObjectName(_fromUtf8("label_9"))
        self.label_10 = QtGui.QLabel(self.centralwidget)
        self.label_10.setGeometry(QtCore.QRect(60, 380, 21, 21))
        self.label_10.setPixmap(QtGui.QPixmap(_fromUtf8("images/bullet.png")))
        self.label_10.setObjectName(_fromUtf8("label_10"))
        self.label_11 = QtGui.QLabel(self.centralwidget)
        self.label_11.setGeometry(QtCore.QRect(60, 410, 21, 21))
        self.label_11.setPixmap(QtGui.QPixmap(_fromUtf8("images/bullet.png")))
        self.label_11.setObjectName(_fromUtf8("label_11"))
        self.label_12 = QtGui.QLabel(self.centralwidget)
        self.label_12.setGeometry(QtCore.QRect(60, 450, 21, 31))
        self.label_12.setPixmap(QtGui.QPixmap(_fromUtf8("images/bullet.png")))
        self.label_12.setObjectName(_fromUtf8("label_12"))
        self.label_13 = QtGui.QLabel(self.centralwidget)
        self.label_13.setGeometry(QtCore.QRect(-10, 550, 911, 51))
        self.label_13.setStyleSheet(_fromUtf8("background-color: rgba(13, 247, 255, 72);"))
        self.label_13.setText(_fromUtf8(""))
        self.label_13.setObjectName(_fromUtf8("label_13"))
        self.pushButton_3 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(818, 560, 71, 21))
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(_fromUtf8("images/arrow.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_3.setIcon(icon1)
        self.pushButton_3.setIconSize(QtCore.QSize(20, 20))
        self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
        self.pushButton_4 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_4.setGeometry(QtCore.QRect(10, 560, 71, 23))
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap(_fromUtf8("images/cancel.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_4.setIcon(icon2)
        self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QObject.connect(self.pushButton_4, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.close)
        QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.close) #This button
        QtCore.QObject.connect(self.pushButton_3, QtCore.SIGNAL(_fromUtf8("clicke
import sysd()")), MainWindow.close) #And this button too.
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "Turbulence", None))
        self.label_3.setText(_translate("MainWindow", "label3", None))
        self.pushButton.setText(_translate("MainWindow", "Welcome", None))
        self.pushButton_2.setText(_translate("MainWindow", "Folders", None))
        self.label_7.setText(_translate("MainWindow", "label7", None))
        self.label_8.setText(_translate("MainWindow", "label8", None))
        self.pushButton_3.setText(_translate("MainWindow", "Next", None))
        self.pushButton_4.setText(_translate("MainWindow", "Cancel", None))

这是第二个窗口的代码

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(893, 591)
        MainWindow.setMinimumSize(QtCore.QSize(893, 591))
        MainWindow.setMaximumSize(QtCore.QSize(893, 591))
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(_fromUtf8("images/turbulence.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        MainWindow.setStyleSheet(_fromUtf8(""))
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.label_2 = QtGui.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(-10, 50, 911, 581))
        self.label_2.setStyleSheet(_fromUtf8("background-color: rgb(0, 0, 0);"))
        self.label_2.setText(_fromUtf8(""))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(-20, -10, 921, 71))
        self.label.setStyleSheet(_fromUtf8("background-color: rgb(0, 37, 37);"))
        self.label.setText(_fromUtf8(""))
        self.label.setPixmap(QtGui.QPixmap(_fromUtf8("../images/header.png")))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_3 = QtGui.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(60, 0, 351, 51))
        self.label_3.setStyleSheet(_fromUtf8("font: 20pt \"Sans Serif\";\n"
"color: rgb(0, 221, 221);"))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.label_4 = QtGui.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(-4, 73, 901, 41))
        self.label_4.setStyleSheet(_fromUtf8("background-color: rgba(13, 247, 255, 72);"))
        self.label_4.setText(_fromUtf8(""))
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.label_6 = QtGui.QLabel(self.centralwidget)
        self.label_6.setGeometry(QtCore.QRect(100, 80, 31, 31))
        self.label_6.setPixmap(QtGui.QPixmap(_fromUtf8("../images/arrow.png")))
        self.label_6.setObjectName(_fromUtf8("label_6"))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(10, 80, 91, 31))
        self.pushButton.setStyleSheet(_fromUtf8("font: 13pt \"Sans Serif\";\n"
"color: rgb(0, 255, 255);"))
        self.pushButton.setFlat(True)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.pushButton_2 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(130, 80, 91, 31))
        self.pushButton_2.setStyleSheet(_fromUtf8("font: 12pt \"Sans Serif\";\n"
"color: rgb(0, 255, 255);"))
        self.pushButton_2.setFlat(True)
        self.pushButton_2.setObjectName(_fromUtf8("pushButton_2"))
        self.label_5 = QtGui.QLabel(self.centralwidget)
        self.label_5.setGeometry(QtCore.QRect(6, 3, 51, 51))
        self.label_5.setText(_fromUtf8(""))
        self.label_5.setPixmap(QtGui.QPixmap(_fromUtf8("../images/turbulence.png")))
        self.label_5.setObjectName(_fromUtf8("label_5"))
        self.label_13 = QtGui.QLabel(self.centralwidget)
        self.label_13.setGeometry(QtCore.QRect(-10, 550, 911, 51))
        self.label_13.setStyleSheet(_fromUtf8("background-color: rgba(13, 247, 255, 72);"))
        self.label_13.setText(_fromUtf8(""))
        self.label_13.setObjectName(_fromUtf8("label_13"))
        self.pushButton_3 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_3.setGeometry(QtCore.QRect(818, 560, 71, 21))
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(_fromUtf8("../images/arrow.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_3.setIcon(icon1)
        self.pushButton_3.setIconSize(QtCore.QSize(20, 20))
        self.pushButton_3.setObjectName(_fromUtf8("pushButton_3"))
        self.pushButton_4 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_4.setGeometry(QtCore.QRect(10, 560, 71, 23))
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap(_fromUtf8("../images/cancel.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_4.setIcon(icon2)
        self.pushButton_4.setObjectName(_fromUtf8("pushButton_4"))
        self.pushButton_5 = QtGui.QPushButton(self.centralwidget)
        self.pushButton_5.setGeometry(QtCore.QRect(720, 560, 91, 21))
        icon3 = QtGui.QIcon()
        icon3.addPixmap(QtGui.QPixmap(_fromUtf8("../images/arrowreverse.png")), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.pushButton_5.setIcon(icon3)
        self.pushButton_5.setIconSize(QtCore.QSize(20, 20))
        self.pushButton_5.setObjectName(_fromUtf8("pushButton_5"))
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QObject.connect(self.pushButton_4, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.close)
        QtCore.QObject.connect(self.pushButton_2, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.close)
        QtCore.QObject.connect(self.pushButton_3, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.close)
        QtCore.QObject.connect(self.pushButton_5, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.close)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "Turbulence", None))
        self.label_3.setText(_translate("MainWindow", "Customize Your Desktop", None))
        self.pushButton.setText(_translate("MainWindow", "Folders", None))
        self.pushButton_2.setText(_translate("MainWindow", "Themes", None))
        self.pushButton_3.setText(_translate("MainWindow", "Next", None))
        self.pushButton_4.setText(_translate("MainWindow", "Cancel", None))
        self.pushButton_5.setText(_translate("MainWindow", "Previous", None))

我认为这样做的方法可能是创建一个函数来打开第二个窗口,然后关闭第一个窗口,但我不确定是否应该将它放在我用来导入这两个窗口的文件中,或第一个窗口.

I think the way to do this might be to create a function which opens the second window, then closes the first, but I'm not sure if I should put this in the file I'm using to import these two windows, or the first window.

我可以将按钮连接到通常会打开另一个窗口的功能,但是当我点击按钮时什么也没有发生.

I can hook up the buttons to a function that would normally open up the other window, but when I click on the button nothing happens.

感谢您的帮助.

推荐答案

您确实需要重新考虑应用程序的设计.

You really need to reconsider the design of your application.

以您描述的方式打开和关闭多个主窗口很丑陋且完全没有必要.相反,您应该有一个主窗口并使用 QStackedWidget 来保存一个可以使用按钮导航的页面序列.

Opening and closing multiple main windows in the manner you describe is ugly and completely unnecessary. Instead, you should have one main window and use a QStackedWidget to hold a sequence of pages that can navigated through using buttons.

要试验这个想法,请在 Qt Designer 中创建一个新的主窗口并向其添加 QStackedWidget(位于容器"部分).然后打开你已经设计好的两个 UI,将每个 UI 的小部件复制到堆叠小部件的单独页面中.

To experiment with this idea, create a new main window in Qt Designer and add a QStackedWidget to it (it's in the "Containers" section). Then open the two UIs you've already designed, and copy the widgets of each UI into separate pages of the stacked-widget.

完成后,请确保为所有小部件提供描述性名称,因为稍后在开始为程序编写逻辑时需要引用它们.您的应用程序的主脚本应如下所示:

Once you've done that, make sure you give all the widgets descriptive names, because you will need to refer to them later when you start writing the logic for your program. The main script of your application should look something like this:

from PyQt4 import QtCore, QtGui
from mainwindow_ui import Ui_MainWindow

class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)
        ...
        self.buttonNext.clicked.connect(self.handleButtonNext)
        self.buttonPrev.clicked.connect(self.handleButtonPrev)

    def handleButtonNext(self):
        index = self.stackedWidget.currentIndex() + 1
        if index < self.stackedWidget.count():
            self.stackedWidget.setCurrentWidget(index)

    def handleButtonPrev(self):
        index = self.stackedWidget.currentIndex() - 1
        if index >= 0:
            self.stackedWidget.setCurrentWidget(index)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

显然,您的程序的真正逻辑将比这更复杂,但它应该让您对如何处理事情有一个大致的了解.

Obviously, the real logic for your program will be more sophisticated than this, but it should give you a general idea of how to go about things.

附注:

我自己从未使用过它,但您可能还想看看 QWizard 类可能更适合您的需求.

I have never used it myself, but you might also want to see if the QWizard class might be more suitable for your needs.

这篇关于在qt4中单击按钮时从另一个窗口生成一个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆