从 MainWindow 内的 QWidget 内的按钮更改 MainWindow 小部件
Posted
技术标签:
【中文标题】从 MainWindow 内的 QWidget 内的按钮更改 MainWindow 小部件【英文标题】:Change MainWindow widget from button inside QWidget inside MainWindow 【发布时间】:2015-12-03 21:44:29 【问题描述】:所以我有一个名为MainWindow
的空主框架和一个WelcomeWidget
,它在程序启动时被称为 immidiatley 并加载到主框架内。然后我希望 WelcomeWidget
内的按钮 next_btn
在 MainWindow
类内调用 LicenseWidget
QWidget 。我该怎么做?
这是我的代码:
Main.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Main.py
#
# Copyright 2015 Ognjen Galic <gala@thinkpad>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
from PyQt4 import QtGui, QtCore
from MainWindow import Ui_MainWindow
from WelcomeWidget import Ui_welcome_widget
from LicenseWidget import Ui_license_widget
import sys
class WelcomeWidget(QtGui.QWidget, Ui_welcome_widget):
def __init__(self, parent=None):
super(WelcomeWidget, self).__init__()
self.setupUi(self)
self.cancel_btn.pressed.connect(self.close)
self.next_btn.pressed.connect(self.license_show)
def close(self):
sys.exit(0)
def license_show(self):
mainWindow.cw = LicenseWidget(self)
mainWindow.setCentralWidget(self.cw)
class LicenseWidget(QtGui.QWidget, Ui_license_widget):
def __init__(self, parent=None):
super(LicenseWidget, self).__init__()
self.setupUi(self)
class mainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
super(mainWindow, self).__init__()
self.setupUi(self)
mainWindow.cw = WelcomeWidget(self)
self.setCentralWidget(self.cw)
def main():
app = QtGui.QApplication(sys.argv)
ui = mainWindow()
ui.show()
sys.exit(app.exec_())
main()
LicenseWidget.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'LicenseWidget.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
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_license_widget(object):
def setupUi(self, license_widget):
license_widget.setObjectName(_fromUtf8("license_widget"))
license_widget.resize(640, 420)
license_widget.setMinimumSize(QtCore.QSize(640, 420))
license_widget.setMaximumSize(QtCore.QSize(640, 420))
self.frame_btn = QtGui.QFrame(license_widget)
self.frame_btn.setGeometry(QtCore.QRect(0, 365, 641, 56))
self.frame_btn.setFrameShape(QtGui.QFrame.StyledPanel)
self.frame_btn.setFrameShadow(QtGui.QFrame.Raised)
self.frame_btn.setObjectName(_fromUtf8("frame_btn"))
self.no_btn = QtGui.QPushButton(self.frame_btn)
self.no_btn.setGeometry(QtCore.QRect(540, 15, 87, 26))
self.no_btn.setObjectName(_fromUtf8("no_btn"))
self.yes_btn = QtGui.QPushButton(self.frame_btn)
self.yes_btn.setGeometry(QtCore.QRect(430, 15, 87, 26))
self.yes_btn.setObjectName(_fromUtf8("yes_btn"))
self.back_btn = QtGui.QPushButton(self.frame_btn)
self.back_btn.setEnabled(True)
self.back_btn.setGeometry(QtCore.QRect(346, 15, 87, 26))
self.back_btn.setCheckable(False)
self.back_btn.setObjectName(_fromUtf8("back_btn"))
self.main_frame = QtGui.QFrame(license_widget)
self.main_frame.setGeometry(QtCore.QRect(0, 0, 640, 75))
self.main_frame.setMinimumSize(QtCore.QSize(8, 0))
self.main_frame.setFrameShape(QtGui.QFrame.StyledPanel)
self.main_frame.setFrameShadow(QtGui.QFrame.Raised)
self.main_frame.setObjectName(_fromUtf8("main_frame"))
self.Title = QtGui.QLabel(self.main_frame)
self.Title.setGeometry(QtCore.QRect(10, 5, 311, 61))
self.Title.setObjectName(_fromUtf8("Title"))
self.license_cont = QtGui.QTextEdit(license_widget)
self.license_cont.setGeometry(QtCore.QRect(0, 74, 640, 260))
self.license_cont.setObjectName(_fromUtf8("license_cont"))
self.agree_or_not = QtGui.QLabel(license_widget)
self.agree_or_not.setGeometry(QtCore.QRect(10, 340, 621, 17))
self.agree_or_not.setObjectName(_fromUtf8("agree_or_not"))
self.retranslateUi(license_widget)
QtCore.QMetaObject.connectSlotsByName(license_widget)
def retranslateUi(self, license_widget):
license_widget.setWindowTitle(_translate("license_widget", "Form", None))
self.no_btn.setText(_translate("license_widget", "No", None))
self.yes_btn.setText(_translate("license_widget", "Yes", None))
self.back_btn.setText(_translate("license_widget", "Back", None))
self.Title.setText(_translate("license_widget", "<html><head/><body><p><span style=\" font-size:11pt; font-weight:600;\">Program License</span></p><p>Please read the license carefully</p></body></html>", None))
self.license_cont.setHtml(_translate("license_widget", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li white-space: pre-wrap; \n"
"</style></head><body style=\" font-family:\'Droid Sans\'; font-size:10pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">example license</p></body></html>", None))
self.agree_or_not.setText(_translate("license_widget", "Do you agree to the license? If you click \"No\", the installer will close.", None))
WelcomeWidget.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'WelcomeWidget.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
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_welcome_widget(object):
def setupUi(self, welcome_widget):
welcome_widget.setObjectName(_fromUtf8("welcome_widget"))
welcome_widget.resize(640, 420)
welcome_widget.setMinimumSize(QtCore.QSize(640, 420))
welcome_widget.setMaximumSize(QtCore.QSize(640, 420))
self.side_pixmap = QtGui.QLabel(welcome_widget)
self.side_pixmap.setGeometry(QtCore.QRect(0, 0, 220, 365))
self.side_pixmap.setText(_fromUtf8(""))
self.side_pixmap.setPixmap(QtGui.QPixmap(_fromUtf8("media/InstallShield.png")))
self.side_pixmap.setObjectName(_fromUtf8("side_pixmap"))
self.welcome_frame = QtGui.QFrame(welcome_widget)
self.welcome_frame.setGeometry(QtCore.QRect(0, 365, 641, 56))
self.welcome_frame.setFrameShape(QtGui.QFrame.StyledPanel)
self.welcome_frame.setFrameShadow(QtGui.QFrame.Raised)
self.welcome_frame.setObjectName(_fromUtf8("welcome_frame"))
self.cancel_btn = QtGui.QPushButton(self.welcome_frame)
self.cancel_btn.setGeometry(QtCore.QRect(540, 15, 87, 26))
self.cancel_btn.setObjectName(_fromUtf8("cancel_btn"))
self.next_btn = QtGui.QPushButton(self.welcome_frame)
self.next_btn.setGeometry(QtCore.QRect(430, 15, 87, 26))
self.next_btn.setObjectName(_fromUtf8("next_btn"))
self.back_btn = QtGui.QPushButton(self.welcome_frame)
self.back_btn.setEnabled(False)
self.back_btn.setGeometry(QtCore.QRect(346, 15, 87, 26))
self.back_btn.setObjectName(_fromUtf8("back_btn"))
self.welcome_header = QtGui.QLabel(welcome_widget)
self.welcome_header.setEnabled(True)
self.welcome_header.setGeometry(QtCore.QRect(240, 10, 361, 91))
font = QtGui.QFont()
font.setPointSize(20)
self.welcome_header.setFont(font)
self.welcome_header.setWordWrap(True)
self.welcome_header.setObjectName(_fromUtf8("welcome_header"))
self.welcome_desc = QtGui.QLabel(welcome_widget)
self.welcome_desc.setGeometry(QtCore.QRect(240, 120, 391, 51))
self.welcome_desc.setWordWrap(True)
self.welcome_desc.setObjectName(_fromUtf8("welcome_desc"))
self.retranslateUi(welcome_widget)
QtCore.QMetaObject.connectSlotsByName(welcome_widget)
def retranslateUi(self, welcome_widget):
welcome_widget.setWindowTitle(_translate("welcome_widget", "Form", None))
self.cancel_btn.setText(_translate("welcome_widget", "Cancel", None))
self.next_btn.setText(_translate("welcome_widget", "Next", None))
self.back_btn.setText(_translate("welcome_widget", "Back", None))
self.welcome_header.setText(_translate("welcome_widget", "<html><head/><body><p><span style=\" font-size:16pt;\">Welcome to the InstallShield wizard for Google Chrome.</span></p></body></html>", None))
self.welcome_desc.setText(_translate("welcome_widget", "<html><head/><body><p>This install wizard will install Google Chrome to your computer. To continue press Next.</p></body></html>", None))
MainWindow.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'MainWindow.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!
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(640, 420)
MainWindow.setMinimumSize(QtCore.QSize(640, 420))
MainWindow.setMaximumSize(QtCore.QSize(640, 420))
MainWindow.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
MainWindow.setAnimated(False)
self.main_widget = QtGui.QWidget(MainWindow)
self.main_widget.setObjectName(_fromUtf8("main_widget"))
MainWindow.setCentralWidget(self.main_widget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "InstallShield Wizard", None))
如果这直接从 WelcomeWidget 类中运行,并告诉 MainWindow 类,那就太棒了。
class WelcomeWidget(QtGui.QWidget, Ui_welcome_widget):
[ ... ]
def license_show(self):
mainWindow.cw = LicenseWidget(self)
mainWindow.setCentralWidget(self.cw)
有答案的人会得到一个电子cookie!
【问题讨论】:
QWizardPage居然不能用? 【参考方案1】:QWizard
可能有用。另一种方法是将两个小部件布局在 QVerticalLayout
中并隐藏您不感兴趣的小部件。可见的小部件会占用所有空间。
它甚至可以完全在 QtCreator 中构建.. 只是 hide()
你不想看到的和 show()
你想看到的。
可以在 QtCreator 中构建具有大量小部件的复杂布局,并仅显示手头任务所需的内容。
【讨论】:
【参考方案2】:#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Main.py
#
# Copyright 2015 Ognjen Galic <gala@thinkpad>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
from PyQt4 import QtGui, QtCore
from MainWindow import Ui_MainWindow
from MainWidget import Ui_main_widget
from WelcomeWidget import Ui_welcome_widget
from LicenseWidget import Ui_license_widget
import sys
class mainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
super(mainWindow, self).__init__()
self.setupUi(self)
mainWindow.welcomeWidget = WelcomeWidget(self)
mainWindow.licenseWidget = LicenseWidget(self)
mainWindow.mainWidget = MainWidget(self)
self.setCentralWidget(self.mainWidget)
self.mainWidget.addWidget(self.welcomeWidget)
self.welcomeWidget.next_btn.pressed.connect(self.license_show)
self.welcomeWidget.cancel_btn.pressed.connect(self.close_cancel)
self.licenseWidget.cancel_btn.pressed.connect(self.close_cancel)
self.licenseWidget.back_btn.pressed.connect(self.go_back)
self.licenseWidget.i_do.toggled.connect(self.accept_license)
self.licenseWidget.i_dont.toggled.connect(self.dont_accept_license)
def go_back(self):
self.mainWidget.removeWidget(self.licenseWidget)
self.mainWidget.addWidget(self.welcomeWidget)
def close_cancel(self):
sys.exit(0)
def license_show(self):
self.mainWidget.removeWidget(self.welcomeWidget)
self.mainWidget.addWidget(self.licenseWidget)
def accept_license(self):
self.licenseWidget.next_btn.setEnabled(True)
def dont_accept_license(self):
self.licenseWidget.next_btn.setEnabled(False)
class WelcomeWidget(QtGui.QStackedWidget, Ui_welcome_widget):
def __init__(self, parent=None):
super(WelcomeWidget, self).__init__()
self.setupUi(self, "Linux 2.6.32.68")
class LicenseWidget(QtGui.QStackedWidget, Ui_license_widget):
def __init__(self, parent=None):
super(LicenseWidget, self).__init__()
license_file = open("license.html")
license_text_file = license_file.read()
self.setupUi(self, license_text_file)
class MainWidget(QtGui.QStackedWidget, Ui_main_widget):
def __init__(self, parent=None):
super(MainWidget, self).__init__()
self.setupUi(self)
def main():
app = QtGui.QApplication(sys.argv)
ui = mainWindow()
ui.show()
sys.exit(app.exec_())
main()
这行得通。感谢所有帮助过的人。
【讨论】:
以上是关于从 MainWindow 内的 QWidget 内的按钮更改 MainWindow 小部件的主要内容,如果未能解决你的问题,请参考以下文章
如何将数据从 MainWindow 传递到 MainWindow 内的用户控件?
循环刷新MainWindow以模拟QT中QWidget的移动