主要内容
- 通过继承实现自己的界面类;
- 涉及模块:QDialog, QLineEdit , QTextBrowser
- 界面布局:绝对布局,布局类
实例讲解
先看一段代码,我们定义了一个类Form,它继承自QDialog
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
|
class Form(QDialog): def __init__( self ,
parent = None ): super (Form, self ).__init__(parent) self .browser = QTextBrowser() self .lineedit = QLineEdit( "Type
an expression and press Enter" ) self .lineedit.selectAll() layout = QVBoxLayout() #垂直盒式布局 layout.addWidget( self .browser) layout.addWidget( self .lineedit)<br> #layout
= QGridLayout() #网格布局 #layout.addWidget(self.browser,0,
0) #layout.addWidget(self.lineedit,0,
0) self .setLayout(layout) self .lineedit.setFocus() self .connect( self .lineedit,
SIGNAL( "returnPressed()" ), self .updateUi) #信号绑定到槽 self .setWindowTitle( "Calculate" ) def updateUi( self ): try : text = unicode ( self .lineedit.text()) self .browser.append( "%s
= <b>%s</b>" % (text, eval (text))) except : self .browser.append( "<font
color=red>%s is invalid!</font>" % text) app = QApplication(sys.argv) form = Form() form.show() app.exec_() |
(1) QDialog是窗口类, QLineEdit文本框,QTextBrowser显示内容的文本区域,支持html格式语法。
(2)layout = QVBoxLayout() 垂直盒式布局,即内容对象上下排列
说到布局,主要分为两类:绝对布局,相对布局(相应类)
- 绝对布局,调用move(x,y)方法
- 相对布局,常用的有 QHBoxLayout, QVBoxLayout 和QGridLayout (网格布局)
(3)特别注意的是 self.connect(self.lineedit, SIGNAL("returnPressed()"), self.updateUi)
这是PyQt的事件处理机制 ---- 信号与槽(Signals and slots):
信号相当于一个事件:如点击按钮,完成输入后按回车等等;槽相当于处理函数。
在上面程序中,当在文本框中完成输入按回车时,就会调用updateUi函数,这就是connect绑定的效果。
程序效果如下:
简要总结:
- 通过继承实现窗体类
- 窗口布局方法
- QDialog, QLineEdit , QTextBrowser
- QLineEdit 方法:
- 获取unicode 文本: unicode(lineEdit.text())
- QTextBrowser 方法:
- 添加内容 textBrowser.appen("formatText")
- 信号与槽
- self.connect(widget, signal , slot)
扩展知识:
熟悉常用的窗口组件:
1 按钮类
QPushButton |
普通按钮 |
QToolButton |
工具按钮:通常在工具栏使用 |
QRadioButton |
单选框 |
QCheckBox |
复选框 |
QCommanLinkButton |
Vista风格的命令链接按钮 |
QDialogButtonBox |
对话框按钮组:确定、取消 |
2 显示组件
QLabel |
标签 |
QTextBrowser |
文本区域 |
QGraphicsView |
图像显示 |
QCalendarWidget |
日历组件 |
QProgressBar |
进度条 |
QLCDNumber |
液晶数字显示 |
QWebView |
Web浏览器视图 |
QDeclarativeView |
显示Qt声明的用户接口 |
3 输入组件
QComboBox |
下拉选框 |
QFontComboBox |
字体选择 |
QLineEdit |
单行文本框 |
QTextEdit |
多行文本框(富文本) |
QPlainTextEdit |
多行文本框(纯文本) |
QSpinBox |
整数范围调节器 |
QDoubleSpinBox |
实型范围调节器 |
QDial |
环形范围调节器 |
QSlider |
滑动调节器 |
QTimeEdit |
时间输入框 |
QDateEdit |
日期输入框 |
QDateTimeEdit |
时间日期输入框 |
4 容器类
QFrame |
帧窗口 |
QWidget |
界面部件,所有界面对象类的基类 |
QToolBox |
工具栏容器 |
QTabWidget |
多标签容器 |
QStackedWidget |
层次容器,一次只有一个可见 |
QScollArea |
滚动区域 |
QGroupBox |
对象组容器 |
QMdiArea |
多文档容器 |
QDockWidget |
悬浮容器 |
QDail, QSpinBox的使用
QDial:环形的范围选择器
QSpinBox :下拉列表形式的整数选择器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
class Form(QDialog): def __init__( self ,
parent = None ): super (Form, self ).__init__(parent) dial = QDial() dial.setNotchesVisible( True ) spinbox = QSpinBox() layout = QHBoxLayout() layout.addWidget(dial) layout.addWidget(spinbox) self .setLayout(layout) self .connect(dial,
SIGNAL( "valueChanged(int)" ),spinbox.setValue) self .connect(spinbox,
SIGNAL( "valueChanged(int)" ),dial.setValue) self .setWindowTitle( "Signals
and Slots" ) |