我想在MainWindow选项卡中显示一个Form。已经创建了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>
Home类:
# 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。
答案 0 :(得分:2)
您错误地认为通过执行sef.tabInvoices = ListInvoice()
来替换小部件,然后,否,变量self.tabInvoices指向对象,而当您重新分配它时,该变量将不再指向初始对象,而是到另一个。
考虑到以上情况,至少有两种解决方案:
使用布局将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) # <---
使用Qt Designer推广ListInvoice,有关更多信息,您可以查看以下答案:
考虑解决方案是将 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>
并删除家庭类中的self.tabInvoices = ListInvoice()
,因为这是不必要的。