Results 1 to 4 of 4

Thread: Complete newbie hello world question!

  1. #1
    Join Date
    Jan 2013
    Posts
    2
    Qt products
    Platforms
    Unix/X11

    Smile Complete newbie hello world question!

    I am using PyQT and am trying to set up a really simple program. All I want is a simple button that when clicked changes a label text to hello world.. Sorry if this is the wrong place to ask this question.. I have designed the UI in QT 4 Designer and am editing the code in Eric.

    The UI code is:

    Qt Code:
    1. # -*- coding: utf-8 -*-
    2.  
    3. # Form implementation generated from reading ui file 'test.ui'
    4. #
    5. # Created: Sat Jan 26 21:56:01 2013
    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_Form(object):
    18. def setupUi(self, Form):
    19. Form.setObjectName(_fromUtf8("Form"))
    20. Form.resize(247, 177)
    21. self.label = QtGui.QLabel(Form)
    22. self.label.setGeometry(QtCore.QRect(60, 40, 131, 20))
    23. self.label.setObjectName(_fromUtf8("label"))
    24. self.button = QtGui.QPushButton(Form)
    25. self.button.setGeometry(QtCore.QRect(70, 100, 96, 27))
    26. self.button.setObjectName(_fromUtf8("button"))
    27.  
    28. self.retranslateUi(Form)
    29. QtCore.QMetaObject.connectSlotsByName(Form)
    30.  
    31. def retranslateUi(self, Form):
    32. Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
    33. self.label.setText(QtGui.QApplication.translate("Form", "Hmmm...", None, QtGui.QApplication.UnicodeUTF8))
    34. self.button.setText(QtGui.QApplication.translate("Form", "Press Me!", None, QtGui.QApplication.UnicodeUTF8))
    To copy to clipboard, switch view to plain text mode 

    and the main app code is:

    Qt Code:
    1. import sys
    2. from PyQt4 import QtCore, QtGui
    3.  
    4. import ui_test
    5.  
    6. def clicked():
    7. designed.label.setText("Hello World!")
    8.  
    9. def main():
    10. app = QtGui.QApplication(sys.argv)
    11. designed = ui_test.Ui_Form()
    12. widget = QtGui.QWidget()
    13. designed.setupUi(widget)
    14. designed.button.clicked.connect(clicked)
    15. widget.show()
    16. sys.exit(app.exec_())
    17.  
    18. if __name__ == '__main__':
    19. main()
    To copy to clipboard, switch view to plain text mode 


    But when I run the program and click the button I get the error message:

    The debugged program raised the exception unhandled NameError
    "global name 'designed' is not defined"
    File: /home/peter/Documents/qt/test/app.py, Line: 7
    Break here?

    So I guess its this line that is causing the problem..

    Qt Code:
    1. designed.label.setText("Hello World!")
    To copy to clipboard, switch view to plain text mode 

    Can anyone help please

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Complete newbie hello world question!

    Your "clicked" function doesn't know anything about a "designed" object created in another function. This is a strictly python issue and nothing to do with Qt.


    Added after 7 minutes:


    One possibility to work around it is to define a function in Ui_Form and call that as a slot. Note the code below is sort of a hack that uses some features unique to Python. Usually you'd subclass QWidget, in the constructor instantiate Ui_Form and define the slot function in the QWidget subclass and not the Ui_Form class itself.

    python Code:
    1. def fun_myClickHandler(self):
    2. self.label.setText("Hello World!")
    3.  
    4. ui_test.Ui_Form.myClickHandler = fun_myClickHandler
    5.  
    6. designed = ui_test.Ui_Form()
    7. widget = QtGui.QWidget()
    8. designed.setupUi(widget)
    9. designed.button.clicked.connect(designed.myClickHandler)
    10. # and so on...
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 27th January 2013 at 02:56.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2013
    Posts
    2
    Qt products
    Platforms
    Unix/X11

    Default Re: Complete newbie hello world question!

    Thank you, got this working now with the following code:

    Qt Code:
    1. import sys
    2. from PyQt4 import QtCore, QtGui
    3.  
    4. import ui_test
    5.  
    6. def fun_myClickHandler(self):
    7. self.label.setText("Hello World!")
    8.  
    9. def main():
    10. ui_test.Ui_Form.myClickHandler = fun_myClickHandler
    11. app = QtGui.QApplication(sys.argv)
    12. designed = ui_test.Ui_Form()
    13. widget = QtGui.QWidget()
    14. designed.setupUi(widget)
    15. designed.button.clicked.connect(designed.myClickHandler)
    16. widget.show()
    17. sys.exit(app.exec_())
    18.  
    19. if __name__ == '__main__':
    20. main()
    21. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    Although I don't really understand it, I think I need to learn Python :-).. I have been aproaching this from a perspective of wishing to learn to code GUI software and to find an equivalent of visual basic under Linux as its all I have really done before..

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Complete newbie hello world question!

    Hard to do that without learning a programming language first.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Newbie question about QToolBar
    By AlbertoN in forum Newbie
    Replies: 1
    Last Post: 28th May 2012, 06:38
  2. Complete newbie with programming and Qt
    By Jacob in forum Newbie
    Replies: 1
    Last Post: 6th March 2010, 06:26
  3. Newbie Designer question
    By JariV in forum Qt Tools
    Replies: 0
    Last Post: 15th February 2009, 22:24
  4. Newbie threading question
    By deepayan in forum Qt Programming
    Replies: 17
    Last Post: 16th April 2007, 00:25
  5. simple hello world question
    By chap19150 in forum Qt Programming
    Replies: 12
    Last Post: 11th July 2006, 19:42

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
  •  
Qt is a trademark of The Qt Company.