Qt World Summit: Submit your Presentation

Error: Defining QML types from C++

  • Hi;

    // messagebox.h
    #ifndef MESSAGEBOX_H
    #define MESSAGEBOX_H
    #include <QObject>
    #include <QMessageBox>
    class MessageBox : public QObject
      Q_PROPERTY(QString message READ message WRITE set_message)
      explicit MessageBox(QObject *parent = 0);
      inline void set_message(QString& msg);
    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;
      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");

    project\path\main.cpp:11: error: C2974: 'qmlRegisterType' : invalid template argument for 'T', type expected

  • Lifetime Qt Champion

    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 \
    RESOURCES += qml.qrc
    # Additional import path used to resolve QML modules in Qt Creator's code model
    # Default rules for deployment.
    HEADERS += \

  • The problem continues.

Log in to reply