我想在QML接收到类发送的信号
所以使用了qmlRegisterType
代码如下:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
//注册类到QML
qmlRegisterType<mTimer>("myTimer", 1,0, "MTimer");
QQmlEngine engine;
mTimer m_timer;
engine.rootContext()->setContextProperty("m_timer", &m_timer);
QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/main.qml")));
component.create();
return app.exec();
}
类的代码:
class mTimer : public QObject
{
Q_OBJECT
public:
explicit mTimer(QObject *parent = 0);
signals:
void whenTimeChanged(const QString &timeStr);
public slots:
void onStartBtnClicked();
void onStopBtnClicked();
void onTimerTimerOut();
}
QML
MTimer{
onWhenTimeChanged: timeText.text = timeStr
}
已经对照书和百度上的教程好多遍了,感觉没有错误
但是编译的时候要么就提示component is not ready 或者不提示这个错误,但是接收不到信号
请问是为什么?