分割条能够通过拖动子控件之间的边界,使得它们的尺寸动态变化
QSplitter的函数
- addWidget():对splitter的布局上添加控件
- indexOf():返回控件的索引
- setSizes():设置每个控件的初始尺寸
- insertWidget():在指定索引上插入控件
- setOrientation():设置布局水平还是垂直,参数有Qt.Horizontal和Qt.Vertical
- count():返回控件的总数
QSplitter的信号
当分割条被拖动时,发射splitterMoved()信号。
栗子
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
| 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() splitter1 = QSplitter(Qt.Horizontal) splitter2 = QSplitter(Qt.Vertical)
textedit = QTextEdit() topleft = QFrame() bottom = QFrame() textedit = QTextEdit()
topleft.setFrameShape(QFrame.StyledPanel) bottom.setFrameShape(QFrame.StyledPanel)
splitter1.addWidget(topleft) splitter1.addWidget(textedit) splitter1.setSizes([100, 200]) splitter2.addWidget(splitter1) splitter2.addWidget(bottom) hbox.addWidget(splitter2)
self.setLayout(hbox) self.setGeometry(300, 300, 300, 200) self.setWindowTitle('QSplitter demo')
def main(): app = QApplication(sys.argv) ex = window() ex.show() sys.exit(app.exec_())
if __name__ == '__main__': main()
|