I created the QML file with QtCreator to avoid errors
But when I run the python file, it shows a blank window and then immediately says "Python stopped working"
file.py
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load(QUrl('main.qml'))
engine.quit.connect(app.quit)
sys.exit(app.exec_())
main.qml
import QtQuick 2.10
import QtQuick.Window 2.2
Window{
visible: true
width: 500
height: 400
}
It just crashes everytime, but it runs ok in QtCreator with C++
I found the solution, is the cache..!
Yesterday I found a folder in this location:
C:\Users\username\AppData\Local\cache\qtshadercache
The thing is that if you create an app in QtCreator, and run it, then if you try to run QML in Python it will crash unless you remove the files in the qtshadercache folder, once the files are removed you can run QML in Python, but you have to remove the files before running QML in Python, only if have used QtCreator before
I hope this could help someone
Related
I use python version 3.11.1 in a virtual env and used pip install pyside6 to install pyside6
The full error I get is:
QQmlApplicationEngine failed to load component
file:///E:/project_path/gui_pyside6/first_gui/virenv/src/main.qml:1:1: Cannot load library E:\project_path\gui_pyside6\first_gui\virenv\Lib\site-packages\PySide6\qml\QtQuick\qtquick2plugin.dll: The specified module could not be found.
I have checked and qtquick2plugin.dll is in that folder. I even added the path to my environment paths, but nothing seems to solve it. How can I solve this?
This is my main.py
import sys
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.quit.connect(app.quit)
engine.load('main.qml')
sys.exit(app.exec())
This is my main.qml
import QtQuick 2.15
import QtQuick.Controls 2.15
ApplicationWindow {
visible: true
width: 600
height: 500
title: "HelloApp"
Text {
anchors.centerIn: parent
text: "Hello World"
font.pixelSize: 24
}
}
Platform: Windows 10
Issue
I used Qt Design Studio to create some UI components and screens (.ui.qml format), but no matter what I do, whenever I try to run it in PyQt5 I kept encountering the following error:
module "QtQuick.Studio.Components" is not installed
The .ui.qml file has an import
import QtQuick.Studio.Components 1.0
I looked everywhere online and couldn't find much on the topic on how to fix this problem.
Question
What's the best way to overcome this issue? I've been using it on Windows but I am mainly looking to use the code on a raspberry pi, are there extra dependencies that need to be installed?
Python code
import os
from pathlib import Path
import sys
from PyQt5.QtCore import QCoreApplication, Qt, QUrl
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine
if __name__ == '__main__':
CURRENT_DIRECTORY = Path(__file__).resolve().parent
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.addImportPath(os.fspath(CURRENT_DIRECTORY.parents[0]))
url = QUrl.fromLocalFile(os.fspath(CURRENT_DIRECTORY / "Screen01.ui.qml"))
def handle_object_created(obj, obj_url):
if obj is None and url == obj_url:
print("exit")
QCoreApplication.exit(-1)
engine.objectCreated.connect(
handle_object_created, Qt.ConnectionType.QueuedConnection
)
engine.load(url)
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec())
Windows
I used print(engine.importPathList()) to see my import paths. This prompted me to go C:/Users/USER/AppData/Local/Programs/Python/Python38/lib/site-packages/PyQt5/Qt5/qml and noticed that QtQuick exists but does not have a Studio folder.
Since Qt Design Studio can run my code with no import issues, I knew that it would have a Studio folder. Therefore, I went into C:\Program Files\Qt\qtdesignstudio-2.2.0-community\qt5_design_studio_reduced_version\qml\QtQuick and copied the Studio folder from there onto the PyQt5 path above.
This fixed the issue.
Linux
I am yet to try it on Linux.
I'm trying to learn PyQt5 + qml, and everything I tried so far works fine, however I've ran into a snag when trying to import QtMultimedia into my qml file results in the following error:
plugin cannot be loaded for module "QtMultimedia": Cannot load library
D:\py35venvQt\lib\site-packages\PyQt5\Qt\qml\QtMultimedia\declarative_multimedia.dll:
The specified module could not be found.
main.qml:
import QtQuick 2.8
import QtQuick.Window 2.2
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.1
import QtMultimedia 5.6
Window {
id: root
visible: true
title: 'my pyqt app'
width: 1280
height: 720
}
main.py
if __name__ == '__main__':
def handleStatusChange(status):
if status == QQuickView.Error:
errors = appLabel.errors()
if errors:
print (errors[0].description())
myApp = QApplication(sys.argv)
appLabel = QQuickView()
appLabel.statusChanged.connect(handleStatusChange)
model = models.ActorModel(DB_PATH)
ctxt = appLabel.rootContext()
ctxt.setContextProperty('myModel', model)
appLabel.setSource(QUrl('./qml/main/main.qml'))
try:
sys.exit(myApp.exec_())
except:
print("Exiting")
Without the QtMultimedia import everything works fine, also I've tried all Possible version of QtMultimedia (5.0,5.1 etc). In addition the dll does exist in the correct path.
When searching for a solution online I found some post saying that QtMultimedia is not supported in PyQt5, though I think those posts are too old.
I would appreciate it if someone would shed some light on this situation,
Thank you.
Edit: Python Version 3.5.2 PyQt Version 5.8.
PyQt installation process: new virtualenv -> pip install pyqt5
Edit2: Tried reinstalling to a completely new virtual environment using pip3 install pyqt5 still getting the same error.
Edit3: According to http://www.dependencywalker.com the following dlls can not be found in the dir where declarative_multimedia.dll is:
QT5MULTIMEDIA.DLL QT5QUICK.DLL QT5GUI.DLL QT5QML.DLL QT5CORE.DLL
QT5MULTIMEDIAQUICK_P.DLL
With the exception of QT5MULTIMEDIAQUICK_P.DLL they are all present in the \Lib\site-packages\PyQt5\Qt\bin directory
QT5MULTIMEDIAQUICK_P.DLL is not present at all.
I tried copying all the missing files into \Lib\site-packages\PyQt5\Qt\qml\QtMultimedia to see if it would make any difference. It did not.
I also tried to install PyQt 5 into my proper python 3.5 installation (without virtualenv) and running my code. The result is the same.
I had similar issue on Ubuntu and I solved my problem by adding environment variable LD_LIBRARY_PATH=/home/slav/Apps/Qt5.9.2/5.9.2/gcc_64/lib.
"/home/slav/Apps/Qt5.9.2/5.9.2/gcc_64/lib" here I've install Qt with QtInstaller
maybe You should use pyqt not pyside
I setup a virtualenv and installed pyqt5 (PyQt5-5.7-cp35-cp35m-manylinux1_x86_64.whl):
virtualenv -p /usr/bin/python3.5 .
source bin/activate
pip install pyqt5
I created a basic.qml file:
import QtQuick 2.7
import QtQuick.Controls 2.0
Rectangle {
width: 300
height: 100
color: "red"
}
and tried to load it in my python code with:
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQuick import QQuickView
if __name__ == '__main__':
myApp = QApplication(sys.argv)
view = QQuickView()
view.setSource(QUrl('basic.qml'))
view.show()
sys.exit(myApp.exec_())
It fails with
file:///[...]/main.qml:2:1: plugin cannot be loaded for module "QtQuick.Controls": Cannot load library /[virtualenv]/lib/python3.5/site-packages/PyQt5/Qt/qml/QtQuick/Controls.2/libqtquickcontrols2plugin.so: (libQt5QuickTemplates2.so.5: Can't open shared object file: File or directory not found)
import QtQuick.Controls 2.0
^
Process finished with exit code 0
I checked. This file it complains about actually doesn't exist. But how can I install it? Does PyQt5 support QtQuickControls2 at all?
If I switch the import in basic.qml from import QtQuick.Controls 2.0 to import QtQuick.Controls 1.2, it works. But I want to use the new controls.
This looks like a bug in PyQt5. The package is missing both libQt5QuickTemplates2.so and libQt5QuickControls2.so.
Hoping that the Qt 5.7 build contained by the PyQt 5.7 package and the official Qt 5.7 build available at qt.io are built in a fully binary compatible way, one possibility could be to download and install Qt 5.7 from qt.io, and copy the missing libraries into your virtualenv. For example:
$ cp ~/Qt/5.7/gcc_64/lib/libQt5QuickTemplates2.* path/to/lib/python3.5/site-packages/PyQt5/Qt/lib
$ cp ~/Qt/5.7/gcc_64/lib/libQt5QuickControls2.* path/to/lib/python3.5/site-packages/PyQt5/Qt/lib
I am trying to build a very basic executable (Windows) using PySide. The following script runs properly in the interpreter (Python 2.7, PySide 1.1.2)
#!/usr/bin/python
import sys
sys.stdout = open("my_stdout.log", "w")
sys.stderr = open("my_stderr.log", "w")
import PySide.QtGui
from PySide.QtGui import QApplication
from PySide.QtGui import QMessageBox
# Create the application object
app = QApplication(sys.argv)
# Create a simple dialog box
msgBox = QMessageBox()
msgBox.setText("Hello World - using PySide version " + PySide.__version__)
msgBox.exec_()
I tried 3 methods (py2exe, pyinstaller and cx_freeze) and all the 3 generated executables fail to execute. The two stdout/stderr files appear, so I found the first PySide import is making everything fail. (Unhandled exception/Access violation)
I analyzed the executable file with depends (http://www.dependencywalker.com/) and everything looks correctly linked.
Any idea?
You need to add the atexit module as an include. source: http://qt-project.org/wiki/Packaging_PySide_applications_on_Windows
(is also the case for Linux btw)
Thank you for your help. Actually, this did not change anything :/ However, I found a solution to my problem: if I add from PySide import QtCore, QtGui, then the executable (with pyinstaller) does work!