<div class=”clearfix post-header”></div>
I am new to QT. I am writing a demo C++ program integrating to QT. It got the module is not installed error all the time, even I tried several tricks from internet post.
<div class=”content”>
Here is the code:
————— myclass.h
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QObject>
#include <QString>
class MyClass : public QObject
{
Q_OBJECT
public:
explicit MyClass(QObject *parent = 0);
Q_INVOKABLE QString sayHello() const;
};
#endif // MYCLASS_H
———— myclass.cpp
#include “myclass.h”
MyClass::MyClass(QObject *parent)
: QObject(parent)
{
}
QString MyClass::sayHello() const
{
return “Hello!”;
}
—————- main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include <QQmlContext>
#include <QQmlEngine>
#include “myclass.h”
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
qmlRegisterType<MyClass>(“com.mycompany.myapplication”, 1, 0, “MyClass”);
QQmlApplicationEngine engine;
MyClass myClass;
engine.rootContext()->setContextProperty(“myClass”, &myClass);
engine.load(QUrl(QLatin1String(“qrc:/main.qml”)));
return app.exec();
}
—————- main.qml
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import com.mycompany.myapplication 1.0
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr(“Hello World”)
Text {
anchors.centerIn: parent
text: myClass.sayHello()
}
}
—————— myclass.pro
QT += quick
CONFIG += c++11
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES +=
main.cpp
myclass.cpp
RESOURCES += qml.qrc
QML_IMPORT_PATH =
QML_DESIGNER_IMPORT_PATH =
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
<h2>HEADERS +=
myclass.h</h2>
<h2>Appreciate for any advice…..</h2>
</div>