如何将Qt Quick的按钮点击连接到一个C ++方法 [英] How to connect a Qt Quick button click to a c++ method

查看:157
本文介绍了如何将Qt Quick的按钮点击连接到一个C ++方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习关于Qt移动编程Qt Quick的2.0,我已经对谷歌整天和它的驾驶我疯狂所以这里去。
我有只是你的标准Qt Quick的移动应用,QT使得你。这里是所有文件。

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. 如何在地球上我这个类的方法连接到该QML文件中的按钮点击。

  2. 有没有可能连接相同的点击,并通过点击从QML PARAMS传递给方法,如果是我会怎么做呢?

  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?

和还我想知道如果我可以在类链接到Qt Quick的像一个普通的QWidget类,例如:

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


在main()CPP文件

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

这仅仅是个按钮元素从我main.qml

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

当然cpp文件:

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

我真的AP preciate所有帮助。
谢谢你。

I really appreciate all the help. Thank you.

推荐答案

首先,您需要将SomeClass的对象导出到QtQuick(是你的第三个问题):

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

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

这使得QtQuick提供的SC对象,名称为_someObject下。

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

然后,在QML使用它是这样的:

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

这篇关于如何将Qt Quick的按钮点击连接到一个C ++方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆