python version 3.3.2
ide: PyDev Eclipse with PyQt installed
I designed a gui in Qt Designer, saved the .ui file and convert it into python code with this command:
pyuic4 -x DeA.ui -o DeA.py
Qt Designer xml output:
<?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>485</width>
<height>374</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="btn_add">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>121</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>Add item</string>
</property>
</widget>
<widget class="QListView" name="lst">
<property name="geometry">
<rect>
<x>10</x>
<y>60</y>
<width>461</width>
<height>301</height>
</rect>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>
pyuic4 final python gui output:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'DeA.ui'
#
# Created: Tue Jun 4 03:30:45 2013
# by: PyQt4 UI code generator 4.10.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(485, 374)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.btn_add = QtGui.QPushButton(self.centralwidget)
self.btn_add.setGeometry(QtCore.QRect(10, 10, 121, 41))
self.btn_add.setObjectName(_fromUtf8("btn_add"))
self.lst = QtGui.QListView(self.centralwidget)
self.lst.setGeometry(QtCore.QRect(10, 60, 461, 301))
self.lst.setObjectName(_fromUtf8("lst"))
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.btn_add.setText(_translate("MainWindow", "Add item", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
then I copy and paste the content of newly created python file(“DeA.py”) into my python file in PyDev Eclipse and hit Run, and gui shows up just fine;
my questions:
where exactly in PyDev Eclipse script pane (not using slots in Qt Designer) can I add my own python functions so that when I hit the “Add item” button, it adds 100 items into the ListView?
I found this question: Linking a qtDesigner .ui file to python/pyqt? about linking the .ui file into the final project and build gui at run time; but I couldn’t understand it; Please help; what exactly should I add into my PyDev Eclipse script pane to link the .ui file and build it at runtime? (suppose the .ui file path is: D:\DeA.ui)
you do not have to actually convert the .ui file to .py to use it, you can do something like this..
lest say that my .ui file is
"C:/mydesign.ui"
from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4 import uic
import sys
FORM_1, BASE_1 = uic.loadUiType(r"C:/mydesign.ui")
APP = QtGui.QApplication(sys.argv)
class MyApp(FORM_1, BASE_1):
def __init__(self, parent=None):
super(MyApp, self).__init__(parent)
self.setupUi(self)
self.connect(self.btn_add, QtCore.SIGNAL("released()"), self.do_something)
def do_something(self):
print "working"
FORM = MyApp()
FORM.show()
APP.exec_()