How to connect a Qt Quick button click to a c++ method

前端 未结 1 2016
广开言路
广开言路 2020-12-17 02:54

I\'ve just started to learn about Qt Mobile programming with Qt Quick 2.0 and I\'ve been on google the whole day and its driving me crazy so here goes. I have got just your

相关标签:
1条回答
  • 2020-12-17 03:30

    First, you need to export the SomeClass object to QtQuick (is that your third question?):

    SomeClass sc;
    viewer.rootContext()->setContextProperty(QStringLiteral("_someObject"), &sc);
    

    This makes the sc object available in QtQuick, under the name "_someObject".

    Then, in the QML use it like this:

    Button {
        ....
        onClicked: {
            _someObject.buttonClicked(); //_someObject is the name used in setContextProperty.
        }
    }
    

    This assumes the Button has a signal clicked() that's emitted on click/touch. Not knowing which Button component you use, I can't check that.

    To pass an argument, just do

    onClicked: {
        _someObject.buttonClicked("Something");
    }
    
    0 讨论(0)
提交回复
热议问题