c++ - Qt 信号和插槽 - 没有任何反应

标签 c++ qt signals slots

我目前正在尝试将 QML 信号连接到 C++ 插槽,但未成功。

我刚刚看了几个其他的例子,但我想我不明白如何获取 qml 文档的根对象...

我的问题是,信号似乎是从 qml 文件发送的,但没有在 cpp 文件中接收到。执行此代码时没有错误。

//Counter.h
#ifndef COUNTER_H
#define COUNTER_H

#include <QObject>

class Counter : public QObject
{
    Q_OBJECT

private:
    int counter;

public:
    explicit Counter(QObject *parent = 0);

signals:
    void counterHasChanged(int);

public slots:
    void click();
};

#endif // COUNTER_H


//counter.cpp
#include "counter.h"
#include <QDebug>

Counter::Counter(QObject *parent) :
    QObject(parent)
{
    this->counter = 0;

    qDebug() << "Class Counter created";
}

void Counter::click() {
    this->counter++;

    qDebug() << "clickRegistered() - emit counterHasChanged()";

    emit counterHasChanged(counter);
}



//main.cpp
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include "counter.h"
#include <QObject>
#include <QtQuick>
#include <QDebug>
#include <QQuickItem>
#include <QQmlContext>
#include <QtCore>

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

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

    QQuickView view;
    view.setSource(QUrl::fromLocalFile("qml/StatesTest2/main.qml"));

    QObject *item = view.rootObject();

    Counter counter;

    QObject::connect(item, SIGNAL(click()), &counter, SLOT(click()));

    return app.exec();
}



//main.qml
import QtQuick 2.0

Rectangle {
    id: root
    width: 360
    height: 360

    signal click

    Text {
        text: qsTr("Hello World")
        anchors.centerIn: parent
    }

    MouseArea {

        anchors.fill: parent
        onClicked: {
            root.click
            console.log("click() qml")
        }
    }

    Text {
        text: "clicks: "
        x: 30
        y: 250
    }

    Text {
        id: counter
        text: "0"
        x: 75
        y: 250
    }
}

我知道有很多这样的话题.. 出于某种原因,没有其他解决方案对我有用..也许我应该改变我的 IDE :D

最佳答案

我建议您将计数器添加为上下文属性。这需要进行以下更改。

//Counter.h
#ifndef COUNTER_H
#define COUNTER_H

#include <QObject>

class Counter : public QObject
{
    Q_OBJECT

private:
    int counter;

public:
    explicit Counter(QObject *parent = 0);

signals:
    void counterHasChanged(int Value);

public slots:
    void click();
};

#endif // COUNTER_H

Counter.cpp 文件

//counter.cpp
#include "counter.h"
#include <QDebug>

Counter::Counter(QObject *parent) :
    QObject(parent)
{
    this->counter = 0;

    qDebug() << "Class Counter created";
}

void Counter::click() {
    this->counter++;

    qDebug() << "clickRegistered() - emit counterHasChanged()";

    emit counterHasChanged(counter);
}

main.cpp文件

   //main.cpp
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include "counter.h"
#include <QObject>
#include <QtQuick>
#include <QDebug>
#include <QQuickItem>
#include <QQmlContext>
#include <QtCore>

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

    Counter counter;
    QtQuick2ApplicationViewer viewer;
    viewer.rootContext()->setContextProperty("counterObj",&counter);

    viewer.setMainQmlFile(QStringLiteral("qml/SO_QMLCPPCommunication/main.qml"));


    viewer.showExpanded();

    return app.exec();
}

并且在qml文件中,可以通过引用counterObj来访问Counter对象的slot。

  //main.qml
import QtQuick 2.0

Rectangle {
    id: root
    width: 360
    height: 360

    signal click

    Text {
        text: qsTr("Hello World")
        anchors.centerIn: parent
    }

    MouseArea {

        anchors.fill: parent
        onClicked: {
            counterObj.click()
            console.log("click() qml")
        }
    }

Text {
    text: "clicks: "
    x: 30
    y: 250
}

Text {
    id: counter
    text: "0"
    x: 75
    y: 250
}
Connections
{
    id:cppConnection
    target:counterObj
    ignoreUnknownSignals : true
    onCounterHasChanged:{
          //To access signal parameter,please name the parameter.
          console.debug("Counter value changed")
          counter.text = Value
   }
}

}

关于c++ - Qt 信号和插槽 - 没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22605257/

相关文章:

c++ - 我可以将一个值放入存储在 C++ 映射中的 vector 中吗?

c++ - 在 Qt 中选择实体

python - 如何修复 : "Attribute Qt::AA_EnableHighDpiScaling must be set before QCoreApplication is created." warning

python - 在 Queue.Queue.get() 中阻塞时处理 SIGTERM

c++ - 尝试创建SurfaceMesh,但存在连接问题

python - 3D *.STL 表面模型转换为 2D 图像堆栈?

c++ - 我如何运行 quickfix 示例?

c++ - Windows GUI 以及控制台应用程序

c - 以延迟取消结束 pthread

matlab - Matlab 中的矢量化像素加倍插值