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. undefined reference to QMessageBox

undefined reference to QMessageBox

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 2.0k 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.
  • AhtiA Offline
    AhtiA Offline
    Ahti
    wrote on last edited by Ahti
    #1

    Hi
    I am trying to show simple error message with QMessageBox but I keep getting this error:
    undefined reference to `_imp___ZN11QMessageBoxD1Ev'

    My Code:

    #include <QtWidgets/QMessageBox>
    
    QMessageBox msg;
    msg.setWindowTitle("Error");
    msg.setIcon(QMessageBox::Critical);
    msg.setText("Error: Something went wrong!");
    msg.exec();
    

    Error:

    0_1560272897359_QMessageBox.PNG

    Please help

    what is a signature ?? Lol

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      "QT +=widgets" in the pro file should help.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      AhtiA 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

        "QT +=widgets" in the pro file should help.

        AhtiA Offline
        AhtiA Offline
        Ahti
        wrote on last edited by
        #3

        @Christian-Ehrlicher Okay I added widgets in pro file and this is how it looks now:

        pro file:

        TEMPLATE = app
        
        QT += qml quick
        QT += widgets
        
        CONFIG += c++11
        ...
        

        But when I run my project I get this error now:

        0_1560281156659_runtime error.PNG

        what is a signature ?? Lol

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Run it through the debugger. That will show you where the failure happens.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          AhtiA 1 Reply Last reply
          3
          • SGaistS SGaist

            Hi,

            Run it through the debugger. That will show you where the failure happens.

            AhtiA Offline
            AhtiA Offline
            Ahti
            wrote on last edited by Ahti
            #5

            @SGaist Its says Cannot create a QWidget without QApplication so I changed this:

            #include <QGuiApplication>
            int main(int argc, char *argv[])
            {
                QGuiApplication app(argc, argv);
                ...
            }
            

            to this:

            #include <QApplication>
            int main(int argc, char *argv[])
            {
                QApplication app(argc, argv);
                ...
            }
            

            It works but now I get a message on console saying: ReferenceError: adjust is not defined ( adjust is the object of some class which I use in qml to access its method )

            what is a signature ?? Lol

            Pradeep P NP 1 Reply Last reply
            0
            • AhtiA Ahti

              @SGaist Its says Cannot create a QWidget without QApplication so I changed this:

              #include <QGuiApplication>
              int main(int argc, char *argv[])
              {
                  QGuiApplication app(argc, argv);
                  ...
              }
              

              to this:

              #include <QApplication>
              int main(int argc, char *argv[])
              {
                  QApplication app(argc, argv);
                  ...
              }
              

              It works but now I get a message on console saying: ReferenceError: adjust is not defined ( adjust is the object of some class which I use in qml to access its method )

              Pradeep P NP Offline
              Pradeep P NP Offline
              Pradeep P N
              wrote on last edited by Pradeep P N
              #6

              Hi @Ahti

              Your intention is to show the QMessageBox on the QML UI ? If not correct me.

              If you have the QML UI why dont you use the MessageDialog.

              As per my understanding,

              • QCoreApplication is the base class, QGuiApplication extends the base class with functionality related to handling windows and GUI (non widget, e.g. OpenGL or QtQuick, QML),
              • QApplication extends QGuiApplication with functionality related to handling widgets.

              All the best.

              Pradeep Nimbalkar.
              Upvote the answer(s) that helped you to solve the issue...
              Keep code clean.

              AhtiA 1 Reply Last reply
              3
              • Pradeep P NP Pradeep P N

                Hi @Ahti

                Your intention is to show the QMessageBox on the QML UI ? If not correct me.

                If you have the QML UI why dont you use the MessageDialog.

                As per my understanding,

                • QCoreApplication is the base class, QGuiApplication extends the base class with functionality related to handling windows and GUI (non widget, e.g. OpenGL or QtQuick, QML),
                • QApplication extends QGuiApplication with functionality related to handling widgets.

                All the best.

                AhtiA Offline
                AhtiA Offline
                Ahti
                wrote on last edited by Ahti
                #7

                @Pradeep-P-N Well the error message should be shown when the c++ method I am calling from qml fails.

                what is a signature ?? Lol

                Pradeep P NP 1 Reply Last reply
                0
                • AhtiA Ahti

                  @Pradeep-P-N Well the error message should be shown when the c++ method I am calling from qml fails.

                  Pradeep P NP Offline
                  Pradeep P NP Offline
                  Pradeep P N
                  wrote on last edited by
                  #8

                  @Ahti Fine, The UI you have is of QML or QWidget ?

                  Pradeep Nimbalkar.
                  Upvote the answer(s) that helped you to solve the issue...
                  Keep code clean.

                  AhtiA 1 Reply Last reply
                  0
                  • Pradeep P NP Pradeep P N

                    @Ahti Fine, The UI you have is of QML or QWidget ?

                    AhtiA Offline
                    AhtiA Offline
                    Ahti
                    wrote on last edited by Ahti
                    #9

                    @Pradeep-P-N I use qml for front-end and c++ as back-end

                    what is a signature ?? Lol

                    Pradeep P NP 1 Reply Last reply
                    0
                    • AhtiA Ahti

                      @Pradeep-P-N I use qml for front-end and c++ as back-end

                      Pradeep P NP Offline
                      Pradeep P NP Offline
                      Pradeep P N
                      wrote on last edited by Pradeep P N
                      #10

                      @Ahti Cool.

                      Just wrote a small example. Which brings the QMessageBox on launch.

                      As you use QML front-end the Best solution would be prefer you to use the MessageDialog which react for your C++ function.

                          MessageDialog {
                              id: messageDialog
                      
                              title: "May I have your attention please"
                              text: "It's so cool that you are using Qt Quick."
                      
                      // Option 1
                              visible: adjust.functionCall() // functionCall() returns bool.
                      
                              onAccepted: {
                                  console.log("And of course you could only agree.")
                                  Qt.quit()
                              }
                          }
                      
                      // Option 2
                      
                      you can even set messageDialog.visible = true/false from other components or signals on functionCall from C++
                      
                      

                      Please check this and let me know if its helpful.

                      // .pro file
                      
                      QT += quick core gui widgets
                      
                      // main.cpp
                      
                      #include <QGuiApplication>
                      #include <QQmlApplicationEngine>
                      #include <QApplication>
                      #include <QQmlContext>
                      
                      #include "MyClass.h"
                      
                      int main(int argc, char *argv[])
                      {
                          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
                      
                          QApplication app(argc, argv);
                          QQmlApplicationEngine engine;
                      
                          // Registers the C++ type in the QML
                          // qmlRegisterType<MyClass>("MyClass", 1, 0, "Adjust");
                      
                          MyClass myclass;
                          engine.rootContext()->setContextProperty("adjust", &myclass);
                      
                          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
                          if (engine.rootObjects().isEmpty())
                              return -1;
                      
                          return app.exec();
                      }
                      
                      // main.qml
                      
                      import QtQuick 2.9
                      import QtQuick.Window 2.2
                      
                      // If qmlRegisterType is used
                      // import MyClass 1.0
                      
                      Window {
                          visible: true
                          width: 640
                          height: 480
                          title: qsTr("Hello World")
                      
                          // If qmlRegisterType is used
                          // Adjust { }
                      
                          Component.onCompleted: {
                              adjust.name = "My Name";
                      
                              console.log(adjust.name)
                          }
                      }
                      
                      // MyClass. cpp 
                      
                      #include "MyClass.h"
                      #include <QMessageBox>
                      
                      MyClass::MyClass(QObject *parent) : QObject(parent)
                      {    
                          QMessageBox msg;
                          msg.setWindowTitle("Error");
                          msg.setIcon(QMessageBox::Critical);
                          msg.setText("Error: Something went wrong!");
                          msg.exec();
                      }
                      
                      QString MyClass::name() const
                      {
                          return m_name;
                      }
                      
                      void MyClass::setName(const QString &name)
                      {
                          m_name = name;
                      
                          emit nameChanged();
                      }
                      
                      
                      // MyClass.h
                      
                      #ifndef MYCLASS_H
                      #define MYCLASS_H
                      
                      #include <QObject>
                      
                      class MyClass : public QObject
                      {
                          Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
                          Q_OBJECT
                      public:
                          explicit MyClass(QObject *parent = nullptr);
                      
                          QString name() const;
                          void setName(const QString &name);
                      
                      signals:
                          void nameChanged();
                      
                      public slots:
                      
                      private:
                          QString m_name;
                      
                      };
                      
                      #endif // MYCLASS_H
                      
                      

                      Pradeep Nimbalkar.
                      Upvote the answer(s) that helped you to solve the issue...
                      Keep code clean.

                      1 Reply Last reply
                      1
                      • Pradeep P NP Offline
                        Pradeep P NP Offline
                        Pradeep P N
                        wrote on last edited by Pradeep P N
                        #11

                        Hi @Ahti
                        Below is sample code for MessageDialog

                        QML side:

                        import QtQuick 2.9
                        import QtQuick.Window 2.2
                        import QtQuick.Dialogs 1.3
                        
                        Window {
                            visible: true
                            width: 640
                            height: 480
                            title: qsTr("Hello World")
                        
                            MessageDialog {
                                id: messageDialog
                                title: "May I have your attention please"
                                text: "It's so cool that you are using Qt Quick."
                        
                                visible: adjust.Adjust
                        
                                onAccepted: {
                                    console.log("And of course you could only agree.")
                                }
                            }
                        }
                        

                        on the C++ side :

                         MyClass myclass;
                            engine.rootContext()->setContextProperty("adjust", &myclass);
                        
                        
                        // Here i am just exposing the Q_PROPERTY
                        
                            Q_PROPERTY(bool Adjust READ Adjust WRITE setAdjust NOTIFY AdjustChanged)
                        
                        
                        // You can change it as per your requirement.
                        
                        MyClass::MyClass(QObject *parent)
                            : QObject(parent)
                            , m_Adjust(false)
                        {    
                            QTimer *_timer = new QTimer(this);
                        
                            _timer->start(3000);
                        
                            connect(_timer, &QTimer::timeout, [this] {
                                setAdjust(!m_Adjust);
                            });
                        
                        }
                        
                        
                        bool MyClass::Adjust() const
                        {
                            return m_Adjust;
                        }
                        
                        void MyClass::setAdjust(bool Adjust)
                        {
                            if (Adjust == m_Adjust)
                                return;
                            m_Adjust = Adjust;
                        
                            emit AdjustChanged();
                        }
                        

                        Pradeep Nimbalkar.
                        Upvote the answer(s) that helped you to solve the issue...
                        Keep code clean.

                        1 Reply Last reply
                        2

                        • Login

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