对于QDialog而言,添加控件的过程如下:
1.创建子窗口部件,如
self.cursorTabWidget = QTabWidget()
tab1 = QWidget()
tabWidget = QTabWidget()
button1 = QPushButton("All Off")
checkbox = QCheckBox("Show in all diagrams")
button2 = QPushButton("Result export")
2.创建一个布局
tab1gbox = QGridLayout()
tab1gbox.setSpacing(10)
3.将子窗口部件添加到布局中去
tab1gbox.addWidget(tabWidget, 1, 1, 1, 8)
tab1gbox.addWidget(button1, 2, 1, 1, 1)
tab1gbox.addWidget(checkbox, 2, 2, 1, 6)
tab1gbox.addWidget(button2, 2, 3, 1, 1)
4.将布局设置到QDialog中去
tab1.setLayout(tab1gbox)
self.cursorTabWidget.addTab(tab1, "&Setup")`
这样,在QDialog就可以看到子窗口部件了。但是这样在QMainWindow种是看不到控件的。
这是因为QMainWindow中必须有一个CentralWidget,这时只要在最后加上如下代码即可显示。
self.setCentralWidget(self.cursorTabWidget)
如果把上述函数写成子函数,即:
def Cursor(self):
self.cursorTabWidget = QTabWidget()
self.cursorTabWidget.setSizePolicy(QSizePolicy.Preferred,
QSizePolicy.Ignored) # Setup
tab1 = QWidget()
tabWidget = QTabWidget()
button1 = QPushButton("All Off")
checkbox = QCheckBox("Show in all diagrams")
button2 = QPushButton("Result export")
# Setup -> C1
tab01 = QWidget()
tab01gbox = QGridLayout()
tab01gbox.setSpacing(10)
l1 = QLabel("Enable")
l2 = QLabel("Source")
l3 = QLabel("Type")
l4 = QLabel("Y user position 1")
l5 = QLabel("Y user position 2")
l6 = QLabel("X position 1")
l7 = QLabel("X position 2")
checkbox1 = QCheckBox("Track waveform")
checkbox2 = QCheckBox("Coupling(2 follows 1)")
checkbox3 = QCheckBox("coupling(2 follows 1)")
lineedit1 = QLineEdit()
lineedit2 = QLineEdit()
lineedit3 = QLineEdit()
lineedit4 = QLineEdit()
tab01gbox.addWidget(l1, 1, 1)
tab01gbox.addWidget(l2, 1, 2)
tab01gbox.addWidget(l3, 1, 3)
tab01gbox.addWidget(l4, 2, 1)
tab01gbox.addWidget(l5, 2, 2)
tab01gbox.addWidget(checkbox1, 2, 3)
tab01gbox.addWidget(lineedit1, 3, 1)
tab01gbox.addWidget(lineedit2, 3, 2)
tab01gbox.addWidget(checkbox2, 3, 3)
tab01gbox.addWidget(l6, 4, 1)
tab01gbox.addWidget(l7, 4, 2)
tab01gbox.addWidget(lineedit3, 5, 1)
tab01gbox.addWidget(lineedit4, 5, 2)
tab01gbox.addWidget(checkbox3, 5, 3)
tab01.setLayout(tab01gbox)
tab1gbox = QGridLayout()
tab1gbox.setSpacing(10)
tab1gbox.addWidget(tabWidget, 1, 1, 1, 8)
tab1gbox.addWidget(button1, 2, 1, 1, 1)
tab1gbox.addWidget(checkbox, 2, 2, 1, 6)
tab1gbox.addWidget(button2, 2, 3, 1, 1)
tab1.setLayout(tab1gbox)
tabWidget.addTab(tab01,"CH1")
self.cursorTabWidget.addTab(tab, "&Setup")
self.setCentralWidget(self.cursorTabWidget)
那么,在主函数里加上一句self.show()即可。