我正在尝试学习如何将Qt快速对象信号连接到c ++代码中的类中的插槽。当我将类定义添加到由系统创建的“qtquick2applicationviewer.h”文件时,一切正常。 但我找不到添加我自己的.h文件的方法(我应该按照.h文件中的说明执行此操作:“建议不要修改此文件,因为较新版本的Qt Creator可能会提供更新版本的它。”) 我尝试将源添加到.pro文件(HEADERS + = classes.h),也尝试将其添加到.pri文件(HEADERS + = $$ PWD /../ classes.h)但没有运气。
我得到的错误是链接器错误: LNK2001:未解析的外部sybol“public:virtual struct QMetaObject const * __thiscall MyClass :: metaObject(void)const”(?metaObject @ Myclass @@ UBEPBUQMetaObject @@ XZ) 还有两个相似的。
对我做错的任何意见都非常感谢:) 可能可以帮助许多其他人,这也是基于QTQuick教程应用程序的一般性问题。
//classes.h
#ifndef CLASSES_H
#define CLASSES_H
#include <QObject>
#include <QVariant>
class MyClass : public QObject
{
Q_OBJECT
public:
MyClass(QObject *QMLObject) : m_QMLObject(QMLObject) {}
public slots:
void handleClick() {
m_QMLObject->setProperty("text", "Qt was here");
}
protected:
QObject *m_QMLObject;
};
#endif // CLASSES_H
//main.cpp
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include "classes.h"
#include <QtQuick>
#include <QtDebug>
void MyClassInQtQhfile::cppSlot(const QString &msg) {
qDebug() << "Called the C++ slot with message:" << msg;
}
int main(int argc, char *argv[])
{
QTextStream out(stdout);
QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("qml/Transitions/main.qml"));
viewer.showExpanded();
QObject *rectangle = viewer.rootObject()->findChild<QObject*>("rectangle");
QObject *text = viewer.rootObject()->findChild<QObject*>("text");
MyClassInQtQhfile *myClassInQtQhfile = new MyClassInQtQhfile();
MyClass *myclass = new MyClass(text);
QObject::connect(rectangle, SIGNAL(qmlSignal(QString)),
myClassInQtQhfile, SLOT(cppSlot(QString)));
QObject::connect(rectangle, SIGNAL(qmlSignal(QString)),
myclass, SLOT(handleClick()));
//rectangle is the object name, which needs to be define in qml for the component.e.g. objectName: "rectangle"
return app.exec();
}
//QML object sending signal
Rectangle {
objectName: "rectangle"
id: bottomLeftRect
width: 64
height: 64
color: "#00000000"
radius: 6
anchors.left: parent.left
anchors.leftMargin: 10
anchors.bottom: parent.bottom
anchors.bottomMargin: 20
border.color: "#808080"
signal qmlSignal(string msg)
MouseArea {
id: mousearea3
anchors.fill: parent
onClicked:{
bottomLeftRect.qmlSignal("Hello from QML")
onClicked: page.state = 'State2'
}
}
}
qtquick2applicationviewer.pri的开头
# checksum 0x21c9 version 0x90005
# This file was generated by the Qt Quick 2 Application wizard of Qt Creator.
# The code below adds the QtQuick2ApplicationViewer to the project and handles
# the activation of QML debugging.
# It is recommended not to modify this file, since newer versions of Qt Creator
# may offer an updated version of it.
QT += qml quick
SOURCES += $$PWD/qtquick2applicationviewer.cpp
HEADERS += $$PWD/qtquick2applicationviewer.h
HEADERS += $$PWD/../classes.h
INCLUDEPATH += $$PWD
...
.pro文件
# Add more folders to ship with the application, here
folder_01.source = qml/Transitions
folder_01.target = qml
DEPLOYMENTFOLDERS = folder_01
# Additional import path used to resolve QML modules in Creator's code model
QML_IMPORT_PATH =
# The .cpp file which was generated for your project. Feel free to hack it.
SOURCES += main.cpp
# Installation path
# target.path =
# Please do not modify the following two lines. Required for deployment.
include(qtquick2applicationviewer/qtquick2applicationviewer.pri)
qtcAddDeployment()
HEADERS += \
classes.h