Qt project doesn't compile, undefined reference to - c++

Main.cpp
#include <QApplication>
int main (int argc, char* argv[]) {
QApplication app(argc, argv);
return app.exec();
}
test.pro
SOURCES += \
main.cpp
greaterThan(QT_MAJOR_VERSION,4) : QT +=widgets
outputs :
I can't compile my project because of "undefined reference to", it seems that my compiler doesn't find "QApplication" but i don't know how to solve it.
I unistall and reinstall my Qt but it didn't fix it.

Related

How to use mpfr/gmp in Qt-Creator properly?

Hello I want to create a Qt5 project using Qt-creator and want to use mpfr/gmp so I need how to configure the project.
because if i compile I get these errors:
#include "mainwindow.h"
#include <QApplication>
#include <stdio.h>
#include <gmp.h>
#include <mpfr.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
mpfr_t x, y, z, t;
mpfr_inits2 (256, x, y, z, t, (mpfr_ptr) 0);
return a.exec();
}
The output:
error: undefined reference to `mpfr_inits2'
But on codeblocks I add the include path and library path and add the flags -lgmp -lmpfr to the compiler and works fine.
In QtCreator, open the .pro file of your project and append the following line:
unix: LIBS += -lmpfr -lgmp
Alternatively, you can also use the UI to do this: In the "Projects" list, right-click on your project, select "Add library" > "System Library". In the "Library file" field add e.g. /usr/lib/mpfr.so. QtCreator will then turn this into -lmpfr, as shown in the "Summary" view. Repeat these steps to add /usr/lib/libgmp.so as well.

Getting error : Must construct a QGUIApplication first

I am developing a GUI application, but whenever I am trying to close the application, it throws an error that "Must construct QGuiapplication first". My main is not returning exit code 0, so it's not exiting normally. I think some destructor is getting called twice but need some help here. I am attaching main.cpp code here for reference.
#include <QGuiApplication>
#include <QFontDatabase>
#include <QtWebEngine>
#include "ApplicationManager.h"
#include "AppLogger.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);
QGuiApplication app(argc, argv);
QtWebEngine::initialize();
app.setApplicationName("MCS3.0");
QFontDatabase::addApplicationFont(":/Fonts/Roboto.ttf");
#ifdef VERSION
app.setApplicationVersion(QString("%1").arg(VERSION));
logInfoMessage(app.applicationName()+app.applicationVersion()+" Started");
#endif
ApplicationManager::instance().run();
return app.exec();
}
The relevant part of the problem is inside ApplicationManager.h which was not exposed by OP.
I bet that it makes another instance of QApplication (or QGUIApplication or QCoreApplication).
How can I know this? It's partly a guess (as the name looks like) and partly result of the following test:
testQApp.cc:
#include <QtWidgets>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
{ QApplication app(argc, argv);
QLabel qLbl("The app in app");
qLbl.show();
app.exec();
}
return app.exec();
}
testQApp.pro:
SOURCES = testQApp.cc
QT = widgets
Compiled and tested in cygwin64 on Windows 10:
$ qmake-qt5 testQApp.pro
$ make
$ ./testQApp
When I quit the application, the issue occurs:
QApplication::exec: Please instantiate the QApplication object first
Segmentation fault (core dumped)
$
To make this complete, the relevant paragraph of doc. about QApplication:
For any GUI application using Qt, there is precisely one QApplication object, no matter whether the application has 0, 1, 2 or more windows at any given time. For non-QWidget based Qt applications, use QGuiApplication instead, as it does not depend on the QtWidgets library.
Please, note that the emphasize is not done by me.

Undefined reference to QDeclarativePropertyMap in qt

I am new to Qt.
Qml is facing issue while passing QDeclarativePropertyMap from c++ to Qml.
Here is my code:
main.cpp
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QtDeclarative/qdeclarativepropertymap.h>
#include <QVariant>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQmlApplicationEngine engine;
///giving error in below line
QDeclarativePropertyMap *m_propertymap = new QDeclarativePropertyMap();
m_propertymap->insert("time" ,QVariant(QString("yet to do")));
m_propertymap->insert("area" ,QVariant(QString("india")));
engine.rootContext()->setContextProperty("M_propertymap", m_propertymap);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
Error:
undefined reference to QDeclarativePropertyMap QObject
Use QtQml/QQmlPropertyMap instead
undefined reference to QDeclarativePropertyMap QObject
is a linker error, cause you trying to mix different versions of qtquick

mcvs qwt - must construct a QApplication before a QPaintDevice

I would like to ask if you could help me with my app.
I have an application in C++ using Qt (and Qwt) in MCVS 2010. I want to open QDialog window with QwtPlot on button click from Main Window (QMainWindow). Here is some code:
MainWindow.cpp
void MainWindow::on_pushButton_1_clicked ()
{
Dialog_plot dp;
dp.setModal(true);
dp.exec();
}
Dialog_plot.cpp:
#include <qwt_plot.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_curve.h>
#include "Dialog_plot.h"
Dialog_plot::Dialog_plot(QWidget *parent)
{
plot = new QwtPlot();
//more code...
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
This code is compiling, but when I click on that pushbutton_1 in my application I'm getting error:
Must construct a QApplication before a QPaintDevice
I know that such error was discussed many times and I was reading a lot about it but I can't see solution for my problem.
One more thing I would like to mention - I have similar application with Qwt plot written by somebody else and his application compiles and works without any problems in my MCVS. I was trying to compare Linker/libraries included but it seems to be the same. So I guess there is a problem with my application, I just can't solve it. I really need some help!

Undefined reference to XOpenDisplay in a Qt project

Now I am feeling quite stupid. I am trying to do some stuff with xlib in Qt Creator.
My code:
#include <QtCore/QCoreApplication>
#include <X11/Xlib.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Display *display = XOpenDisplay(NULL);
return 0;
}
Just one line of code and gives me:
/main.cpp:8: undefined reference to `XOpenDisplay'
It is defined in Xlib.h as
extern Display *XOpenDisplay(
_Xconst char* /* display_name */
);
I feel I am missing something very basic.
I've figured it out.
Adding -lX11 to the the Makefile solved this issue.
#КодСерфинг145 I added LIBS += -lX11 to the make file (the .pro file)
Adding Additional arguments to Build steps inside Projects did not work for me either and neither did QMAKE_CXXFLAGS += -lX11 like many suggests.