如何将 Qt 快速按钮单击连接到 C++ 方法 [英] How to connect a Qt Quick button click to a c++ method

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

问题描述

我刚刚开始学习使用 Qt Quick 2.0 进行 Qt Mobile 编程,我整天都在 google 上,这让我发疯了,所以就这样吧.我只有 qt 为您制作的标准 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 传递到方法,如果可以,我该怎么做?

  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?

而且我还想知道是否可以像常规 QWidget 类一样将该类快速链接到 qt:

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文件:

#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.

推荐答案

首先,您需要将 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);

这使得 sc 对象在 QtQuick 中可用,名称为_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.
    }
}

这假设 Button 有一个 clicked() 信号,它在点击/触摸时发出.不知道您使用的是哪个 Button 组件,我无法检查.

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.

要传递参数,只需执行

onClicked: {
    _someObject.buttonClicked("Something");
}

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

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