Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
What is the correct setup for developing GUI with PyQt and QML?
dhb285 last edited by
I am looking for advice on developing PyQt QML applications. More specifically, I would like to know what setup and working mode ensures the smoothest progress.
I have been trying to write a GUI using the following:
In Eclipse IDE with PyDev, I create a new PyDev project. My main .py file launches a QML application:
app = QApplication(sys.argv) # Create a label and set its properties engine = QQmlApplicationEngine() engine.load(QUrl('QmlBackbone.qml')) win = engine.rootObjects() win.show() sys.exit(app.exec_())
The QML application is defined in a .qml file, which I create by right-clicking my PyDev project and clicking on 'add new file'. I name this file 'QmlBackbone.qml'. When the file is created, QtCreator automatically loads up, since I have given the file a .qml extension. There I can modify the QML structure.
I launch the application from Eclipse, by running main.py. This works if there is no error in my .qml files. If there is any error, the application exits at win = engine.rootObjects() (because, most probably, engine.load(QUrl('QmlBackbone.qml')) fails, so engine has not root objects), without logging any errors in QtCreator. I am left trying to guess what causes the error, which drastically amplifies the debug time.
Another disadvantage is that I do not see a Project structure in QtCreator. Thus, I can only access .qml files from the 'Open Documents' box, provided I opened them before from Eclipse.
In other words, QtCreator just works as a QML code editor - nothing else.
ImportError: No module named PyQt5.QtCore
I feel like my approach is completely off-track and unproductive.
How should I plan to develop a GUI using PyQt? Which tools should I use? What is the default recommended workflow? Should I create a project within QtCreator? Creating a project from QtCreator assumes I am using C++, but I am using Python.
From the looks of it, you don't have PyQt5 in your python path when using the console. Your IDE likely takes care of that for you.
@dhb285 Do you use Python only to load QML and the rest of your application is just that QML? If so, why using Python at all?
patrickkidd last edited by
I am also wondering whether it is possible and makes sense to mix pyqt and QML. For example, I want to develop a custom diagraming tool with special symbols and rules, and don't want to plugins in C++. Maybe that's what he is after...
@patrickkidd Well, if the whole application is QML and Python is only used to load that QML then I don't see the need for Python. Just use C++ : it will be just one main.cpp loading the QML :-)
I know that you can connect C++ and QML but I don't think there is a direct connection between Python and QML. You most probably will need to write some C++ code to connect you Python code and QML, but then you can just use C++.
patrickkidd last edited by
@patrickkidd If customization means UI in this context then QML is for sure more flexible and customizable than C++/Widgets.
TheBadger last edited by
Maybe it is coincidence but I just started on a personal project that also requires this type of information. At this point I am coding everything, the Python and QML code by hand in notepadd++ but was wondering about all of this.
My reasoning for integrating PyQt and QML: I have an external Python library that I want to create a GUI for so decided to use QML. I have basically no Python experience, and only a bit of QML . I am quite comfortable with Qt C++ but could not find a easy way to make the Python library available to Qt C++ (without boost or something). So I see this as a good challenge and learning opportunity since I also have another QML project I want to start.
I am using the following links as references, it seems like mixing Python and QML is already well supported using PyQt:
- Integrating Python and QML
- Beginners PyQt5 and QML integration tutorial! Check out the link in the comments to the guy Patrick's github page, he has a lot of advanced apps that fully integrate PyQt with QML. His sodoku app for example has the whole game engine in Python but the user interface in QML, he uses PyQt to communicate between the two.
In conclusion, I also want to know what tools can be recommended and some workflow guidance.