PyQT tutorial
2014-01-03 23:29
471 查看
### Misc
1. QWidget(包括派生类)background color 有两种 设置方式:palette 和 style sheet
http://qt-project.org/wiki/How_to_Change_the_Background_Color_of_QWidget
# # # Properties
1. 对于 LineEdit,修改 text() 属性 :
Setting this property clears the selection, clears the undo/redo history, moves the cursor to the end of the line and resets the modified property
to false.
The text is not validated when inserted
with setText()。 ????
类似的:insert()
and clear().
### Tutorial 1
Every PyQt4 application must create anapplication object.
The
is the base class of all user interface objects
in PyQt4.
The
default constructor has no parent. So, a widget with no parent is called awindow.
sys.exit(app.exec_())
Finally, we enter the mainloop of the application. The event handling starts from this point.
The mainloop ends if we call the
app 的 exit() ) method or the main widget is destroyed. The
tooltip 是有静态属性的,比如 font 的 pixel ,比如 现实时的延迟。因此,接口应该是 configs。
tool tip 的设置 使用了 rich text formatting.
btn.sizeHint() 使用了 sizeHint() 设置 推荐的窗口大小。
是 parent widget 又如何呢?
signal & slot mechanism : The
sender and the receiver : sender is the push button, the receiver is theapplication object.
the main event loop. It processes and dispatches all events. The
instance.
既然引用了QCoreApplication,那么CoreApplication又是什么时候创建的呢?
created with the
closeEvent是QtGui.QWidget的默认event
handler。close 会引发 QCloseEvent,调用 closeEvent。
注意,reimplement嘛,参数就应该写对。
MessageBox参数:The
first string appears on the titlebar. The second string is the message text displayed by the dialog. The third argument specifies the combination of buttons appearing in the dialog.
centering:居中是对整个widget(没有parent)的居中。
QtGui.QDesktopWidget 提供 Desktop 的信息。
得到 一个 rectangle specifying the geometry of the main window。
And from this resolution, we get thecenter point.
然后,将
qr 的 center 对齐到 center point。再将 widget的 TopLeft 对齐
qr 的 TopLeft。
###
Tutorial 2
The
provides a main application window. This enables to create a classic application skeleton with a statusbar, toolbars and a menubar.
menu
will contain one action . The action is accessible with the Ctrl + Q shortcut.
filemenu,toolbar
都通过 addAction 将之前创建的 QAction 关联到 menu 或者 toolbar对象(toolbar就一个)
### Layout
A top level layout is necessary to ensure that your widgets will resize correctly when its window is resized.
They stay there when we resize
addWidget() (horizon,vertical,grid) 是将 control 进行定位的工具(添加到layout中)。
To create such a layout, we will use one horizontal and one vertical box. To create the neccessary space, we will add a stretch
factor.
The stretch adds a stretchable spacebeforethe
two buttons.push them to the right of the window.这里的 before,指的是 “从左向右”。
对于GridLayout,
这里的addWidget(), 参数1 表示 start grid 的 (row,column), 参数2 表示 end grid的(row,column)。
ref :http://harmattan-dev.nokia.com/docs/library/html/qt4/qgridlayout.html#addWidget-2
#### Tutorial : Signal & Slot
When we call the application's
enters the main loop. The main loop fetches events and sends them to the objects.
注释:不要忘了,application object 和 GUI object 都只是内存对象。当 GUI object 调用 show() 的时候,才将数据拷贝到显存中,显示出来。但是,show() 之后,依旧执行下一段指令。即 QtGui.QApplication.exec_()。这个时候,才进入了 main loop,计算和派发消息。
In the event model, there are three participants:
event source
event object
event target
The event object(Event)
encapsulates the state changes in the event source.
注释:event object 其实就是 消息object。
A slot is called when a signal connected
to it is emitted.
注释:
当然,每个 receiver 还有 event handler
在QT 文档中,称为 event handler 是 virtual,是 Protected Function。
http://qt-project.org/doc/qt-4.7/qwidget.html
1. QWidget(包括派生类)background color 有两种 设置方式:palette 和 style sheet
http://qt-project.org/wiki/How_to_Change_the_Background_Color_of_QWidget
# # # Properties
1. 对于 LineEdit,修改 text() 属性 :
Setting this property clears the selection, clears the undo/redo history, moves the cursor to the end of the line and resets the modified property
to false.
The text is not validated when inserted
with setText()。 ????
类似的:insert()
and clear().
### Tutorial 1
Every PyQt4 application must create anapplication object.
The
QtGui.QWidgetwidget
is the base class of all user interface objects
in PyQt4.
w = QtGui.QWidget()
The
default constructor has no parent. So, a widget with no parent is called awindow.
sys.exit(app.exec_())
Finally, we enter the mainloop of the application. The event handling starts from this point.
The mainloop ends if we call the
exit()(译者:可能指的是
app 的 exit() ) method or the main widget is destroyed. The
sys.exit()method ensures a clean exit.
tooltip 是有静态属性的,比如 font 的 pixel ,比如 现实时的延迟。因此,接口应该是 configs。
tool tip 的设置 使用了 rich text formatting.
btn.sizeHint() 使用了 sizeHint() 设置 推荐的窗口大小。
qbtn = QtGui.QPushButton('Quit', self)
是 parent widget 又如何呢?
signal & slot mechanism : The
sender and the receiver : sender is the push button, the receiver is theapplication object.
qbtn.clicked.connect(QtCore.QCoreApplication.instance().quit)
QtCore.QCoreApplicationcontains
the main event loop. It processes and dispatches all events. The
instance()method gives us itscurrent
instance.
既然引用了QCoreApplication,那么CoreApplication又是什么时候创建的呢?
QtCore.QCoreApplicationis
created with the
QtGui.QApplication.
closeEvent是QtGui.QWidget的默认event
handler。close 会引发 QCloseEvent,调用 closeEvent。
注意,reimplement嘛,参数就应该写对。
MessageBox参数:The
first string appears on the titlebar. The second string is the message text displayed by the dialog. The third argument specifies the combination of buttons appearing in the dialog.
centering:居中是对整个widget(没有parent)的居中。
QtGui.QDesktopWidget 提供 Desktop 的信息。
qr = self.frameGeometry()
得到 一个 rectangle specifying the geometry of the main window。
cp = QtGui.QDesktopWidget().availableGeometry().center()
And from this resolution, we get thecenter point.
然后,将
qr 的 center 对齐到 center point。再将 widget的 TopLeft 对齐
qr 的 TopLeft。
###
Tutorial 2
The
QtGui.QMainWindowclass
provides a main application window. This enables to create a classic application skeleton with a statusbar, toolbars and a menubar.
menu
will contain one action . The action is accessible with the Ctrl + Q shortcut.
filemenu,toolbar
都通过 addAction 将之前创建的 QAction 关联到 menu 或者 toolbar对象(toolbar就一个)
### Layout
A top level layout is necessary to ensure that your widgets will resize correctly when its window is resized.
They stay there when we resize
addWidget() (horizon,vertical,grid) 是将 control 进行定位的工具(添加到layout中)。
To create such a layout, we will use one horizontal and one vertical box. To create the neccessary space, we will add a stretch
factor.
The stretch adds a stretchable spacebeforethe
two buttons.push them to the right of the window.这里的 before,指的是 “从左向右”。
对于GridLayout,
names = ['Cls', 'Bck', '', 'Close', '7', '8', '9', '/', '4', '5', '6', '*', '1', '2', '3', '-', '0', '.', '=', '+']
pos = [(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3), (3, 0), (3, 1), (3, 2), (3, 3 ), (4, 0), (4, 1), (4, 2), (4, 3)]
grid = QtGui.QGridLayout()
for btn_nm in names: button = QtGui.QPushButton(btn_nm) grid.addWidget(button, pos[j][0], pos[j][1]) j = j + 1
这里的addWidget(), 参数1 表示 start grid 的 (row,column), 参数2 表示 end grid的(row,column)。
ref :http://harmattan-dev.nokia.com/docs/library/html/qt4/qgridlayout.html#addWidget-2
#### Tutorial : Signal & Slot
When we call the application's
exec_()method, the application
enters the main loop. The main loop fetches events and sends them to the objects.
注释:不要忘了,application object 和 GUI object 都只是内存对象。当 GUI object 调用 show() 的时候,才将数据拷贝到显存中,显示出来。但是,show() 之后,依旧执行下一段指令。即 QtGui.QApplication.exec_()。这个时候,才进入了 main loop,计算和派发消息。
In the event model, there are three participants:
event source
event object
event target
The event object(Event)
encapsulates the state changes in the event source.
注释:event object 其实就是 消息object。
A slot is called when a signal connected
to it is emitted.
注释:
当然,每个 receiver 还有 event handler
在QT 文档中,称为 event handler 是 virtual,是 Protected Function。
http://qt-project.org/doc/qt-4.7/qwidget.html
相关文章推荐
- ZetCode PyQt4 tutorial widgets II
- ZetCode PyQt4 tutorial Drag and Drop
- PyQt5-Beginner-tutorial
- PyQt4-(3) Tutorial 1
- PyQt4-(4) Tutorial 2
- PyQt5-Beginner-tutorial-part 2
- ZetCode PyQt4 tutorial First programs
- PyQt4-(6) Tutorial 4
- ZetCode PyQt4 tutorial layout management
- PyQt4-(7) Tutorial 5
- PyQt4-(8) Tutorial 6
- PyQt5-Beginner-tutorial-part 3
- python+PyQT+Eric安装配置(转载)
- pycharm+PyQt5+python最新开发环境配置,踩坑过程详解
- Lua Math Library Tutorial 数学库教程
- pyqt5 第一个人机交互界面———弹出式闹钟
- PyQT 入门(1):程序基础框架
- PyQt5的学习笔记
- Cocos2D-X Tutorial for iOS and Android: Getting Started
- UFLDL Tutorial深度学习基础——学习总结:稀疏自编码器(一)神经网络(neural networks)