PDA

View Full Version : Please! Need help JUST TO START implementing Python code into QT designed forms



OPG_79
31st July 2012, 21:03
Dear users,

I am reading the Summerfield's PyQT book. Was fighting many days to start simple Python code in QT designed forms. I am using QT designer v. 4.8.0, Python v. 3.2.3. The simple program I am trying to make: Window with a pushButton, lineEdit and a textLabel. So what I want to do: when I push the button, the textLabel adopts the text from the lineEdit, that's it!

What I have done:
1. In QT designer drew a Dialog window with the three above metioned elements.
2. Using pyuic.py program converted the ui file to py file
3. created the following Python script:



from PyQt4.QtCore import *
from PyQt4.QtGui import *
import t_transfer # my py file obtained in step 2, shown below
class DoItNow(QDialog, t_transfer.t_transfer): #created my own class DoItNow, where I try to initialize the form. Not sure about this, though :)
def __init__(self, parent=None):
super(DoItNow, self).__init__(parent)
self.setupUi(self)

@pyqtSignature("")
def on_pushButton_clicked(self):
a = lineEdit.text()
label.setText(a)

app = QApplication(sys.argv)
form = Dialog()
form.show()
app.exec_()



My t_transfer.py file



# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 't_transfer.ui'
#
# Created: Wed Aug 1 00:05:17 2012
# by: PyQt4 UI code generator 4.9.1
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s

class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(400, 300)
self.pushButton = QtGui.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(230, 120, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.lineEdit = QtGui.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(50, 50, 113, 18))
self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
self.label = QtGui.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(50, 130, 111, 16))
font = QtGui.QFont()
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName(_fromUtf8("label"))

self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)

def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QtGui.QApplication.translate ("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
self.pushButton.setText(QtGui.QApplication.transla te("Dialog", "Do it!", None, QtGui.QApplication.UnicodeUTF8))
self.label.setText(QtGui.QApplication.translate("Dialog", "TextLabel", None, QtGui.QApplication.UnicodeUTF8))


if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())



I would greatly appreciate your help!

ChrisW67
2nd August 2012, 06:50
Your main program adapted with markers showing where:


from PyQt4.QtCore import *
from PyQt4.QtGui import *

from t_transfer import Ui_Dialog # <<<< Import the class the Designer built

class DoItNow(QDialog):
def __init__(self, parent=None):
super(DoItNow, self).__init__(parent)

self.ui = Ui_Dialog() # <<<< Creates an instance of the class the Designer built
self.ui.setupUi(self) # <<<< Finishes initialising it

@pyqtSignature("")
def on_pushButton_clicked(self):
a = self.ui.lineEdit.text() # <<<< self.ui refers to the ui object just like in C++
self.ui.label.setText(a)

if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
form = DoItNow() #<<<< your class is called DoItNow not Dialog
form.show()
sys.exit(app.exec_())


There are other ways to do the UI incorporation: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/designer.html

OPG_79
2nd August 2012, 17:56
It works! Thanks a lot ChrisW67. Now I can continue :)