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

霸气de小男生 提交于 2019-12-18 04:18:11

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/21437841/how-to-connect-a-qt-quick-button-click-to-a-c-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!