注意事項:
- 新建的c++類必須是QObject的枕磁,我這邊的Qt在我新建完一個類的時候储笑,是不會自動導入頭文件的瞻讽,所以需要自己導入頭文件追迟。
進入正題
當我右鍵 -> 添加新文件 -> C++ class
qt新建QObject.png
需要交互脆荷,則c++必須是個槽函數(shù)
#include <QObject> //這里需要手動導入
class UA4Qml2 : public QObject // 這里必須是QObject的子類
{
Q_OBJECT
public:
UA4Qml2(QObject *parent = 0);
signals:
public slots:
void loadUrl(); // 這里可以由qml進行調(diào)用
};
然后需要在入口函數(shù)加入以下代碼
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蛋逾,正常調(diào)用
import QtQuick 2.5
import QtQuick.Controls 1.4
ApplicationWindow {
visible: true
width: 640
height: 480
Button{
id:loadUrl
text: "測試加載url"
onClicked: {
// 這里就是調(diào)用的地方
$SigDispatcher.loadUrl();
}
}
}
然后就可以正常響應(yīng)了
話外音
- 然后我們來看一下集晚,如果不加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();
//這里注意是調(diào)用的UA4Qml,而非上面的UA4Qml2
//這里調(diào)用的不是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
看的一頓暈眩。惡心亏钩。
-
如果我們不放到槽函數(shù)里面會怎樣
#ifndef UA4QML2_H #define UA4QML2_H #include <QObject> class UA4Qml2 : public QObject { Q_OBJECT public: UA4Qml2(QObject *parent = 0); void loadUrl(); //這里不是槽函數(shù) 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轉(zhuǎn)過來的踏枣,所以看上去就不是特別的友好,說好的c++ 是最好的語言呢钙蒙?