Error: Defining QML types from C++
-
Hi;
// messagebox.h #ifndef MESSAGEBOX_H #define MESSAGEBOX_H #include <QObject> #include <QMessageBox> class MessageBox : public QObject { Q_OBJECT Q_PROPERTY(QString message READ message WRITE set_message) public: explicit MessageBox(QObject *parent = 0); inline void set_message(QString& msg); signals: public slots: }; #endif // MESSAGEBOX_H
// messagebox.cpp #include "messagebox.h" MessageBox::MessageBox(QObject *parent) : QObject(parent) { } void MessageBox::set_message(QString& msg) { QMessageBox::information(this, tr(""), tr(msg)); }
// main.cpp #include <QGuiApplication> #include <QQmlApplicationEngine> #include <QtQuick> #include "messagebox.h" int main(int argc, char* argv[]) { QGuiApplication app(argc, argv); qmlRegisterType<MessageBox>("MessageBox", 1, 0, "MessageBox"); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec(); }
I am running this application but I get this error message:
project\path\messagebox.h:5: error: C1083: Cannot open include file: 'QMessageBox': No such file or directory
Then I am changing QMessageBox to QDebug:// messagebox.h #include <QDebug> // messagebox.cpp void MessageBox::set_message(QString& msg) { qDebug() << tr(msg); }
And I get this error message:
GCC (MingW):
project\path\main.cpp:11: error: no matching function for call to 'qmlRegisterType(const char [11], int, int, const char [11])'
qmlRegisterType<MessageBox>("MessageBox", 1, 0, "MessageBox");
^
MSVC:
project\path\main.cpp:11: error: C2974: 'qmlRegisterType' : invalid template argument for 'T', type expected
Thanks. -
Regarding "'QMessageBox': No such file or directory"
do you have
QT += widgets
in your pro file? -
Regarding "'QMessageBox': No such file or directory"
do you have
QT += widgets
in your pro file?@jsulm Yes, I do. I include Qt += widgets in .pro file.
.pro File:TEMPLATE = app QT += qml quick QT += widgets CONFIG += c++11 SOURCES += main.cpp \ messagebox.cpp RESOURCES += qml.qrc # Additional import path used to resolve QML modules in Qt Creator's code model QML_IMPORT_PATH = # Default rules for deployment. include(deployment.pri) HEADERS += \ messagebox.h