Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
568 views
in Technique[技术] by (71.8m points)

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

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 standard qt quick mobile app that qt makes for you. Here is all the files.

(I'm brand new to this so these might be noob qu's, sorry) Ok so here is my list of I Don't Knows:

  1. how on earth do I connect this class method to the button click in the qml file.
  2. Would it be possible to connect the same click and pass params through the click from the qml to the method and if so how would I do it?

  3. and also I was wondering if I could link the class to the qt quick like a regular QWidget class for instance:

    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);
    SomeClass *sc = new SomeClass();
    
    QtQuick2ApplicationViewer viewer;
    
    // this I guess would be more like the Widget method but this i really dont know 
    // about, just a question I'm throwing out there
    viewer. (Link qt quick qml to object "sc"(Containing all my processes))
    
    viewer.setMainQmlFile(QStringLiteral("qml/Final2/main.qml"));
    viewer.showExpanded();
    
    return app.exec();
    }
    

The main(.)cpp file

#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;
    viewer.setMainQmlFile(QStringLiteral("qml/Final2/main.qml"));
    viewer.showExpanded();

    return app.exec();
}

This is just the button element from my main.qml

Button {
        id: btn
        y: 443
        height: 39
        text: "Click Me"
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 8
        anchors.right: parent.right
        anchors.rightMargin: 25
        anchors.left: parent.left
        anchors.leftMargin: 15
    }

This is just some random class header :

#ifndef SOMECLASS_H
#define SOMECLASS_H

#include <QObject>
#include <QDebug>>

class SomeClass : public QObject
{
    Q_OBJECT
public:
    explicit SomeClass(QObject *parent = 0);

signals:

public slots:
    void buttonClicked();
    void buttonClicked(QString &in);
};

#endif // SOMECLASS_H

Of course the cpp file :

#include "someclass.h"

SomeClass::SomeClass(QObject *parent) :
    QObject(parent)
{
}

void SomeClass::buttonClicked()
{
    // do something
}

void SomeClass::buttonClicked(QString &in)
{
    qDebug() << "Your string was : " << in;
}

I really appreciate all the help. Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...