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.
Related
i have few web based games on html and java-script , want to open it inside pyqt5 window in python.
i was trying it on tkinter but failed to render html in it.
need help
Thanks.
Suppose this game (contra) want to play it inside pyqt5 window
screenshot of pycharm package installer
Screenshots of IDLE
Screenshot pycharm
This isn't using PyQt5 but you could launch a temp web server using SimpleHTTPServer
$ python2 -m SimpleHTTPServer 8000
or
$ python3 -m http.server 8000
It will serve up whatever is in the folder you run the command in. This shouldn't be used for production as it only provides the most basic security checks.
https://www.poftut.com/how-to-run-and-use-simple-http-server-in-python2-and-python3/
A simple solution is to use QWebEngineView that comes in the PyQtWebEngine package
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
view = QtWebEngineWidgets.QWebEngineView()
url = "https://www.retrogames.cz/play_022-NES.php?language=EN"
view.load(QtCore.QUrl(url))
view.show()
sys.exit(app.exec_())
You could load the page in a QWebView:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import QWebEngineView as QWebView, QWebEnginePage as QWebPage
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("https://oldgameshelf.com/contra-208.html"))
web.show()
sys.exit(app.exec_())
If you need to scroll to a particular element, you could pass JavaScript to web.runJavaScript() that does the scrolling.
In the example above, I used the oldgameshelf.com link of the game you linked to. But you could replace this with the URL of any page.
Also, make sure you install PyQtWebEngine if you haven't already:
pip install PyQtWebEngine
Since it's now in a separate package.
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
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"
Is there possible to use PyQt5 in Visual Studio 2015?
I installed PyQt and created python project. I get an error.
No module named 'PyQt5'
If there is a handler for this exception, the
program may be safely continued.
What do I have to do?
import sys
from PyQt5 import QtWidgets
def main():
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
button = QtWidgets.QPushButton("Hello world")
window.setCentralWidget(button)
window.show()
app.exec_()
if __name__ == '__main__':
main()
Try this:
In "Solution Explorer" choose "References"->"Add Reference"->"Browse"- tab, where select *.pyd files in (Python installation folder)\Python34\Lib\site-packages\PyQt4.
Then in code one can use something like this: "from Qt import * "
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!