Is it possible execute python in qml?

1,013 views
Skip to first unread message

鄭暐翰

unread,
Nov 28, 2015, 5:23:16 AM11/28/15
to Machinekit
Tab {
    id: python tab
    title: qsTr("execute Python")

     Button {
                 id: mybtn
                 text: "press"
                 onClicked: {
                                  /* python example.py */
                 }
}

schoo...@btinternet.com

unread,
Nov 28, 2015, 5:53:40 AM11/28/15
to machi...@googlegroups.com
I would not imagine that qml has a specific method to execute python
scripts, however the underlying C++ is the same as normal Qt

You can for instance launch any process by using QProcess, so just need
to define a class that does that and call it in your qml bit

eg (off the top of my head - not for cut and paste)

class pyLauncher : public QObject
{
Q_OBJECT

public:
pyLauncher(QWidget *parent);
virtual ~pyLauncher() {};

void launch(QString& str);
private:
QProcess *proc;
}


pyLauncher :: pyLauncher(QWidget *parent)
{
proc = new QProcess(parent);
}

void pyLauncher :: launch(QString& str);
{
QString s;
s = "python " + str;
proc->start(s);
}


> Tab {
> id: python tab
> title: qsTr("execute Python")
>
> Button {
> id: mybtn
> text: "press"
> onClicked: {
> mypyLauncherInstance->launch("example.py");
> }
> }

regards

鄭暐翰

unread,
Nov 29, 2015, 8:02:27 PM11/29/15
to Machinekit
Thanks for Schooner.
But I still have problem.
when I use machineface user interface where can i append these code?
I am confuse that where do I append these code?
have any complete example for me.

Thank you very much.

Yishin Li

unread,
Nov 29, 2015, 11:06:30 PM11/29/15
to Machinekit

schoo...@btinternet.com

unread,
Nov 30, 2015, 3:52:17 AM11/30/15
to machi...@googlegroups.com
Yes, that is a good example and surprisingly close to what you asked about.

All you basically do is declare an instance of the pyLauncher class in your code, so that you can use it in the qml file.

Note that it appears you need to declare a function that will be referenced in qml, with the parameter Q_INVOKABLE, or declare it as a slot specifically in the class.
See the code linked to for an example of that.

So I have learnt something too, albeit I don't use QtQuick or Qml, my knowledge is largely from 'standard' Qt C++

regards

-Yishin
--
website: http://www.machinekit.io blog: http://blog.machinekit.io github: https://github.com/machinekit
---
You received this message because you are subscribed to the Google Groups "Machinekit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to machinekit+...@googlegroups.com.
Visit this group at http://groups.google.com/group/machinekit.
For more options, visit https://groups.google.com/d/optout.

鄭暐翰

unread,
Nov 30, 2015, 6:53:47 PM11/30/15
to Machinekit
Excellent, problem have solved.

Thanks for everyone.

regards

Schooner於 2015年11月30日星期一 UTC+8下午4時52分17秒寫道:

Alexander Rössler

unread,
Nov 30, 2015, 10:37:50 PM11/30/15
to 鄭暐翰, Machinekit
Not native. But there is a very promising QML plugin that provides this
feature:
https://thp.io/2011/pyotherside/
--
Alexander
Reply all
Reply to author
Forward
0 new messages