PySide: 构建一个"查找"对话框

先看一下最终的效果图:

完全代码:

#!/usr/bin/env python

import sys

from PySide.QtCore import *
from PySide.QtGui import *

class FindDialog(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__(self, parent)

        #创建Label & Edit
        label = QLabel('Find &what:')
        lineEdit = QLineEdit()
        label.setBuddy(lineEdit) #当按下Alt+w时,焦点落在Edit上。

        #创建CheckBox
        caseCheckBox = QCheckBox('Match &case')
        backwardCheckBox = QCheckBox('Search &backward')

        #创建Find Button
        findButton = QPushButton('&Find')
        findButton.setDefault(True) #设置为默认按钮
        findButton.setEnabled(False) #设置按钮禁用

        closeButton = QPushButton('Close')

        #布局对话框内容
        topLeftLayout = QHBoxLayout()
        topLeftLayout.addWidget(label)
        topLeftLayout.addWidget(lineEdit)

        leftLayout = QVBoxLayout()
        leftLayout.addLayout(topLeftLayout)
        leftLayout.addWidget(caseCheckBox)
        leftLayout.addWidget(backwardCheckBox)

        rightLayout = QVBoxLayout()
        rightLayout.addWidget(findButton)
        rightLayout.addWidget(closeButton)
        rightLayout.addStretch()

        mainLayout = QHBoxLayout()
        mainLayout.addLayout(leftLayout)
        mainLayout.addLayout(rightLayout)

        self.setLayout(mainLayout)

        #设置对话框标题
        self.setWindowTitle('Find')

def main():
    app = QApplication(sys.argv)
    d = FindDialog()
    d.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

这段代码中没有实际功能代码,主要演示通过QHBoxLayout和QVBoxLayout构建一个"查找"对话框用户界面。

下面的对话框布局图和父子关系图:

posted @ 2010-10-27 14:52  Sean Lv  阅读(521)  评论(0编辑  收藏