0
我想連接一個C++類到QML,但我面臨一個問題,編譯時出現以下錯誤。設置QML上下文失敗
我加入了一個圖像顯示錯誤:
我用一個簡單的類只是爲了測試,如果我的代碼工作,這裏是代碼 testing.h:
#ifndef TESTING_H
#define TESTING_H
class Testing
{
public:
Testing();
void trying();
};
#endif // TESTING_H
和testing.cpp:
#include "testing.h"
#include <iostream>
using namespace std;
Testing::Testing()
{
}
void Testing::trying()
{
cout<<"hello"<<endl;
}
和main.cpp中:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "testing.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QQmlContext* context= engine.rootContext();
Testing a;
context->setContextProperty("test",&a);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
和main.qml:
import QtQuick 2.5
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
MouseArea{
anchors.fill: parent
onClicked: test.tryout();
}
}
什麼是試用? – eyllanesc
從發短信的方法,對不起,我沒有提到 –
看到我的回答請 – eyllanesc