Хотите сделать посильный вклад в развитие Ubuntu и русскоязычного сообщества? Помогите нам с документацией!
0 Пользователей и 1 Гость просматривают эту тему.
import sysfrom PyQt4 import QtGui, uicapp = QtGui.QApplication(sys.argv)widget = uic.loadUi("my.ui")widget.show()app.exec_()
# coding: utf-8from PyQt4 import QtCore, QtGui, uicimport sysclass Main(QtGui.QMainWindow): def __init__(self, parent=None): QtGui.QMainWindow.__init__(self, parent) uic.loadUi("my.ui", self) self.the_text_edit.setReadOnly(True) self.the_text_edit.setText(u"Текст по умолчанию.") QtCore.QObject.connect(self.button_0, QtCore.SIGNAL("clicked()"), self.signal_0) QtCore.QObject.connect(self.button_1, QtCore.SIGNAL("clicked()"), self.signal_1) QtCore.QObject.connect(self.actionExit, QtCore.SIGNAL("activated()"), self.signal_1) def signal_0(self): self.the_text_edit.insertHtml(u" Гы") def signal_1(self): sys.exit()App = QtGui.QApplication(sys.argv)Prim = Main()Prim.show()sys.exit(App.exec_())
<?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>284</width> <height>270</height> </rect> </property> <property name="windowTitle"> <string>Это Пример</string> </property> <widget class="QWidget" name="MainWidget"> <widget class="QPushButton" name="button_0"> <property name="geometry"> <rect> <x>10</x> <y>130</y> <width>81</width> <height>71</height> </rect> </property> <property name="text"> <string>Кнопка</string> </property> </widget> <widget class="QPushButton" name="button_1"> <property name="geometry"> <rect> <x>200</x> <y>170</y> <width>71</width> <height>61</height> </rect> </property> <property name="text"> <string>Выйти</string> </property> </widget> <widget class="QTextEdit" name="the_text_edit"> <property name="geometry"> <rect> <x>30</x> <y>10</y> <width>181</width> <height>101</height> </rect> </property> </widget> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>284</width> <height>25</height> </rect> </property> <widget class="QMenu" name="menuFile"> <property name="title"> <string>File</string> </property> <addaction name="actionExit"/> </widget> <addaction name="menuFile"/> </widget> <action name="actionExit"> <property name="text"> <string>Exit</string> </property> </action> </widget> <resources/> <connections/></ui>
QtCore.QObject.connect(self.button_0, QtCore.SIGNAL("clicked()"), self.signal_0)
self.button_0.clicked.connect(self.signal_0)
вместо:Код: [Выделить]QtCore.QObject.connect(self.button_0, QtCore.SIGNAL("clicked()"), self.signal_0)так проще:Код: [Выделить]self.button_0.clicked.connect(self.signal_0)
blindvic, чем проще-то? меньше букв, что-ли?
This section describes the older style for connecting signals and slots. It uses the same API that a C++ application would use. This has a number of advantages. It is well understood and documented. Any future changes to the C++ API should be easily included.It also has a number of disadvantages. It requires knowledge of the C++ types of signal arguments. It is error prone in that if you mis-type the signal name or signature then no exception is raised, either when the signal is connected or emitted. It is verbose. It is not Pythonic.This older style of connecting signals and slots will continue to be supported throughout the life of PyQt v4.
An interesting variation you can use when defining Python slots on the flyis to use the signal as a decorator:@button.clicked.connectdef clicked(): print "Button clicked"
@button.clicked.connectdef clicked(): print "Button clicked"
Но работает только с версии 4.7, так ?
...new style of connecting signals and slots introduced in PyQt v4.5.
Страница сгенерирована за 0.064 секунд. Запросов: 21.