QStackedWidget跟QTabWidget类似,也能有效利用空间
- addTab():添加一个标签以及与之相关联的页面
- insertTab():在想要的位置插入一个标签及页面
- removeTab():删除给定索引的标签
- setCurrentIndex():设置当前可见的页面的索引作为当前操作
- setCurrentWidget():使可见页面作为当前
- setTabBar():设置标签栏
- setTabPosition():设置标签位置,参数有QTabWidget.North(页面上方)、QTabWidget.South(页面下方)、QTabWidget.West(页面左侧)、QTabWidget.East(页面右侧)
- setTabText():定义该tab的文本
- currentChanged():当前页面索引变化
- tabClosedRequested():点击了标签上的关闭按钮
栗子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| import sys from PyQt4.QtCore import * from PyQt4.QtGui import *
class window(QWidget): def __init__(self, parent=None): super(window, self).__init__(parent) self.init()
def init(self): hbox = QHBoxLayout() self.leftlist = QListWidget() self.Stack = QStackedWidget()
self.leftlist.insertItem(0,"Contact") self.leftlist.insertItem(1,"Personal") self.leftlist.insertItem(2,"Educational") self.stack1 = QWidget() self.stack2 = QWidget() self.stack3 = QWidget() self.stack1Window() self.stack2Window() self.stack3Window() self.leftlist.currentRowChanged.connect(self.display)
self.Stack.addWidget(self.stack1) self.Stack.addWidget(self.stack2) self.Stack.addWidget(self.stack3) hbox.addWidget(self.leftlist) hbox.addWidget(self.Stack) self.setLayout(hbox) self.setWindowTitle(u"关联标签显示")
def stack1Window(self): layout = QFormLayout() layout.addRow("Name", QLineEdit()) layout.addRow("Address", QLineEdit()) self.stack1.setLayout(layout)
def stack2Window(self): layout = QFormLayout() sex = QHBoxLayout() sex.addWidget(QRadioButton("Male")) sex.addWidget(QRadioButton("Female")) layout.addRow(QLabel("Sex"), sex) layout.addRow("Date of Birth", QLineEdit())
self.stack2.setLayout(layout)
def stack3Window(self): layout = QHBoxLayout() layout.addWidget(QLabel("subjects")) layout.addWidget(QCheckBox("Physics")) layout.addWidget(QCheckBox("Maths")) self.stack3.setLayout(layout)
def display(self, i): self.Stack.setCurrentIndex(i)
def main(): app = QApplication(sys.argv) ex = window() ex.show() sys.exit(app.exec_())
if __name__ == '__main__': main()
|