undefined reference to QMessageBox
-
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:
Please help
-
"QT +=widgets" in the pro file should help.
-
@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:
-
Hi,
Run it through the debugger. That will show you where the failure happens.
-
@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 )
-
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-P-N Well the error message should be shown when the c++ method I am calling from qml fails.
-
@Pradeep-P-N I use qml for front-end and c++ as back-end
-
@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
-
Hi @Ahti
Below is sample code for MessageDialogQML 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(); }