Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Error: Defining QML types from C++

Error: Defining QML types from C++

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.2k Views
  • 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 Offline
    I Offline
    Ibrahim
    wrote on last edited by
    #1

    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
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • jsulmJ jsulm

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

        I Offline
        I Offline
        Ibrahim
        wrote on last edited by
        #3

        @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
        0
        • I Offline
          I Offline
          Ibrahim
          wrote on last edited by
          #4

          The problem continues.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved