[prev in list] [next in list] [prev in thread] [next in thread]
List: pykde
Subject: Re: [PyQt] QQmlApplicationEngine and QGuiApplication
From: Daniel Krause <madakr () web ! de>
Date: 2016-02-27 17:20:53
Message-ID: CAMiLiKgNDdTwZT_dbRNU9rEYs9S=y3MPVTNazXnxieNQtNTr=g () mail ! gmail ! com
[Download RAW message or body]
[Attachment #2 (multipart/related)]
[Attachment #4 (multipart/alternative)]
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@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@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@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@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@riverbankcomputing.com
>>>> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>>>>
>>>
>>>
>>
>> _______________________________________________
>> PyQt mailing list PyQt@riverbankcomputing.com
>> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
>>
>
>
[Attachment #7 (text/html)]
<div dir="ltr">Hi,<div><br></div><div>I replaced the ApplicationWindow{...} with \
Rectangle{...} that did the trick, also for nested structures</div><div>The \
QML-code:</div><br>import QtQuick 2.5<br>import QtQuick.Controls 1.4<br><br>Rectangle {<br> \
id: mainForm1<br> color: "#383535"<br> anchors.fill: parent<br><br> \
SplitView {<br> id: splitView1<br> width: 100<br> \
anchors.left: parent.left<br> anchors.leftMargin: 36<br> anchors.bottom: \
parent.bottom<br> anchors.bottomMargin: 8<br> anchors.top: parent.top<br> \
anchors.topMargin: 8<br> orientation: Qt.Vertical<br><br> Button {<br> \
id: btn_welcome<br> x: 49<br> y: 21<br> \
text: qsTr("Welcome")<br> }<br><br> Button {<br> \
id: btn_bye<br> x: 49<br> y: 50<br> text: \
qsTr("Bye")<br> }<div><br> MouseArea {<br> id: \
mouseArea1<br> x: 0<br> y: 191<br> width: \
100<br> height: 100<br> }<br> }<br>}<div> <pre \
style="margin-top:0px;margin-bottom:0px"><br></pre></div><div>The Python \
code:</div><div><div><br></div><div>import sys</div><div>from PyQt5.QtCore import QUrl, \
QRect</div><div>from PyQt5.QtWidgets import QApplication</div><div>from PyQt5.QtQuick import \
QQuickView</div><div><br></div><div>class MyQmlApplication(QApplication):</div><div> \
_application_name = "UNDEFINED"</div><div> _qquickview = None</div><div> \
_engine = None</div><div> _settings = None</div><div> </div><div> def \
__init__(self, title, args):</div><div> QApplication.__init__(self, args)</div><div> \
self._qquickview = QQuickView()</div><div> \
self._qquickview.setTitle(title)</div><div> \
self._qquickview.setResizeMode(QQuickView.SizeRootObjectToView)</div><div> \
self._qquickview.setGeometry(QRect(200,200,200,100))</div><div> self._engine = \
self._qquickview.engine()</div><div> </div><div> def showAndExec(self, \
qml_url):</div><div> self._qquickview.setSource(qml_url)</div><div> \
self._qquickview.show()</div><div> return self.exec_()</div><div><br></div><div>if \
__name__ == '__main__':</div><div> app = \
MyQmlApplication('Test',sys.argv)</div><div> \
app.showAndExec(QUrl("main.qml"))</div></div><div><br></div><div><img \
src="cid:ii_15323bc3220944d3" alt="Inline-Bild 1" width="202" \
height="132"><br></div><div><br></div><div>Thanks for the hints!</div><div>Daniel</div><div \
class="gmail_extra"><br><div class="gmail_quote">2016-02-25 23:56 GMT+01:00 Jérôme Godbout \
<span dir="ltr"><<a href="mailto:jerome@bodycad.com" \
target="_blank">jerome@bodycad.com</a>></span>:<br><blockquote class="gmail_quote" \
style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Sound like \
you are missing the plugin and module path maybe. You can add this function to your new \
MyQmlApplication class:<div><br></div><div><div>def loadPluginPath(self):</div><div> \
for i in os.environ['QT_PLUGIN_PATH'].split(';'):</div><div> \
self._engine.addPluginPath(i)</div></div><div><br></div><div><br></div><div>You can also add \
the following before creating the application \
object:</div><div><div><br></div><div>os.environ['QML2_IMPORT_PATH'] = \
';'.join(['.', \
r"qml"])<br></div><div>os.environ['QT_PLUGIN_PATH'] = \
';'.join(['.', r"plugins"])</div></div><div><br></div><div>Add any \
required path to both depending on your use case. Call the app.loadPluginPath() before the \
app.showAndExec()</div><div><br></div><div>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.</div><div><br></div><div>Also on another note, Try to remove the <span \
style="font-size:12.8px">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:</span></div><div><span style="font-size:12.8px"><br></span></div><div><span \
style="font-size:12.8px">TestMain.qml:</span></div><div><span style="font-size:12.8px">import \
QtQuick 2.5</span></div><div><span style="font-size:12.8px">Rectangle</span></div><div><span \
style="font-size:12.8px">{</span></div><div><span style="font-size:12.8px"> color: \
'red'</span></div><div><span style="font-size:12.8px"> width: \
500</span></div><div><span style="font-size:12.8px"> height: 500</span></div><div><span \
style="font-size:12.8px">}</span></div></div><div class="HOEnZb"><div class="h5"><div \
class="gmail_extra"><br><div class="gmail_quote">On Thu, Feb 25, 2016 at 4:25 PM, Daniel Krause \
<span dir="ltr"><<a href="mailto:madakr@web.de" target="_blank">madakr@web.de</a>></span> \
wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex"><div dir="ltr"><div style="font-size:12.8px">Thank you for your \
suggestions.<br></div><span><div style="font-size:12.8px"><br></div><div \
style="font-size:12.8px">@Jerome</div><div style="font-size:12.8px">I used you code "as \
is", but for the last lines: I changed them to</div><div \
style="font-size:12.8px"><br></div></span><div style="font-size:12.8px"><div>if __name__ == \
'__main__':</div><span><div> app = MyQmlApplication("MyAppTitle", \
sys.argv)</div><div> \
app.showAndExec(QUrl("main.qml"))</div></span></div><span><div \
style="font-size:12.8px"><br></div><div style="font-size:12.8px">The code executes, but the \
Window is empty.</div><div style="font-size:12.8px"><br></div><div \
style="font-size:12.8px"><img src="cid:ii_1531a3e3c9fc0060" alt="Inline-Bild 1" width="472" \
height="345"><br></div><div style="font-size:12.8px"><br></div><div \
style="font-size:12.8px">@Miguel:</div><div style="font-size:12.8px">I changed additionally the \
name "MainForm" to "Rectangle" as you suggested. But still, the window is \
empty...</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">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...</div><div \
style="font-size:12.8px">Could it be an OS-problem, as I am working on windows?</div><div \
style="font-size:12.8px"><br></div><div style="font-size:12.8px">Any ideas would be \
welcome.</div><div style="font-size:12.8px"><br></div><div \
style="font-size:12.8px">Daniel</div></span><div class="gmail_extra"><br><div \
class="gmail_quote"><span>2016-02-25 18:37 GMT+01:00 Jérôme Godbout <span dir="ltr"><<a \
href="mailto:jerome@bodycad.com" \
target="_blank">jerome@bodycad.com</a>></span>:<br></span><blockquote class="gmail_quote" \
style="margin:0px 0px 0px \
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div><div><div \
dir="ltr">No sure what is wrong with yours, but I'm using a QQuickView inside a \
QApplication and it work well.<div><br></div><div>import \
sys<br></div><div><div><br></div><div>from PyQt5.QtCore import QSettings, QUrl, QRect, \
pyqtSignal, pyqtSlot</div><div>from PyQt5.QtWidgets import QApplication</div><div>from \
PyQt5.QtQuick import QQuickView, QQuickWindow</div><div><br></div><div>class \
MyQmlApplication(QApplication):</div><div> _application_name = \
"UNDEFINED"</div><div> _qquickview = None</div><div> _engine = \
None</div><div> _settings = None</div><div> </div><div> def __init__(self, \
title, args):</div><div> QApplication.__init__(self, args)</div><div> \
self._qquickview = QQuickView()</div><div> \
self._qquickview.setTitle(title)</div><div> \
self._qquickview.setResizeMode(QQuickView.SizeRootObjectToView)</div><div> \
self._qquickview.setGeometry(QRect(100,100,600,800))</div><div> self._engine = \
self._qquickview.engine()</div><div> </div><div> def showAndExec(self, \
qml_url):</div><div> self._qquickview.setSource(qml_url)</div><div> \
self._qquickview.show()</div><div> return self.exec_()</div><div><br></div><div>app \
= BodycadQmlApplication("MyAppTitle", \
sys.argv)</div><div>app.showAndExec(QUrl("Main.qml"))</div></div><div><br></div></div></div></div><div \
class="gmail_extra"><br><div class="gmail_quote"><div><div><div><div>On Thu, Feb 25, 2016 at \
10:43 AM, Daniel Krause <span dir="ltr"><<a href="mailto:madakr@web.de" \
target="_blank">madakr@web.de</a>></span> wrote:<br></div></div></div></div><blockquote \
class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div><div><div \
dir="ltr"><div><div><span style="font-size:12.8px">Hello,</span><div \
style="font-size:12.8px"><br></div><div style="font-size:12.8px">I am new to PyQt and having \
trouble to include QML-Code into an python application.</div><div \
style="font-size:12.8px"><br></div><div style="font-size:12.8px">The QML-File (main.qml) I can \
build in a project in QtCreator (a Qt Quick Application was used to create the \
project).</div><div style="font-size:12.8px"><br></div><div \
style="font-size:12.8px"><div>import QtQuick 2.5</div><div>import QtQuick.Controls \
1.4</div><div>import QtQuick.Dialogs 1.2</div><div>import QtQml.Models \
2.1</div><div><br></div><div>ApplicationWindow {</div><div> visible: true</div><div> \
width: 640</div><div> height: 480</div><div> title: qsTr("Hello \
World")</div><div><br></div><div> menuBar: MenuBar {</div><div> Menu \
{</div><div> title: qsTr("File")</div><div> \
MenuItem {</div><div> text: qsTr("&Open")</div><div> \
onTriggered: console.log("Open action triggered");</div><div> \
}</div><div> MenuItem {</div><div> text: \
qsTr("Exit")</div><div> onTriggered: Qt.quit();</div><div> \
}</div><div> }</div><div> }</div><div><br></div><div> MainForm \
{</div><div> id: mainForm1</div><div> anchors.fill: \
parent</div><div><br></div><div> SplitView {</div><div> id: \
splitView1</div><div> width: 100</div><div> anchors.left: \
parent.left</div><div> anchors.leftMargin: 36</div><div> \
anchors.bottom: parent.bottom</div><div> anchors.bottomMargin: 8</div><div> \
anchors.top: parent.top</div><div> anchors.topMargin: 8</div><div> \
orientation: Qt.Vertical</div><div><br></div><div><br></div><div> Button \
{</div><div> id: btn_welcome</div><div> x: \
49</div><div> y: 21</div><div> text: \
qsTr("Welcome")</div><div> }</div><div><br></div><div> \
Button {</div><div> id: btn_database</div><div> \
x: 49</div><div> y: 50</div><div> text: \
qsTr("Database")</div><div> }</div><div><br></div><div> \
MouseArea {</div><div> id: mouseArea1</div><div> \
x: 0</div><div> y: 191</div><div> width: \
100</div><div> height: 100</div><div> }</div><div> \
}</div><div> }</div><div>}</div></div><div style="font-size:12.8px"><br></div><div \
style="font-size:12.8px">It results in the following window:</div><div \
style="font-size:12.8px"><br></div><div \
style="font-size:12.8px"><br></div></div></div><span><div style="font-size:12.8px">To create \
the application in Python I tried to use QGuiApplication and QQmlApplicationEngine. \
</div><div style="font-size:12.8px">main.py and main.qml are located in the same \
directory:</div><div style="font-size:12.8px"><br></div><div \
style="font-size:12.8px"><div>import sys</div><div>from PyQt5.QtQml import \
QQmlApplicationEngine</div><div>from PyQt5.QtGui import \
QGuiApplication</div><div><br></div><div>if __name__ == '__main__':</div><div> app \
= QGuiApplication(sys.argv)</div><div> engine = \
QQmlApplicationEngine("main.qml")</div><div> print(engine)</div><div> \
sys.exit(app.exec_())</div></div><div style="font-size:12.8px"><br></div><div \
style="font-size:12.8px">The print()-statement is never executed, no window is \
appearing.</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">What did \
I miss?</div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">Thanks in \
advance</div><span><font color="#888888"><div style="font-size:12.8px"><br></div><div \
style="font-size:12.8px">Daniel</div></font></span></span></div> \
<br></div></div><span>_______________________________________________<br> PyQt mailing list \
<a href="mailto:PyQt@riverbankcomputing.com" \
target="_blank">PyQt@riverbankcomputing.com</a><br> <a \
href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt" rel="noreferrer" \
target="_blank">https://www.riverbankcomputing.com/mailman/listinfo/pyqt</a><br></span></blockquote></div><br></div>
</blockquote></div><br></div></div>
<br>_______________________________________________<br>
PyQt mailing list <a href="mailto:PyQt@riverbankcomputing.com" \
target="_blank">PyQt@riverbankcomputing.com</a><br> <a \
href="https://www.riverbankcomputing.com/mailman/listinfo/pyqt" rel="noreferrer" \
target="_blank">https://www.riverbankcomputing.com/mailman/listinfo/pyqt</a><br></blockquote></div><br></div>
</div></div></blockquote></div><br></div></div></div>
["image.png" (image/png)]
["image.png" (image/png)]
[Attachment #10 (text/plain)]
_______________________________________________
PyQt mailing list PyQt@riverbankcomputing.com
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic