我想将一个窗体显示到mainWindow选项卡中。 mainWindow已经创建并填充有空白标签,Form也已经创建并填充有空白comboBox,标签等。
mainWindow称为home,并且称为Form listInvoice。它们自己可以很好地工作,但是当我从主页选项卡调用listInvoice时,它将打开一个显示listInvoice GUI的新窗口,而不是在选项卡中显示listInvoice。
您知道如何从mainWindow选项卡中调用表单吗?谢谢您的帮助。
home.ui文件(由QT Designer创建):
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>500</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="lDate">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QTabWidget" name="tabWidget">
<property name="tabPosition">
<enum>QTabWidget::West</enum>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<property name="elideMode">
<enum>Qt::ElideNone</enum>
</property>
<widget class="QWidget" name="tabHome">
<attribute name="title">
<string>Home</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_2"/>
</widget>
<widget class="QWidget" name="tabCompanies">
<attribute name="title">
<string>Companies</string>
</attribute>
</widget>
<widget class="QWidget" name="tabProjects">
<attribute name="title">
<string>Projects</string>
</attribute>
</widget>
<widget class="QWidget" name="tabInvoices">
<attribute name="title">
<string>Invoices</string>
</attribute>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
家庭课:
# Class import
from listInvoice import ListInvoice
# GUI manager
from PyQt5.QtWidgets import QApplication, QComboBox, QDialog, QDialogButtonBox, QFrame, QLabel, QLineEdit, QMainWindow, QMessageBox, QPushButton, QTabWidget, QVBoxLayout,QWidget
from PyQt5.uic import loadUi
# Other imports
import datetime
import sys
class Home(QMainWindow):
print("home.py")
def __init__(self):
super(Home, self).__init__()
loadUi("home.ui", self)
self.setWindowTitle("Home")
# Display current date
self.lDate.setText(str(datetime.date.today()))
# Insert widget into tabs
self.tabInvoices = ListInvoice()
app = QApplication(sys.argv)
widget = Home()
widget.show()
sys.exit(app.exec_())
listInvoice.ui(由QT Designer创建):
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>735</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="pbSearch">
<property name="text">
<string>Search</string>
</property>
</widget>
</item>
<item row="4" column="0" colspan="3">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="2">
<widget class="QPushButton" name="pbAddInvoice">
<property name="text">
<string>Add invoice</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Filter by:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="leSearch"/>
</item>
<item row="2" column="1" colspan="2">
<widget class="QPushButton" name="pbCertified">
<property name="text">
<string>To certified</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
<item row="3" column="0" colspan="3">
<widget class="QTableWidget" name="twInvoices"/>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
和ListInvoice类:
class ListInvoice(QDialog):
print("listInvoice.py")
def __init__(self):
super(ListInvoice, self).__init__()
loadUi("listInvoice.ui", self)
self.setWindowTitle("Invoices")
预期的输出应该是主页选项卡中的listInvoice。
最佳答案
您以为通过执行sef.tabInvoices = ListInvoice()
来替换小部件是错误的,然后否,变量self.tabInvoices指向该对象,而当您重新分配它时,该变量将不再指向初始对象,而是指向另一个对象。
考虑到上述情况,至少有两种解决方案:
1.使用布局:
使用布局将ListInvoice小部件放置在tabInvoices内,而不是替换它,而是放置在内部。
class Home(QMainWindow):
print("home.py")
def __init__(self):
super(Home, self).__init__()
loadUi("home.ui", self)
self.setWindowTitle("Home")
# Display current date
self.lDate.setText(str(datetime.date.today()))
# Insert widget into tabs
self.tabInvoices_w = ListInvoice() # <---
lay = QVBoxLayout(self.tabInvoices) # <---
lay.addWidget(self.tabInvoices_w) # <---
2.推广小部件
使用Qt Designer推广ListInvoice,有关更多信息,您可以查看以下答案:
https://stackoverflow.com/a/47273625
https://stackoverflow.com/a/46671439
https://stackoverflow.com/a/53504994
https://stackoverflow.com/a/53545128
https://stackoverflow.com/a/53926554
考虑到解决方案是将home.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>800</width>
<height>500</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="lDate">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QTabWidget" name="tabWidget">
<property name="tabPosition">
<enum>QTabWidget::West</enum>
</property>
<property name="currentIndex">
<number>3</number>
</property>
<property name="elideMode">
<enum>Qt::ElideNone</enum>
</property>
<widget class="QWidget" name="tabHome">
<attribute name="title">
<string>Home</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_2"/>
</widget>
<widget class="QWidget" name="tabCompanies">
<attribute name="title">
<string>Companies</string>
</attribute>
</widget>
<widget class="QWidget" name="tabProjects">
<attribute name="title">
<string>Projects</string>
</attribute>
</widget>
<widget class="ListInvoice" name="tabInvoices">
<attribute name="title">
<string>Invoices</string>
</attribute>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<customwidgets>
<customwidget>
<class>ListInvoice</class>
<extends>QWidget</extends>
<header>listInvoice</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
并删除home类中的
self.tabInvoices = ListInvoice()
,因为这是不必要的。关于python - 如何在mainWindow内插入表单,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56941021/