Results 1 to 17 of 17

Thread: Import uic or use pyuic4?

  1. #1
    Join Date
    Feb 2015
    Posts
    27
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Import uic or use pyuic4?

    I am a newbie to PyQt, but have created and published a simple app. I would like to make a few improvements to it.

    I have been wondering what the pros and cons are for choosing between importing uic to load the GUI.ui or using pyuic4 to convert the GUI.ui into a .py file.
    The first method is simpler for testing, but the second creates a .pyc file after the first run, so I imagine that it loads the GUi and app quicker. It's difficult to do a comparison as other factors are involved. Speeding up the loading would be helpful. have tried to assess the advantages of method myself but have no experience to base this on. A web search hasn't helped as it doesn't seem to be discussed.

    What are the advantages and disadvantages to each method - I'd welcome opinions as well as facts.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Import uic or use pyuic4?

    What do you mean with "importing uic"?
    How do you load the UI in this case?
    QUiLoader?

    Cheers,
    _

  3. #3
    Join Date
    Feb 2015
    Posts
    27
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Import uic or use pyuic4?

    anda_skoa: I don't know the correct terminology, but I create a GUI called test.ui with Qt Designer which has a form called Ui_Form, then I could either:

    1) Load the .ui file directly into the Python program which has:
    Qt Code:
    1. from PyQt4 import QtCore, QtGui, uic
    2. form_class = uic.loadUiType(test.ui)[0]
    To copy to clipboard, switch view to plain text mode 

    OR

    2) Convert the test.ui into a Python script with:
    Qt Code:
    1. pyuic4 test.ui -o test_ui.py
    To copy to clipboard, switch view to plain text mode 

    Load test_ui.py into the Python program which has:
    Qt Code:
    1. from PyQt4 import QtCore, QtGui
    2. from test_ui import Ui_Form
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Import uic or use pyuic4?

    The second case is the one usually used in C++.

    I was trying to understand if the first case resembles usage of QUiLoader or if it was something PyQt offered as an additional thing.
    Seems to be the latter.

    The two C++ options differ a lot. Not only is the generated code obviously faster (no XML parsing at runtime), it is also safer to program (you get compiler errors when trying to access something that doesn't exist or is of the wrong type).

    The lack of a build stage with Python obviously doesn't make the second thing a factor.

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    ChrisOfBristol (27th March 2015)

  6. #5
    Join Date
    Feb 2015
    Posts
    27
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Import uic or use pyuic4?

    andy_skoa: Thanks, that makes sense to me. If there is a speed advantage it might be worth me using it. I must admit that I was hoping you would say there was no real advantage, because despite a lot of research and experiment, I have been struggling to work out how to change my original program to make it work. I cribbed the basics from this excellent tutorial. to get:
    Qt Code:
    1. # Temperature-conversion program using PyQt
    2. import sys
    3. from PyQt4 import QtCore, QtGui, uic
    4.  
    5. form_class = uic.loadUiType("tempconv.ui")[0] # Load the UI
    6.  
    7. class MyWindowClass(QtGui.QMainWindow, form_class):
    8. def __init__(self, parent=None):
    9. QtGui.QMainWindow.__init__(self, parent)
    10. self.setupUi(self)
    11. # Bind the event handlers to the buttons
    12. blah-blah
    13. # button event handlers
    14. blah-blah
    15.  
    16. app = QtGui.QApplication(sys.argv)
    17. myWindow = MyWindowClass(None)
    18. myWindow.show()
    19. app.exec_()
    To copy to clipboard, switch view to plain text mode 
    but I don't understand object oriented programming etc well enough to know what to change apart from needing something like:
    Qt Code:
    1. from PyQt4 import QtCore, QtGui
    2. from test_ui import Ui_Form
    To copy to clipboard, switch view to plain text mode 

    This documentation looks helpful, but once it starts talking about inheritance and classes I've had it!

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Import uic or use pyuic4?

    I don't know enought Python to be sure but it looks like your class is currently inheriting from QMainWindow and form_class.
    In which case you should be able simply replace form_class in line 7 with Ui_Form

    Cheers,
    _

  8. #7
    Join Date
    Feb 2015
    Posts
    27
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Import uic or use pyuic4?

    Unfortunately it doesn't quite work, there is just a cross on the screen a bit like this ╬. If I've got time over the weekend I'll edit out all the irrelevant stuff from the Python script created from the gui and the main Python code, and post it here, then perhaps a Python expert can point out what I have got wrong.
    Last edited by ChrisOfBristol; 28th March 2015 at 00:03.

  9. #8
    Join Date
    Feb 2015
    Posts
    27
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Import uic or use pyuic4?

    This works:
    Qt Code:
    1. #! /usr/bin/python
    2.  
    3. import sys
    4. from PyQt4 import QtCore, QtGui
    5.  
    6. from pyuic_ui import Ui_MainWindow #The name of my top level object is MainWindow
    7.  
    8. class MyForm(QtGui.QMainWindow): #The name of my top level object is MainWindow
    9. def __init__(self, parent=None):
    10. QtGui.QWidget.__init__(self, parent)
    11. self.ui = Ui_MainWindow() #The name of my top level object is MainWindow
    12. self.ui.setupUi(self)
    13.  
    14. if __name__ == "__main__":
    15. app = QtGui.QApplication(sys.argv)
    16. myapp = MyForm()
    17. myapp.show()
    18. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

  10. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Import uic or use pyuic4?

    That is also how one would do in in C++, i.e. have the generated UI class as a member of the widget class.

    Cheers,
    _

  11. #10
    Join Date
    Feb 2015
    Posts
    27
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Import uic or use pyuic4?

    I spoke too soon! I have added a button to the script, but it causes an error even though the names are correct, as you can see from the script below and the attached files.
    Qt Code:
    1. #! /usr/bin/python
    2.  
    3. import sys
    4. from PyQt4 import QtCore, QtGui
    5. from pyuic_ui import Ui_MainWindow
    6.  
    7. class MyWindowClass(QtGui.QMainWindow):
    8. def __init__(self, parent=None):
    9. QtGui.QWidget.__init__(self, parent)
    10. self.ui = Ui_MainWindow()
    11. self.ui.setupUi(self)
    12.  
    13. self.buttonProcess.triggered.connect(self.buttonProcessTriggered)
    14.  
    15. def buttonProcessTriggered(self):
    16. print 'OK'
    17.  
    18. if __name__ == "__main__":
    19. app = QtGui.QApplication(sys.argv)
    20. mywindow = MyWindowClass()
    21. mywindow.show()
    22. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. chris@Asus:~/Documents/Linux/Debreate/PyQt$ ./pyuic.py
    2. Traceback (most recent call last):
    3. File "./pyuic.py", line 23, in <module>
    4. mywindow = MyWindowClass()
    5. File "./pyuic.py", line 13, in __init__
    6. buttonProcess.triggered.connect(self.buttonProcessTriggered)
    7. NameError: global name 'buttonProcess' is not defined
    To copy to clipboard, switch view to plain text mode 

    There is also a strange anomaly with Qt, in that stuff I have deleted from the GUI with Qt Creator doesn't always get deleted from the .ui file. You can see from the attached files that there is only a button on the gui, but unwanted lines remain in the .ui file.
    Attached Images Attached Images
    Attached Files Attached Files

  12. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Import uic or use pyuic4?

    The widgets created by Ui_MainWindow are members of its instance, i.e. members of the object you have as "ui"
    Qt Code:
    1. self.ui.buttonProcess.triggered.connect(self.buttonProcessTriggered)
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  13. The following user says thank you to anda_skoa for this useful post:

    ChrisOfBristol (30th March 2015)

  14. #12
    Join Date
    Feb 2015
    Posts
    27
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Import uic or use pyuic4?

    It doesn't work, so I tried the alternatives between the hashes, none of those work.

    Qt Code:
    1. #! /usr/bin/python
    2.  
    3. import sys
    4. from PyQt4 import QtCore, QtGui
    5. from pyuic_ui import Ui_MainWindow
    6.  
    7. class MyWindowClass(QtGui.QMainWindow):
    8. def __init__(self, parent=None):
    9. QtGui.QWidget.__init__(self, parent)
    10. self.ui = Ui_MainWindow()
    11. self.ui.setupUi(self)
    12. #
    13. buttonProcess.triggered.connect(self.buttonProcessTriggered)
    14. ui.buttonProcess.triggered.connect(self.buttonProcessTriggered)
    15. self.buttonProcess.triggered.connect(self.buttonProcessTriggered)
    16. self.ui.buttonProcess.triggered.connect(self.buttonProcessTriggered)
    17. #
    18. def buttonProcessTriggered(self):
    19. print 'OK'
    20.  
    21. if __name__ == "__main__":
    22. app = QtGui.QApplication(sys.argv)
    23. mywindow = MyWindowClass()
    24. mywindow.show()
    25. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

  15. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Import uic or use pyuic4?

    I assume you are using the correct name for the button?
    I.e. in your .ui it is called pushButton not buttonProcess, so either the code snippet does not match your .ui file or you have a wrong name in the connect

    Cheers,
    _

  16. The following user says thank you to anda_skoa for this useful post:

    ChrisOfBristol (30th March 2015)

  17. #14
    Join Date
    Feb 2015
    Posts
    27
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Import uic or use pyuic4?

    I checked all that and somehow missed it! Having corrected it to this
    Qt Code:
    1. self.ui.pushButton.triggered.connect(self.pushButtonTriggered)
    To copy to clipboard, switch view to plain text mode 
    gives this
    Qt Code:
    1. chris@Asus:~/Documents/Linux/Debreate/PyQt$ ./pyuic.py
    2. Traceback (most recent call last):
    3. File "./pyuic.py", line 23, in <module>
    4. mywindow = MyWindowClass()
    5. File "./pyuic.py", line 16, in __init__
    6. self.ui.pushButton.triggered.connect(self.pushButtonTriggered)
    7. AttributeError: 'QPushButton' object has no attribute 'triggered'
    To copy to clipboard, switch view to plain text mode 
    though.

  18. #15
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Import uic or use pyuic4?

    Right, the signal is called clicked().

    QAction's have a triggered() signal.

    Cheers,
    _

  19. The following user says thank you to anda_skoa for this useful post:

    ChrisOfBristol (30th March 2015)

  20. #16
    Join Date
    Feb 2015
    Posts
    27
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Import uic or use pyuic4?

    It seems odd to me that the ui file and the ui.py file both have 'triggered', but in the Python file it has to be 'clicked'.

    It's working now, and I have changed this in the app, and on a couple of other lines and the app works too! A quick couple of tries suggests that it loads much more quickly, which was what I wanted to achieve.

    No doubt I would have been able to tackle this better if I was prepared to learn about object orientation/inheritance/classes, but I only started this app by accident really (to get something done), i had to learn Python and Qt from scratch and didn't want to have to learn anything I could possibly manage without.

    Thanks for your extremely patient answers to my newbie questions.

  21. #17
    Join Date
    Feb 2015
    Posts
    27
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Import uic or use pyuic4?

    I've now switched to Python3 and PyQt5, QtWidgets are separate in PyQt5 so a few minor changes are needed to make this script work:
    Qt Code:
    1. #! /usr/bin/python3
    2.  
    3. import sys
    4. from PyQt5 import QtCore, QtGui, QtWidgets #
    5. from image3d_ui import Ui_MainWindow
    6.  
    7. class MyWindowClass(QtWidgets.QMainWindow): #
    8. def __init__(self, parent=None):
    9. QtWidgets.QMainWindow.__init__(self, parent) #
    10. self.ui = Ui_MainWindow()
    11. self.ui.setupUi(self)
    12.  
    13. self.ui.pushButton.clicked.connect(self.pushButtonClicked)
    14.  
    15. def pushButtonClicked(self):
    16. print ('OK')
    17.  
    18. if __name__ == "__main__":
    19. app = QtWidgets.QApplication(sys.argv) #
    20. mywindow = MyWindowClass()
    21. mywindow.show()
    22. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 
    Last edited by ChrisOfBristol; 4th April 2015 at 13:10.

Similar Threads

  1. QML's import does not work
    By MarkoSan in forum Qt Programming
    Replies: 9
    Last Post: 8th September 2014, 14:33
  2. [RESOLVED] PyQt4: pyuic4.bat throws exception
    By rherzog in forum Qt Programming
    Replies: 0
    Last Post: 4th June 2014, 12:18
  3. Import QtQuick 2.0 or 2.1 or 2.2?
    By pherthyl in forum Qt Quick
    Replies: 2
    Last Post: 9th January 2014, 21:13
  4. ImportError: Cannot import name
    By wanmonk in forum Qt Programming
    Replies: 4
    Last Post: 16th December 2013, 06:32
  5. Qt 4.5 import stylesheet
    By dcoppenb in forum Qt Programming
    Replies: 0
    Last Post: 7th March 2013, 13:47

Tags for this Thread

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.