Qt信号和插槽 - 没有任何反应 [英] Qt Signals and Slots - nothing happens
问题描述
我目前正在尝试将QML信号连接到C ++插槽失败。
I'm currently trying to connect a QML Signal to a C++ Slot unsuccessfully.
我刚看了几个其他例子,但我想我没有得到它如何获取一个qml文档的根对象...
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文件发送,但不是在cpp文件中接收。执行此代码时没有错误。
My problem is, it seems like the signal will be sent from the qml file, but not received in the cpp file. 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
}
}
我知道有很多类似的主题。
因为某些原因,没有其他解决方案为我工作..也许我应该改变我的IDE:D
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
推荐答案
要添加计数器作为上下文属性。这需要以下更改。
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
Counter.cpp文件
The Counter.cpp file
//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文件
The main.cpp file
//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文件中,您可以访问Counter对象的插槽引用counterObj。
And in the qml file, you can access the slot of the Counter object by referring to counterObj.
//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
}
}
}
这篇关于Qt信号和插槽 - 没有任何反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!