[PyQt] Invoke function QML with arguments

Charlie Gentil ceg at redaction-developpez.com
Wed Feb 19 20:10:24 GMT 2014


Le mercredi 19 février 2014, 19:03:10 Phil Thompson a écrit :
> On 19-02-2014 6:18 pm, Charlie Gentil wrote:
> > Hello,
> > 
> > I want to invoke a function from QML Python. I use this:
> > 
> > obj=app.rootObjects()
> > 
> > myObject=obj[0].findChild(QObject,'myObj')
> > 
> > QMetaObject.invokeMethod(myObject,"myTest",Qt.DirectConnection)
> > 
> > It works fine, but now I have to pass arguments.
> > 
> > I tried this:
> > 
> > obj=app.rootObjects()
> > 
> > myObject=obj[0].findChild(QObject,'myObject')
> > 
> > QMetaObject.invokeMethod(myObject,"myTest",Qt.DirectConnection,
> > Q_ARG(int, 1020
> > 
> > My QML function :
> > 
> > functionmyTest(x){
> > 
> >  console.log(x)
> > 
> > }
> > 
> > But I have this error in return:
> > 
> > QMetaObject::invokeMethod: No such method
> > MyQML_QMLTYPE_100::myTest(int)
> > 
> > QMetaObject.invokeMethod(myObject, "myTest", Qt.DirectConnection,
> > Q_ARG(int, 1020))
> > 
> > RuntimeError: QMetaObject.invokeMethod() call failed
> > 
> > Can you help me?
> > 
> > In advance thank you
> > 
> > Charlie
> 
> Can you provide a short, complete script that demonstrates the 
problem?
> 
> Phil
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt


Hi,

A simple example : 

Python :
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtCore import QObject, QUrl, pyqtSlot, QVariant, QMetaObject, 
Q_ARG, Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import qmlRegisterType, QQmlApplicationEngine



class MainApp(QObject):
    def /__init__/(self, parent=None):
        super(MainApp, self)./__init__/(parent)



    @pyqtSlot(QVariant, QVariant, QVariant)
    def funct1(self, val1, val2, myengine):

        result = val1*val2

        obj = myengine.rootObjects()
        myObject = obj[0].findChild(QObject, 'myGui')
        QMetaObject.invokeMethod(myObject, "myTest1", Qt.DirectConnection, 
Q_ARG(int,result))

        return 0

    @pyqtSlot(QVariant)
    def funct2(self, myengine):

        obj = myengine.rootObjects()
        myObj = obj[0].findChild(QObject, 'myGui')
        QMetaObject.invokeMethod(myObj, "myTest2", Qt.DirectConnection)

        return 0



if /__name__/ == "__main__":

    qmlRegisterType(MainApp, "Charts", 1, 0, "MainApp")

    app = QApplication(sys.argv)
    engine = QQmlApplicationEngine()
    ctx = engine.rootContext()
    ctx.setContextProperty("mainAppPy", engine)

    engine.load('Test.qml')

    win = engine.rootObjects()[0]
    win.show()
    sys.exit(app.exec_())



QML :
import QtQuick 2.2
import QtQuick.Window 2.0
import QtQuick.Controls 1.1
import Charts 1.0


ApplicationWindow {
    title: /qsTr/("Test Invoke")

    width: 200
    height: 100

    MainApp{
        id : /mainApp/
    }

    Item {
        objectName: "myGui"
        function /myTest1/(x){
            /console/.log("result 1 is : ", /x/)
        }

        function /myTest2/(){
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20140219/295da2dd/attachment-0001.html>


More information about the PyQt mailing list