我试图将QML信号连接到C ++插槽。但是当我使用Qt guide继续前进时,会出现两个错误:
文件:///家/穆罕默德/文档/ qt_projects /集结Plaq-Desktop_Qt_5_8_0_GCC_64bit-调试/ QRC:/main.qml: 没有这样的文件或目录
QObject :: connect:无法连接(null):: qmlSignal(QString) PlaqueFinder :: cppSlot(QString的)
这是我在main.cpp中的代码:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <plaquefinder.h>
#include <QQuickView>
#include <QString>
#include <QObject>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
QQuickView view(QUrl::fromLocalFile("qrc:/main.qml"));
QObject* item = (QObject*)view.rootObject();
PlaqueFinder plaq;
QObject::connect(item, SIGNAL(qmlSignal(QString)),
&plaq, SLOT(cppSlot(QString)));
return app.exec();
}
我该如何解决?
答案 0 :(得分:0)
遗憾的是,这个例子不准确
试试这个:
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
QObject* item = (QObject*)engine.rootObjects()[0];
PlaqueFinder plaq;
QObject::connect(item, SIGNAL(qmlSignal(QString)), &plaq, SLOT(cppSlot(QString)));
return app.exec();
}
通过这一行您将创建您的QQuickView,并且无需通过加载相同的qml再次执行此操作。
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
如果您想连接一些nestet对象信号,请使用findChild
得到适当的item