簡體   English   中英

Qt信號和插槽 - 沒有任何反應

[英]Qt Signals and Slots - nothing happens

我目前正在嘗試將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對象的插槽。

  //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()返回一個QQuickItem ,顯然在Qt5.0上沒有任何點擊信號( http://qt-project.org/doc/qt-5.0/qtquick/qquickitem.html

確保將正確(現有)信號鏈接到正確的插槽,並確保信號實際被調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM