注意事项:
- 新建的c++类必须是QObject的,我这边的Qt在我新建完一个类的时候,是不会自动导入头文件的,所以需要自己导入头文件。
进入正题
当我右键 -> 添加新文件 -> C++ class
需要交互,则c++必须是个槽函数
#include <QObject> //这里需要手动导入
class UA4Qml2 : public QObject // 这里必须是QObject的子类
{
Q_OBJECT
public:
UA4Qml2(QObject *parent = 0);
signals:
public slots:
void loadUrl(); // 这里可以由qml进行调用
};
然后需要在入口函数加入以下代码
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine *engine = new QQmlApplicationEngine();
engine->rootContext()->setContextProperty("$SigDispatcher", new UA4Qml2); //这里请注意
engine->load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
然后是qml,正常调用
import QtQuick 2.5
import QtQuick.Controls 1.4
ApplicationWindow {
visible: true
width: 640
height: 480
Button{
id:loadUrl
text: "测试加载url"
onClicked: {
// 这里就是调用的地方
$SigDispatcher.loadUrl();
}
}
}
然后就可以正常响应了
话外音
- 然后我们来看一下,如果不加QObject 会报什么问题
class UA4QML{
public :
UA4QML();
};
然后在
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "ua4qml2.h"
#include "ua4qml.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine *engine = new QQmlApplicationEngine();
//这里注意是调用的UA4Qml,而非上面的UA4Qml2
//这里调用的不是QObject的子类
engine->rootContext()->setContextProperty("$SigDispatcher", new UA4QML);
engine->load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
查看报的错误
../testCurlPrj/main.cpp: In function ‘int main(int, char**)’:
../testCurlPrj/main.cpp:12:75: error: ‘QVariant::QVariant(void*)’ is private within this context
engine->rootContext()->setContextProperty("$SigDispatcher", new UA4QML);
^
In file included from /usr/include/x86_64-linux-gnu/qt5/QtCore/qlocale.h:43:0,
from /usr/include/x86_64-linux-gnu/qt5/QtGui/qguiapplication.h:46,
from /usr/include/x86_64-linux-gnu/qt5/QtGui/QGuiApplication:1,
from ../testCurlPrj/main.cpp:1:
/usr/include/x86_64-linux-gnu/qt5/QtCore/qvariant.h:471:12: note: declared private here
inline QVariant(void *) Q_DECL_EQ_DELETE;
^~~~~~~~
../testCurlPrj/main.cpp:12:75: error: use of deleted function ‘QVariant::QVariant(void*)’
engine->rootContext()->setContextProperty("$SigDispatcher", new UA4QML);
^
In file included from /usr/include/x86_64-linux-gnu/qt5/QtCore/qlocale.h:43:0,
from /usr/include/x86_64-linux-gnu/qt5/QtGui/qguiapplication.h:46,
from /usr/include/x86_64-linux-gnu/qt5/QtGui/QGuiApplication:1,
from ../testCurlPrj/main.cpp:1:
/usr/include/x86_64-linux-gnu/qt5/QtCore/qvariant.h:471:12: note: declared here
inline QVariant(void *) Q_DECL_EQ_DELETE;
^~~~~~~~
其实,我表示我看的时候,特别的头大。报错说:‘QVariant::QVariant(void*)’ is private within this context
看的一顿晕眩。恶心。
-
如果我们不放到槽函数里面会怎样
#ifndef UA4QML2_H #define UA4QML2_H #include <QObject> class UA4Qml2 : public QObject { Q_OBJECT public: UA4Qml2(QObject *parent = 0); void loadUrl(); //这里不是槽函数 signals: public slots: }; #endif // UA4QML2_H
查看报的错误:
qrc:/main.qml:14: TypeError: Property 'loadUrl' of object UA4Qml2(0x564d6d5a3c40) is not a function
看着这个错误,还是有点意思的,不是一个方法。
-
我们不加头文件
#include <QObject>
#ifndef UA4QML2_H #define UA4QML2_H //#include <QObject> //这里被我注释掉了 class UA4Qml2 : public QObject { Q_OBJECT public: UA4Qml2(QObject *parent = 0); void loadUrl(); signals: public slots: }; #endif // UA4QML2_H
看下报的错误
In file included from ../testCurlPrj/ua4qml2.cpp:1:0: ../testCurlPrj/ua4qml2.h:6:1: error: expected class-name before ‘{’ token { ^ ../testCurlPrj/ua4qml2.h:7:5: error: ‘Q_OBJECT’ does not name a type Q_OBJECT ^~~~~~~~ ../testCurlPrj/ua4qml2.h:12:1: error: ‘signals’ does not name a type signals: ^~~~~~~ ../testCurlPrj/ua4qml2.cpp:4:17: error: expected constructor, destructor, or type conversion before ‘(’ token UA4Qml2::UA4Qml2(QObject *parent) : QObject(parent) ^ Makefile:480: recipe for target 'ua4qml2.o' failed
额,看到这样的错误,也是一顿抽搐,不知所云。话说可能我是java转过来的,所以看上去就不是特别的友好,说好的c++ 是最好的语言呢?