QtQuickControls 2.0 with PyQt5 - python

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

Related

PySide6 and QML error: qtquick2plugin.dll: The specified module could not be found

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

PyQt5 Missing QtQuick.Studio Module

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.

Why Python stop working when loads QML file?

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

Can't import QtMultimedia in PyQt 5 on Windows

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

Does PyQt5/pyqt4 already supports QtVirtualKeyboard with Handwriting Recognition?

I'm working on a desktop application using pyqt5, and I want to use a Virtual Keyboard with Handwriting Recognition. I saw that Qt, QtVirtualKeyboard already support it.
Here's a link!
I got the C++ Qt example code running on QtCreator.
But using python3.5 and PyQt5 it gives this message:
module "QtQuick.VirtualKeyboard" is not installed
import QtQuick.VirtualKeyboard 2.1
How should I go on from here?
Does PyQt5 comes with VirtualKeyboard module? if no How to install it on PyQt5?
for qt desinger you can add only this line on your .py file.
os.environ["QT_IM_MODULE"] = "qtvirtualkeyboard"
but If you want use QML with qtvirtualkeyboard;
There is no virtualkeyboard plugin in pyqt5.8, you must be use qt's paths.
For a example, basic steps for pyqt5, qt5.8 and qtvirtualkeyboard installiation on ubuntu:
1.step install qt5.8 with qtvirtualkeyboard
wget
http://download.qt.io/official_releases/qt/5.8/5.8.0/qt-opensource-linux-x64-5.8.0.run
chmod +x qt-opensource-linux-x64-5.8.0.run
./qt-opensource-linux-x64-5.8.0.run
2.step
apt-get install python3 python3-pip
pip3 install pyqt5
3.step
set environmental variables your qt paths on your python code.
import sys, os
os.environ["QT_DIR"] = "/opt/Qt5.8.0/5.8/gcc_64"
os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = "/opt/Qt5.8.0/5.8/gcc_64/plugins/platforms"
os.environ["QT_PLUGIN_PATH"] = "/opt/Qt5.8.0/5.8/gcc_64/plugins"
os.environ["QML_IMPORT_PATH"] = "/opt/Qt5.8.0/5.8/gcc_64/qml"
os.environ["QML2_IMPORT_PATH"] = "/opt/Qt5.8.0/5.8/gcc_64/qml"
os.environ["QT_IM_MODULE"] = "qtvirtualkeyboard"
#print(os.environ)
from PyQt5.QtCore import *
from PyQt5 import QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtQuick import *
class keyboardapp(object):
def __init__(self):
self.view = QQuickView()
self.view.setObjectName("View")
#self.view.setFlags(Qt.FramelessWindowHint)
self.view.setSource(QUrl("main.qml"))
self.view.setResizeMode(QQuickView.SizeRootObjectToView)
#self.Screen = self.view.rootObject()
#print("Screen(Root) = " + str(self.Screen))
self.view.show()
app = QApplication(sys.argv)
test = keyboardapp()
sys.exit(app.exec_())
I been stuck with this too, and i am new to Qt
After some researching, and digging in source code, found the solution
You dont need to import it to use it since its a module it will implement itself to every qt input
Well you dont need to install it on PyQt5 but on Qt5 if it is not already come with your Qt package, if you use archlinux you can install it with pacman
pacman -S qt5-virtualkeyboard
If you cannot find it in you os repositories try to build it here is documantation
https://doc.qt.io/qt-5/qtvirtualkeyboard-index.html
Then to use it in your pyqt application, set environmental variable QT_IM_MODULE to "qtvirtualkeyboard" either from your bash or inside the top your script like
import os
os.environ["QT_IM_MODULE"] = "qtvirtualkeyboard"

Categories