在 Qt 中更新子部件
Updating child widget in Qt
我有一个简单的项目 classes
- class 主窗口(QMainWindow)
- class主页(QWidget)
- class 登录(QWidget)
我想要的只是能够嵌套 QWidget classes(使它们成为 QMainWindow 的子对象)并在 MainWindow 中显示它们。在 MainWindow 中调用 QWidgets "appear" 后,我不知道如何制作它们。
代码如下:
import sys
from gui.MainWindow import Ui_MainWindow
from gui.home import Ui_Home
from gui.login import Ui_Login
from PyQt4.QtGui import QMainWindow, QApplication, QWidget
class Home(QWidget, Ui_Home):
def __init__(self):
QWidget.__init__(self)
self.setupUi(self)
class Login(QWidget, Ui_Login):
def __init__(self):
QWidget.__init__(self)
self.setupUi(self)
class MainWindow(QMainWindow,Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
#INSERT pushButton.click to go to HOME here
#INSERT pushButton.click to go to LOGIN here
def setHome(self):
self.label_Screen.setText("HOME")
self.mainwidget = Home()
#NEEDS SOMETHING HERE
def setLogin(self):
self.label_Screen.setText("LOGIN")
self.mainwidget = Login()
#NEEDS SOMETHING HERE
if __name__ == '__main__':
app = QApplication(sys.argv)
Main = MainWindow()
Main.show()
sys.exit(app.exec_())
我想我只需要一些我标记为“#NEEDS SOMETHING HERE”的东西,但我不确定是什么!
干杯!
已解决:感谢kh25
只需向 QMainWindow 添加布局并将 setHome 更改为:
def setHome(self):
self.label_Screen.setText("HOME")
self.currentScreen = Home()
self.layout.addWidget(self.currentScreen)
self.setLayout(self.layout)
setLogin 方法也应该做同样的事情。
您需要先创建一个布局并将小部件添加到该布局中。有多种类型的布局。阅读此处:
http://doc.qt.io/qt-4.8/layout.html
对于像您这样的简单案例,我建议使用 QHBoxLayout 或 QVBoxLayout。
声明此布局。在布局上为每个登录和主页小部件调用 addWidget(),然后在 QMainWindow 上调用 setLayout()。
我有一个简单的项目 classes
- class 主窗口(QMainWindow)
- class主页(QWidget)
- class 登录(QWidget)
我想要的只是能够嵌套 QWidget classes(使它们成为 QMainWindow 的子对象)并在 MainWindow 中显示它们。在 MainWindow 中调用 QWidgets "appear" 后,我不知道如何制作它们。
代码如下:
import sys
from gui.MainWindow import Ui_MainWindow
from gui.home import Ui_Home
from gui.login import Ui_Login
from PyQt4.QtGui import QMainWindow, QApplication, QWidget
class Home(QWidget, Ui_Home):
def __init__(self):
QWidget.__init__(self)
self.setupUi(self)
class Login(QWidget, Ui_Login):
def __init__(self):
QWidget.__init__(self)
self.setupUi(self)
class MainWindow(QMainWindow,Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
#INSERT pushButton.click to go to HOME here
#INSERT pushButton.click to go to LOGIN here
def setHome(self):
self.label_Screen.setText("HOME")
self.mainwidget = Home()
#NEEDS SOMETHING HERE
def setLogin(self):
self.label_Screen.setText("LOGIN")
self.mainwidget = Login()
#NEEDS SOMETHING HERE
if __name__ == '__main__':
app = QApplication(sys.argv)
Main = MainWindow()
Main.show()
sys.exit(app.exec_())
我想我只需要一些我标记为“#NEEDS SOMETHING HERE”的东西,但我不确定是什么!
干杯!
已解决:感谢kh25
只需向 QMainWindow 添加布局并将 setHome 更改为:
def setHome(self):
self.label_Screen.setText("HOME")
self.currentScreen = Home()
self.layout.addWidget(self.currentScreen)
self.setLayout(self.layout)
setLogin 方法也应该做同样的事情。
您需要先创建一个布局并将小部件添加到该布局中。有多种类型的布局。阅读此处:
http://doc.qt.io/qt-4.8/layout.html
对于像您这样的简单案例,我建议使用 QHBoxLayout 或 QVBoxLayout。
声明此布局。在布局上为每个登录和主页小部件调用 addWidget(),然后在 QMainWindow 上调用 setLayout()。