I have added a simple QML component ("qml/MyButton") to my "resource.qrc" file:
<RCC>
<qresource prefix="/">
<file>qml/MyButton.qml</file>
</qresource>
</RCC>
I then compiled the QRC to a python module with:
pyside2-rcc -o resource.py resource.qrc
Then I imported resource.py in main.py:
import sys
import os
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine
import resource
if __name__ == "__main__":
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load(os.path.join(os.path.dirname(__file__), "main.qml"))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
And called MyButton component in main.qml:
import QtQuick 2.13
import QtQuick.Window 2.13
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
MyButton {
}
}
This is "qml/MyButton.qml":
import QtQuick 2.0
import QtQuick.Controls 2.13
Button {
text: 'Click Me'
}
When I run the program I get the error that "MyButton is not a type". I want to use the QML component by using the python generated resource file. I don't know what I am doing wrong.
Automatic import if the .qml is next to the main file but in your case MyButton.qml is not next to the main.qml so the package has to be imported:
import QtQuick 2.13
import QtQuick.Window 2.13
import "qrc:/qml"
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
MyButton {
}
}
Related
I want show charts in my app with Python back-end and qml front-end but I got an error.
Import:
import QtCharts 2.3
Error:
module "QtCharts" is not installed
I installed qtcharts with maintaince.exe
How to solve this?
In PyQt6 the chart module comes in a separate package and you must run:
python -m pip install PyQt6-Charts PyQt6
On the other hand maintaince.exe only works for C++ so installing QtCharts does not affect PyQt6.
In the next part there is a demo:
import os
from pathlib import Path
import sys
from PyQt6.QtCore import QCoreApplication, Qt, QUrl
from PyQt6.QtWidgets import QApplication
from PyQt6.QtQml import QQmlApplicationEngine
CURRENT_DIRECTORY = Path(__file__).resolve().parent
def main():
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
filename = os.fspath(CURRENT_DIRECTORY / "main.qml")
url = QUrl.fromLocalFile(filename)
def handle_object_created(obj, obj_url):
if obj is None and url == obj_url:
QCoreApplication.exit(-1)
engine.objectCreated.connect(
handle_object_created, Qt.ConnectionType.QueuedConnection
)
engine.load(url)
sys.exit(app.exec())
if __name__ == "__main__":
main()
import QtQuick
import QtQuick.Controls
import QtCharts
ApplicationWindow {
id: root
width: 640
height: 480
visible: true
title: qsTr("Login Page")
ChartView {
id: chartView
anchors.fill: parent
LineSeries {
XYPoint {
x: 0
y: 0
}
XYPoint {
x: 1.1
y: 2.1
}
}
}
}
Note: In Qt6 the versions of the QML modules should not be indicated.
I have a qt quick pyside application .I had a question before,but now another proplem is there .Just a empty window appears and then in application window I see below message.Although I have another qt quick application that I written that in c++ and there is no problem displaying it, this message is displayed!,This application is in python(pyside6)
I use Qt 6.0.2,Python 3.9.2,Qt Creator 4.14.1 and Pyside6
Failed to create vertex shader: Error 0x80070057: The parameter is incorrect.
Failed to build graphics pipeline state
*main.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
Window {
id:mainWindow
width: 1000
height: 580
visible: true
title: qsTr("JooyaTrader")
Rectangle{
width: 152
height: 62
anchors.fill: parent
color: "red"
}
}
main.py
import sys,os
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
import PySide6
if __name__ == "__main__":
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load(os.path.join(os.path.dirname(__file__), "main.qml"))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
The problem is caused because the backend that Qt Quick uses for rendering does not work for your case, either because there are missing libraries or the version is not according to what Qt expects. In that one solution is to set the QT_QUICK_BACKEND in "software" making the rendering do it Qt Quick 2D Renderer:
os.environ["QT_QUICK_BACKEND"] = "software"
app = QGuiApplication(sys.argv)
For more information read Scene Graph Adaptations.
I just created a new project in Qt-creator: "Qt for python QtQuick application",
When i try to run automatically generated code it throws following error
Error
QQmlApplicationEngine failed to load component
file:///C:/Users/dell/Documents/test1/qml/main.qml:2:1: plugin cannot be loaded for module "QtQuick.Window": Cannot load library C:\Users\dell\Anaconda3\Library\qml\QtQuick\Window.2\windowplugin.dll: The specified procedure could not be found.
main.py
# This Python file uses the following encoding: utf-8
import sys
import os
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine
if __name__ == "__main__":
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load(os.path.join(os.path.dirname(__file__), "main.qml"))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
main.qml
import QtQuick 2.15
import QtQuick.Window 2.15
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
}
I am using pyqt and I have the following directory structure:
root
----> apps/
----> ui/
I have a simple qml based application in the app folder as:
apps/testqt.py
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQuick import QQuickView, QQuickWindow
from PyQt5.QtQml import qmlRegisterType, QQmlApplicationEngine
import sys
app = QApplication(sys.argv)
engine = QQmlApplicationEngine('ui/window.qml')
topLevel = engine.rootObjects()[0]
win = QQuickWindow(topLevel)
win.show()
app.exec_()
ui/window.qml
The qml file defines the app window and uses a StackView as follows:
import QtQuick 2.0
import QtQuick.Window 2.1
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.4
ApplicationWindow {
id: rootWindow
objectName: "window"
visible: true
width: 800
height: 480
title: "Window"
Component.onCompleted: {
setX(Screen.width / 2 - width / 2);
setY(Screen.height / 2 - height / 2);
}
property Component loginView: LoginView {}
StackView {
id: stackView
anchors.fill: parent
Component.onCompleted:
{
stackView.push(loginView)
}
}
}
This uses the LoginView component which is defined as:
apps/LoginView.qml
import QtQuick 2.0
import QtQuick.Window 2.1
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.4
import QtQuick.Dialogs 1.0
ControlView {
ColumnLayout {
anchors.centerIn: parent
spacing: 25
width: 200
TextField {
id: username_fld
placeholderText: qsTr("User name")
Layout.fillWidth: true
}
TextField {
id: password_fld
placeholderText: qsTr("Password")
Layout.fillWidth: true
echoMode: TextInput.Password
}
RowLayout {
Button {
id: login_button
text: "Log In"
Layout.fillWidth: true
}
Button {
id: cancel_button
text: "Cancel"
Layout.fillWidth: true
}
}
}
}
Now when I use qmlscene, the view loads just fine. However, running python results in the application hanging while trying to load the QQmlApplicationEngine. I have a feeling it has something to do with the qml paths perhaps, so I included import ../ui into the window.qml imports but that did not change anything.
I am using Python 2.7 with Qt 5.6 in an Anaconda environment. Qt was installed from here: https://anaconda.org/anaconda/pyqt
Similarly i use multiple qml files along with multiple python files. When i access application it loads perfectly two qml files successfully but when stacked through third qml page, the application crashes. It throws segmentation fault.
I use Stackview to push and pop through QML pages, during debugging i found that it works when push and pop are done for just for two pages.
I have came across if i use Swipeview too. What i guess is that the engine that was created can be loaded with two stack QML files.
I have a python file and a qml file.
There is a button in the qml file to load a FileDialog. When I directly use qmlscene test.qml, the FileDialog is ok. But when I use python3 main.py, the FileDialog is strange, and I can't select a file by it. Please tell me how to fix it.
This is the normal file-dialog:
And this is the strange file-dialog:
The code is the following:
test.qml
import QtQuick 2.4
import QtQuick.Dialogs 1.2
import QtQuick.Controls 1.3
import QtQuick.Controls.Styles 1.3
import QtQuick.Layouts 1.1
Rectangle {
width: 400
height:30
Button {
id: save
text: "save"
onClicked: {
fileDialogLoader.item.open()
}
}
Loader {
id: fileDialogLoader
sourceComponent: fileDialog_com
}
Component{
id: fileDialog_com
FileDialog {
id: fileDialog
title: "select a file"
nameFilters: ["pdf files(*.pdf)"]
selectExisting: false
onAccepted: {
console.log(" you choose: "+ fileDialog.fileUrls)
}
}
}
}
main.py
#!/usr/bin/env python
# encoding: utf-8
from PyQt5.QtCore import QUrl, QObject, pyqtSlot
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQuick import QQuickView
class MyMain(QObject):
pass
if __name__ == '__main__':
path = 'test.qml'
app = QGuiApplication([])
view = QQuickView()
con = MyMain()
context = view.rootContext()
context.setContextProperty("con",con)
view.engine().quit.connect(app.quit)
view.setSource(QUrl(path))
view.show()
app.exec()
The "strange" file-dialog is a default implementation that has been written entirely in QML. Qt will use this as a fallback when it cannot create either the platform's native dialog or the built-in QFileDialog.
The reason why your example uses the qml fallback, is because you are using QGuiApplication, which is not widget-based. If you switch to QApplication, your example will work as expected:
# from PyQt5.QtGui import QGuiApplication
from PyQt5.QtWidgets import QApplication
...
# app = QGuiApplication([])
app = QApplication([])