[PyQt] QQmlApplicationEngine and QGuiApplication
Daniel Krause
madakr at web.de
Sat Feb 27 17:20:53 GMT 2016
Hi,
I replaced the ApplicationWindow{...} with Rectangle{...} that did the
trick, also for nested structures
The QML-code:
import QtQuick 2.5
import QtQuick.Controls 1.4
Rectangle {
id: mainForm1
color: "#383535"
anchors.fill: parent
SplitView {
id: splitView1
width: 100
anchors.left: parent.left
anchors.leftMargin: 36
anchors.bottom: parent.bottom
anchors.bottomMargin: 8
anchors.top: parent.top
anchors.topMargin: 8
orientation: Qt.Vertical
Button {
id: btn_welcome
x: 49
y: 21
text: qsTr("Welcome")
}
Button {
id: btn_bye
x: 49
y: 50
text: qsTr("Bye")
}
MouseArea {
id: mouseArea1
x: 0
y: 191
width: 100
height: 100
}
}
}
The Python code:
import sys
from PyQt5.QtCore import QUrl, QRect
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQuick import QQuickView
class MyQmlApplication(QApplication):
_application_name = "UNDEFINED"
_qquickview = None
_engine = None
_settings = None
def __init__(self, title, args):
QApplication.__init__(self, args)
self._qquickview = QQuickView()
self._qquickview.setTitle(title)
self._qquickview.setResizeMode(QQuickView.SizeRootObjectToView)
self._qquickview.setGeometry(QRect(200,200,200,100))
self._engine = self._qquickview.engine()
def showAndExec(self, qml_url):
self._qquickview.setSource(qml_url)
self._qquickview.show()
return self.exec_()
if __name__ == '__main__':
app = MyQmlApplication('Test',sys.argv)
app.showAndExec(QUrl("main.qml"))
[image: Inline-Bild 1]
Thanks for the hints!
Daniel
2016-02-25 23:56 GMT+01:00 Jérôme Godbout <jerome at bodycad.com>:
> Sound like you are missing the plugin and module path maybe. You can add
> this function to your new MyQmlApplication class:
>
> def loadPluginPath(self):
> for i in os.environ['QT_PLUGIN_PATH'].split(';'):
> self._engine.addPluginPath(i)
>
>
> You can also add the following before creating the application object:
>
> os.environ['QML2_IMPORT_PATH'] = ';'.join(['.', r"qml"])
> os.environ['QT_PLUGIN_PATH'] = ';'.join(['.', r"plugins"])
>
> Add any required path to both depending on your use case. Call the
> app.loadPluginPath() before the app.showAndExec()
>
> This way the QQmlEngine will known where the module and plugin can be
> found. Make sure you have the Qt modules and plugin available inside those
> path list.
>
> Also on another note, Try to remove the ApplicationWindow from the script
> and try a simple script that only display a simple Item like this, you may
> have some Qml errors preventing the application from launching:
>
> TestMain.qml:
> import QtQuick 2.5
> Rectangle
> {
> color: 'red'
> width: 500
> height: 500
> }
>
> On Thu, Feb 25, 2016 at 4:25 PM, Daniel Krause <madakr at web.de> wrote:
>
>> Thank you for your suggestions.
>>
>> @Jerome
>> I used you code "as is", but for the last lines: I changed them to
>>
>> if __name__ == '__main__':
>> app = MyQmlApplication("MyAppTitle", sys.argv)
>> app.showAndExec(QUrl("main.qml"))
>>
>> The code executes, but the Window is empty.
>>
>> [image: Inline-Bild 1]
>>
>> @Miguel:
>> I changed additionally the name "MainForm" to "Rectangle" as you
>> suggested. But still, the window is empty...
>>
>> I have the feeling, that the qml-file is not found / loaded. If I change
>> the file name to "Main.qml" (which does not exist), the window looks the
>> same...
>> Could it be an OS-problem, as I am working on windows?
>>
>> Any ideas would be welcome.
>>
>> Daniel
>>
>> 2016-02-25 18:37 GMT+01:00 Jérôme Godbout <jerome at bodycad.com>:
>>
>>> No sure what is wrong with yours, but I'm using a QQuickView inside a
>>> QApplication and it work well.
>>>
>>> import sys
>>>
>>> from PyQt5.QtCore import QSettings, QUrl, QRect, pyqtSignal, pyqtSlot
>>> from PyQt5.QtWidgets import QApplication
>>> from PyQt5.QtQuick import QQuickView, QQuickWindow
>>>
>>> class MyQmlApplication(QApplication):
>>> _application_name = "UNDEFINED"
>>> _qquickview = None
>>> _engine = None
>>> _settings = None
>>>
>>> def __init__(self, title, args):
>>> QApplication.__init__(self, args)
>>> self._qquickview = QQuickView()
>>> self._qquickview.setTitle(title)
>>> self._qquickview.setResizeMode(QQuickView.SizeRootObjectToView)
>>> self._qquickview.setGeometry(QRect(100,100,600,800))
>>> self._engine = self._qquickview.engine()
>>>
>>> def showAndExec(self, qml_url):
>>> self._qquickview.setSource(qml_url)
>>> self._qquickview.show()
>>> return self.exec_()
>>>
>>> app = BodycadQmlApplication("MyAppTitle", sys.argv)
>>> app.showAndExec(QUrl("Main.qml"))
>>>
>>>
>>> On Thu, Feb 25, 2016 at 10:43 AM, Daniel Krause <madakr at web.de> wrote:
>>>
>>>> Hello,
>>>>
>>>> I am new to PyQt and having trouble to include QML-Code into an python
>>>> application.
>>>>
>>>> The QML-File (main.qml) I can build in a project in QtCreator (a Qt
>>>> Quick Application was used to create the project).
>>>>
>>>> import QtQuick 2.5
>>>> import QtQuick.Controls 1.4
>>>> import QtQuick.Dialogs 1.2
>>>> import QtQml.Models 2.1
>>>>
>>>> ApplicationWindow {
>>>> visible: true
>>>> width: 640
>>>> height: 480
>>>> title: qsTr("Hello World")
>>>>
>>>> menuBar: MenuBar {
>>>> Menu {
>>>> title: qsTr("File")
>>>> MenuItem {
>>>> text: qsTr("&Open")
>>>> onTriggered: console.log("Open action triggered");
>>>> }
>>>> MenuItem {
>>>> text: qsTr("Exit")
>>>> onTriggered: Qt.quit();
>>>> }
>>>> }
>>>> }
>>>>
>>>> MainForm {
>>>> id: mainForm1
>>>> anchors.fill: parent
>>>>
>>>> SplitView {
>>>> id: splitView1
>>>> width: 100
>>>> anchors.left: parent.left
>>>> anchors.leftMargin: 36
>>>> anchors.bottom: parent.bottom
>>>> anchors.bottomMargin: 8
>>>> anchors.top: parent.top
>>>> anchors.topMargin: 8
>>>> orientation: Qt.Vertical
>>>>
>>>>
>>>> Button {
>>>> id: btn_welcome
>>>> x: 49
>>>> y: 21
>>>> text: qsTr("Welcome")
>>>> }
>>>>
>>>> Button {
>>>> id: btn_database
>>>> x: 49
>>>> y: 50
>>>> text: qsTr("Database")
>>>> }
>>>>
>>>> MouseArea {
>>>> id: mouseArea1
>>>> x: 0
>>>> y: 191
>>>> width: 100
>>>> height: 100
>>>> }
>>>> }
>>>> }
>>>> }
>>>>
>>>> It results in the following window:
>>>>
>>>>
>>>> To create the application in Python I tried to use QGuiApplication
>>>> and QQmlApplicationEngine.
>>>> main.py and main.qml are located in the same directory:
>>>>
>>>> import sys
>>>> from PyQt5.QtQml import QQmlApplicationEngine
>>>> from PyQt5.QtGui import QGuiApplication
>>>>
>>>> if __name__ == '__main__':
>>>> app = QGuiApplication(sys.argv)
>>>> engine = QQmlApplicationEngine("main.qml")
>>>> print(engine)
>>>> sys.exit(app.exec_())
>>>>
>>>> The print()-statement is never executed, no window is appearing.
>>>>
>>>> What did I miss?
>>>>
>>>> Thanks in advance
>>>>
>>>> Daniel
>>>>
>>>> _______________________________________________
>>>> PyQt mailing list PyQt at riverbankcomputing.com
>>>> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>>>>
>>>
>>>
>>
>> _______________________________________________
>> PyQt mailing list PyQt at riverbankcomputing.com
>> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160227/1c8906eb/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 3019 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160227/1c8906eb/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 7969 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20160227/1c8906eb/attachment-0003.png>
More information about the PyQt
mailing list