There is a code like this
// interfaceqml.h
class InterfaceQML : public QObject {
Q_OBJECT
public:
InterfaceQML(QObject *parent = 0);
int t;
signals:
void newSignal(int i);
public slots:
void newSlot();
};
//interfaceqml.cpp
InterfaceQML::InterfaceQML(QObject *parent) : QObject(parent) {
t = 0;
}
void InterfaceQML::newSlot() {
emit newSignal(t);
t++;
}
//mainwindow.h
class MainWindow : public QObject {
Q_OBJECT
public:
MainWindow(QObject *parent = 0);
InterfaceQML *interface;
QQuickView *view;
QTimer *timer;
};
//mainwindow.cpp
MainWindow::MainWindow(QObject *parent) : QObject(parent) {
interface = new InterfaceQML;
timer = new QTimer;
view = new QQuickView;
qmlRegisterType<InterfaceQML>("Interface", 1, 0, "Interface");
view->setSource(QUrl("file:/main.qml"));
view->show();
QObject *pObject = view->rootObject();
connect(timer, SIGNAL(timeout()), interface, SLOT(newSlot()));
timer->start(500);
}
//main.gml
import QtQuick 2.0
import Interface 1.0
Rectangle {
width: 500
height: 500
color: "#748596"
signal sgSignal()
Text {
id: text
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
text: "World"
font.bold: true
font.pointSize: 30
color: "red"
}
Interface {
onNewSignal: {
text.text = i
}
}
}
But signal not reach QML. How fix it?
↧