无法连接 qml 信号和 c++ 插槽
can't connect qml signal and c++ slot
我正在尝试将 QML 信号连接到 C++ 插槽。但是当我继续使用 Qt guide 时,出现了两个错误:
file:///home/muhammad/Documents/qt_projects/build-Plaq-Desktop_Qt_5_8_0_GCC_64bit-Debug/qrc:/main.qml:
No such file or directory
QObject::connect: Cannot connect (null)::qmlSignal(QString) to
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();
}
我该如何解决?
不幸的是这个例子不准确
试试这个:
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")));
如果你想连接一些嵌套对象信号,然后使用findChild
获得适当的 item
我正在尝试将 QML 信号连接到 C++ 插槽。但是当我继续使用 Qt guide 时,出现了两个错误:
file:///home/muhammad/Documents/qt_projects/build-Plaq-Desktop_Qt_5_8_0_GCC_64bit-Debug/qrc:/main.qml: No such file or directory
QObject::connect: Cannot connect (null)::qmlSignal(QString) to 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();
}
我该如何解决?
不幸的是这个例子不准确
试试这个:
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")));
如果你想连接一些嵌套对象信号,然后使用findChild
获得适当的 item