Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Error: Defining QML types from C++

    General and Desktop
    2
    4
    957
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • I
      Ibrahim last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion last edited by

        Regarding "'QMessageBox': No such file or directory"
        do you have
        QT += widgets
        in your pro file?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        I 1 Reply Last reply Reply Quote 0
        • I
          Ibrahim @jsulm last edited by

          @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
          
          1 Reply Last reply Reply Quote 0
          • I
            Ibrahim last edited by

            The problem continues.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post