简体   繁体   English

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

[英]Qt Signals and Slots - nothing happens

I'm currently trying to connect a QML Signal to a C++ Slot unsuccessfully. 我目前正在尝试将QML信号连接到C ++插槽失败。

I just had a look on several other examples but I think i didn't got it with how to get the root object of a qml document... 我刚看了几个其他的例子,但我想我没有得到如何获取qml文档的根对象...

My problem is, it seems like the signal will be sent from the qml file, but not received in the cpp file. 我的问题是,似乎信号将从qml文件发送,但在cpp文件中没有收到。 There are no errors when I execute this code. 执行此代码时没有错误。

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

I know there are tons of topics like this.. For some reason, no other solution worked for me.. maybe I should change my IDE :D 我知道有很多这样的话题..出于某种原因,没有其他解决方案对我有用..也许我应该改变我的IDE:D

I suggest you to add counter as a context property.. This requires following changes. 我建议你添加计数器作为上下文属性..这需要进行以下更改。

//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

The Counter.cpp file 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);
}

The main.cpp file 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();
}

And in the qml file, you can access the slot of the Counter object by referring to counterObj. 在qml文件中,您可以通过引用counterObj来访问Counter对象的插槽。

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

}

QQuickView::rootObject() returns you a QQuickItem which apparently on Qt5.0 hasn't any click signal ( http://qt-project.org/doc/qt-5.0/qtquick/qquickitem.html ) QQuickView::rootObject()返回一个QQuickItem ,显然在Qt5.0上没有任何点击信号( http://qt-project.org/doc/qt-5.0/qtquick/qquickitem.html

Make sure you're linking the right (existing) signal to the right slot and that the signal gets actually called. 确保将正确(现有)信号链接到正确的插槽,并确保信号实际被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM