我想下个QT designer。可是找不到。。冒是下QT Creator就行
http://qt-project.org/downloads
好吧。先装再说(d:\ide\qtcreator-2.4.1)
不行,得装QT SDK。。啊。1.7G, i am shattered
(补充:后来看到了PyQT这样一个好东西:http://www.riverbankcomputing.com/software/pyqt/download ,只要下载27.6M,就包含了QT, QT designer)
为什么要下。因为在看这篇文章:http://qt-project.org/wiki/PySideSimplicissimus_Module_2_CloseButton
等装好了再说吧。不知要下多久!
==
不必了。QT Designer只不过是生成了一个描述ui的xml文件。http://akabaila.pcug.org.au/pyside-data/quitter.ui
继续看,no blocker.
打开这个ui文件,发现里边定义了一个按钮,一个菜单栏,像是两行两列的布局,绑定了按钮和MainWindow。就当是给button加了ActionListener而MainWindow需要去实现这个Listener。
接下来把这个xml转成py,竟有此等功能#_!
pyside- uic quitter.ui - o ui_quitter.py
不识别的dos命令。把D:\Python27\Scripts加到System path.
好的,和教程写的一模一样
Traceback (most recent call last):
File "D:\Python27\Scripts\pyside-uic-script.py", line 5, in <module>
from pkg_resources import load_entry_point
ImportError: No module named pkg_resources
需要装setuptools:http://pypi.python.org/pypi/setuptools ,找到setuptools-0.6c11.win32-py2.7.exe
自动安装到了D:\Python27\Lib\site-packages\目录下。。。
再次运行
pyside- uic quitter.ui - o ui_quitter.py
咚咚,迫不及待想看看生成的ui_quitter.py是什么样子
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'quitter.ui' # # Created: Thu Apr 12 23:27:33 2012 # by: pyside-uic 0.2.13 running on PySide 1.1.0 # # WARNING! All changes made in this file will be lost! from PySide import QtCore, QtGui class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(526, 277) self.centralwidget = QtGui.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.gridLayout = QtGui.QGridLayout(self.centralwidget) self.gridLayout.setObjectName("gridLayout") spacerItem = QtGui.QSpacerItem(420, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum) self.gridLayout.addItem(spacerItem, 0, 0, 1, 1) self.quitButton = QtGui.QPushButton(self.centralwidget) self.quitButton.setObjectName("quitButton") self.gridLayout.addWidget(self.quitButton, 0, 1, 1, 1) spacerItem1 = QtGui.QSpacerItem(20, 165, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding) self.gridLayout.addItem(spacerItem1, 1, 1, 1, 1) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtGui.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 526, 29)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtGui.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QObject.connect(self.quitButton, QtCore.SIGNAL("clicked()"), MainWindow.close) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8)) self.quitButton.setText(QtGui.QApplication.translate("MainWindow", "&Quit", None, QtGui.QApplication.UnicodeUTF8))
原来pyside-uic是个代码生成工具,根据xml配置生成一个有头有脸的python类。明白了
主程序从自动生成的类继承而来:
#!/usr/bin/env python # quitter.py - provide a button to quit this "program" import sys from PySide.QtGui import QMainWindow, QPushButton, QApplication from ui_quitter import Ui_MainWindow class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) if __name__ == '__main__': app = QApplication(sys.argv) frame = MainWindow() frame.show() app.exec_()
跑一下。
为什么没写任何代码。点button就可以关闭, 文章指出
The reason for its absence is that it has been created on the Qt Designer , which has a signal-slot editor. We used it surreptitiously(偷偷地) to connect PushButton.click() “signal” to MainWindow.close() “slot”. It is imbedded in the quitter.ui file and following the conversion, in the ui_quitter.py file. And from that file, we imported Ui_MainWindow class, which is one of the ancestors of our MainWindow class. We use Python’s multiple inheritance to facilitate use of methods in the ui_quitter.py module.
最后,结束之前让我再看一眼生成的xml代码(quitter.ui)
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>526</width> <height>277</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <layout class="QGridLayout" name="gridLayout"> <item row="0" column="0"> <spacer name="horizontalSpacer"> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>420</width> <height>20</height> </size> </property> </spacer> </item> <item row="0" column="1"> <widget class="QPushButton" name="quitButton"> <property name="text"> <string>&Quit</string> </property> </widget> </item> <item row="1" column="1"> <spacer name="verticalSpacer"> <property name="orientation"> <enum>Qt::Vertical</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>20</width> <height>165</height> </size> </property> </spacer> </item> </layout> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>526</width> <height>29</height> </rect> </property> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections> <connection> <sender>quitButton</sender> <signal>clicked()</signal> <receiver>MainWindow</receiver> <slot>close()</slot> <hints> <hint type="sourcelabel"> <x>479</x> <y>52</y> </hint> <hint type="destinationlabel"> <x>262</x> <y>138</y> </hint> </hints> </connection> </connections> </ui>