PyQT tutorial

来源:互联网 发布:手机代理软件免费 编辑:程序博客网 时间:2024/05/21 12:41

### 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 QtGui.QWidget widget 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.QCoreApplication contains the main event loop. It processes and dispatches all events. The instance() method gives us itscurrent instance.

既然引用了QCoreApplication,那么CoreApplication又是什么时候创建的呢?

QtCore.QCoreApplication is 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.QMainWindow class 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


0 0
原创粉丝点击