Results 1 to 3 of 3

Thread: Please! Need help JUST TO START implementing Python code into QT designed forms

  1. #1
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    1
    Qt products
    Platforms
    Windows

    Default Please! Need help JUST TO START implementing Python code into QT designed forms

    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:

    Qt Code:
    1. from PyQt4.QtCore import *
    2. from PyQt4.QtGui import *
    3. import t_transfer # my py file obtained in step 2, shown below
    4. class DoItNow(QDialog, t_transfer.t_transfer): #created my own class DoItNow, where I try to initialize the form. Not sure about this, though :)
    5. def __init__(self, parent=None):
    6. super(DoItNow, self).__init__(parent)
    7. self.setupUi(self)
    8.  
    9. @pyqtSignature("")
    10. def on_pushButton_clicked(self):
    11. a = lineEdit.text()
    12. label.setText(a)
    13.  
    14. app = QApplication(sys.argv)
    15. form = Dialog()
    16. form.show()
    17. app.exec_()
    To copy to clipboard, switch view to plain text mode 

    My t_transfer.py file

    Qt Code:
    1. # -*- coding: utf-8 -*-
    2.  
    3. # Form implementation generated from reading ui file 't_transfer.ui'
    4. #
    5. # Created: Wed Aug 1 00:05:17 2012
    6. # by: PyQt4 UI code generator 4.9.1
    7. #
    8. # WARNING! All changes made in this file will be lost!
    9.  
    10. from PyQt4 import QtCore, QtGui
    11.  
    12. try:
    13. _fromUtf8 = QtCore.QString.fromUtf8
    14. except AttributeError:
    15. _fromUtf8 = lambda s: s
    16.  
    17. class Ui_Dialog(object):
    18. def setupUi(self, Dialog):
    19. Dialog.setObjectName(_fromUtf8("Dialog"))
    20. Dialog.resize(400, 300)
    21. self.pushButton = QtGui.QPushButton(Dialog)
    22. self.pushButton.setGeometry(QtCore.QRect(230, 120, 75, 23))
    23. self.pushButton.setObjectName(_fromUtf8("pushButton"))
    24. self.lineEdit = QtGui.QLineEdit(Dialog)
    25. self.lineEdit.setGeometry(QtCore.QRect(50, 50, 113, 18))
    26. self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
    27. self.label = QtGui.QLabel(Dialog)
    28. self.label.setGeometry(QtCore.QRect(50, 130, 111, 16))
    29. font = QtGui.QFont()
    30. font.setPointSize(14)
    31. font.setBold(True)
    32. font.setWeight(75)
    33. self.label.setFont(font)
    34. self.label.setObjectName(_fromUtf8("label"))
    35.  
    36. self.retranslateUi(Dialog)
    37. QtCore.QMetaObject.connectSlotsByName(Dialog)
    38.  
    39. def retranslateUi(self, Dialog):
    40. Dialog.setWindowTitle(QtGui.QApplication.translate("Dialog", "Dialog", None, QtGui.QApplication.UnicodeUTF8))
    41. self.pushButton.setText(QtGui.QApplication.translate("Dialog", "Do it!", None, QtGui.QApplication.UnicodeUTF8))
    42. self.label.setText(QtGui.QApplication.translate("Dialog", "TextLabel", None, QtGui.QApplication.UnicodeUTF8))
    43.  
    44.  
    45. if __name__ == "__main__":
    46. import sys
    47. app = QtGui.QApplication(sys.argv)
    48. Dialog = QtGui.QDialog()
    49. ui = Ui_Dialog()
    50. ui.setupUi(Dialog)
    51. Dialog.show()
    52. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 


    I would greatly appreciate your help!
    Last edited by OPG_79; 31st July 2012 at 23:05.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Please! Need help JUST TO START implementing Python code into QT designed forms

    Your main program adapted with markers showing where:
    Qt Code:
    1. from PyQt4.QtCore import *
    2. from PyQt4.QtGui import *
    3.  
    4. from t_transfer import Ui_Dialog # <<<< Import the class the Designer built
    5.  
    6. class DoItNow(QDialog):
    7. def __init__(self, parent=None):
    8. super(DoItNow, self).__init__(parent)
    9.  
    10. self.ui = Ui_Dialog() # <<<< Creates an instance of the class the Designer built
    11. self.ui.setupUi(self) # <<<< Finishes initialising it
    12.  
    13. @pyqtSignature("")
    14. def on_pushButton_clicked(self):
    15. a = self.ui.lineEdit.text() # <<<< self.ui refers to the ui object just like in C++
    16. self.ui.label.setText(a)
    17.  
    18. if __name__ == "__main__":
    19. import sys
    20. app = QApplication(sys.argv)
    21. form = DoItNow() #<<<< your class is called DoItNow not Dialog
    22. form.show()
    23. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

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

  3. The following user says thank you to ChrisW67 for this useful post:

    OPG_79 (2nd August 2012)

  4. #3
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    1
    Qt products
    Platforms
    Windows

    Default Re: Please! Need help JUST TO START implementing Python code into QT designed forms

    It works! Thanks a lot ChrisW67. Now I can continue

Similar Threads

  1. convert c++ class to python code
    By alrawab in forum Qt Programming
    Replies: 8
    Last Post: 25th June 2012, 08:43
  2. access the source code of the .ui forms
    By Hamza217 in forum Newbie
    Replies: 3
    Last Post: 4th May 2011, 06:33
  3. execute python in the Qt code
    By gorka_sm in forum Newbie
    Replies: 0
    Last Post: 28th April 2010, 15:04
  4. Embedding python code in Qt
    By lixo1 in forum Qt Programming
    Replies: 2
    Last Post: 12th March 2010, 18:02
  5. Objective C, Python and Ruby code with C++ Qt application
    By Berberis in forum Qt Programming
    Replies: 2
    Last Post: 5th June 2008, 12:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.