QMLにC++クラスを接続しようとしていますが、問題が発生しています。コンパイル時に次のエラーが表示されます。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