Simple Qt program builds but doesn't show output - qt

I have just started learning Qt and tried to compile and run a simple program of hello world. The program builds without any issues and gives this output in compiler output
Starting: /qtbuild/bin/qmake /home/ved/Qt/train1/train1.pro -spec /qtbuild/mkspecs/qws/linux-arm-g++ -r CONFIG+=debug
Exited with code 0.
Starting: /usr/bin/make -w
make: Entering directory `/home/ved/Qt/train1'
make: Nothing to be done for `first'.
make: Leaving directory `/home/ved/Qt/train1'
Exited with code 0.
but on trying to run the program, it only displays this:
Starting /home/ved/Qt/train1/train1...
/home/ved/Qt/train1/train1 exited with code 255
My code:
#include
#include
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QLabel *label = new QLabel("Hello World!!!");
label->show();
return a.exec();
}
I am completely new to Qt building procedure and can't understand what is wrong.
Update
tried changing QCoreApplication to QApplication. No change.
Running build steps for project train1...
Starting: /qtbuild//bin/qmake /home/ved/Qt/train1/train1.pro -spec /qtbuild/mkspecs/qws/linux-arm-g++ -r CONFIG+=debug
Exited with code 0.
Starting: /usr/bin/make -w
make: Entering directory `/home/ved/Qt/train1'
arm-linux-g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -DQT_SHARED -I/qtbuild/mkspecs/qws/linux-arm-g++ -I. -I/qtbuild/include/QtCore -I/qtbuild/include/QtNetwork -I/qtbuild/include/QtGui -I/qtbuild/include -I. -I/usr/local/tslib-arm/include -o main.o main.cpp
In file included from /qtbuild/include/QtCore/qobject.h:48,
from /qtbuild/include/QtCore/qiodevice.h:46,
from /qtbuild/include/QtCore/qxmlstream.h:45,
from /qtbuild/include/QtCore/QtCore:3,
from main.cpp:1:
/qtbuild/include/QtCore/qstring.h:91: note: the mangling of 'va_list' has changed in GCC 4.4
arm-linux-g++ -Wl,-rpath,/qtbuild/lib -o train1 main.o -L/usr/local/tslib-arm/lib -L/qtbuild//lib -lQtGui -L/qtbuild//lib -L/usr/local/tslib-arm/lib -lQtNetwork -lQtCore -lpthread
make: Leaving directory `/home/ved/Qt/train1'
Exited with code 0.
I use Qt 4.6.3.

If you want a QLabel to display, you need to run the GUI application class QApplication, not QCoreApplication.

You should tell Qt, that you want to build project with GUI. Open your's project .pro file and change line
QT += ...
to
QT += core gui
example, .pro file:
QT += core gui
TARGET = untitled1
TEMPLATE = app
SOURCES += main.cpp
main.cpp:
#include <QtGui/QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QLabel lbl("hello world");
lbl.show();
return a.exec();
}

You need to create a window if you want to display a label. Basically something like this (not tested):
QMainWindow* win = new QMainWindow();
QLabel *label = new QLabel(win, "Hello World!!!");
label->show();
win->show();

change QCoreApplication to QApplication an add a Main Window
QApplication a(argc, argv);
QMainWindow* mainWin = new QMainWindow();
QLabel *label = new QLabel(mainWin, "Hello World!!!");
mainWin->setCentralWidget(label);
mainWin->show();

You must set in project configuration that you are compiling Qt GUI application. Using of QApplication instead QCoreApplication is not enough. I don't know your IDE, so i can't provide "howto" - but i belive that you will easly find necessary options. For eapmle, in MSVC, you set neccessary application type (console or GUI) during creation of a project.
Also - exit code 255 shows on some error. Exit code must be zero, exept cases, when you manually change it.

Try to unclick Shadow build in your Project/Build properties.

I having the same problem. Let it to be restart QT. Surely it works

Related

Why doesn't qmake add "QT += widgets" to *.pro files automatically?

I am trying to build a simple "Hello World" example in QT5.
I am compiling the code using the below steps
qmake-qt5 -project
qmake-qt5
make
I am getting the below error
main.cpp:1:24: fatal error: QApplication: No such file or directory
#include <QApplication>
^
compilation terminated.
When I read QT forums, its mentioned to add "QT += widgets" to the *.pro file. After doing this the code is compiled.
Question: Why do I need to add "QT += widgets" to the *.pro file manually ? Why doesn't qmake do it automatically ?
Note: I am using Ubuntu
Code
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello Qt!");
label->show();
return app.exec();
}
The QT variable in the .pro file is used to specify the modules which are used in the project.
And as the qmake manual mentions:
By default, QT contains both core and gui, ensuring that standard GUI applications can be built without further configuration.
The Qt Widgets module is not linked by default and has thus to be specified in the .pro file with QT += widgets.

Qt linker errors Qmake + Makefile

I want to build a Qt project outside Qt Creator, so I'm using qmake to generate a Makefile on the following project file:
TEMPLATE = app
TARGET = test
INCLUDEPATH += . \
/usr/include/qt5/QtWidgets/
QMAKE_CXXFLAGS += --std=c++11
# Input
SOURCES += test.cc
Which was generated also by qmake, bar the c++11 flag and the second include path. The Makefile contains link paths to the Qt library
LIBS = $(SUBLIBS) -L/usr/X11R6/lib64 -lQt5Gui -L/usr/lib/x86_64-linux-gnu -lQt5Core -lG L -lpthread
What's weird in the above is that I don't have a /usr/X11R6 folder. Instead, libQt5Gui.so is located in /usr/lib/x86_64-linux-gnu, so I'm a little puzzled where the X11R6 comes from.
Anyway, this is my linker output:
test.cc:(.text.startup+0x20): undefined reference to `QApplication::QApplication(int&, char**, int)'
test.cc:(.text.startup+0x25): undefined reference to `QApplication::exec()'
test.cc:(.text.startup+0x2f): undefined reference to `QApplication::~QApplication()'
test.cc:(.text.startup+0x43): undefined reference to `QApplication::~QApplication()'
The above is the result of building the following source:
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
return app.exec();
}
When I try to build the project file in Qt Creator, the same error appears. Am I missing libraries? Or is something configured erroneously?
(I'm on Ubuntu 14.04, and I just installed the qtcreator package from the repo's, assuming that all development libraries would be installed along with it.)
As stated in the docs, you need to include the widget library to use QApplication.
Add this to your project file:
QT += widgets
If you're not going to build a GUI app, use QCoreApplication instead. It doesn't have that dependency.

Qt 5.3.1: Why do I need to set include path and libs for widgets

I'm on Windows 8.1. I want to build Qt apps from the command line like the Unix trooper I usually am. Well, actually, I want things to be a little more automated! After wondering why qmake would not generate the proper Makefiles and hand-editing them, I finally realized I could just add on to the Qt variable in the Qt .pro file. (Been since about Qt 3-something that I've Qt'ed, so finally the light-bulb went on :)
This is the simplest of applications to help refresh the dead brain cells.
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello Qt!");
label->show();
return app.exec();
}
The .pro file:
######################################################################
# Automatically generated by qmake (3.0) Sat Aug 23 16:38:04 2014
######################################################################
TEMPLATE = app
TARGET = HelloQt
INCLUDEPATH += .
#why do I need these
INCLUDEPATH += "C:\Qt\Qt5.3.1\5.3\msvc2013_64\include\qtwidgets"
LIBS += "C:\Qt\Qt5.3.1\5.3\msvc2013_64\lib\Qt5widgets.lib"
#why does the following line do nothing
qmake: Qt += widgets
CONFIG -= X86
# Input
SOURCES += hello.cpp
Why do I need to specify the widgets INCLUDEPATH and LIBS to get a successful build?
As far as I can tell, Qt += widgets is not a valid addition to the Qt variable.
Why not? If I don't specify the Qt5Widgets include and libs, and I run nmake, I get errors related to not finding the include for QApplication and/or linker errors.
With those two lines in the .pro file everything works fine. Has anybody run into this sort of thing, am I missing something?
The line should look more like QT += core gui widgets. This will set the INCLUDEPATH and LIBS.

netbeans: how to add widgets to QT variable

I have seen posts in which people say that Qt 5 projects are now required to add widgets to QT variable and this is why simple Qt example in netbeans doesn't work: cannot include QtGui/QApplication -> no such file or directory.
how to add this to QT variable? do I have to do it for each project in IDE or in .bashrc or somehowe?
the problem was because of link error. it was because it didn't link with QtWidgets.
g++ -m64 -Wl,-rpath,/opt/Qt5.0.1/5.0.1/gcc_64 -Wl,-rpath,/opt/Qt5.0.1/5.0.1/gcc_64/lib -o dist/Debug/GNU-Linux-x86/QtApplication_2 build/Debug/GNU-Linux-x86/main.o -L/usr/X11R6/lib64 -L/opt/Qt5.0.1/5.0.1/gcc_64/lib -lQt5Gui -lQt5Core -lGL -lpthread
first I have checked that using something from QtCore still works. the code was:
#include <QtCore/QCoreApplication>
int main(int argc, char *argv[]) {
// initialize resources, if needed
// Q_INIT_RESOURCE(resfile);
QCoreApplication a(argc, argv);
// create and show your widgets here
return a.exec();
}
and it was fine, so I knew it is only the linkage error.
I still didn't know what to add, as you don't have QtWidget in Qt tab on project Properties in Netbeans. but trial and error showed that it is enough to check the QtOpenGl on this tab (Modules), then it is linked against more libs:
g++ -m64 -Wl,-rpath,/opt/Qt5.0.1/5.0.1/gcc_64 -Wl,-rpath,/opt/Qt5.0.1/5.0.1/gcc_64/lib -o dist/Debug/GNU-Linux-x86/QtApplication_1 build/Debug/GNU-Linux-x86/main.o -L/usr/X11R6/lib64 -L/opt/Qt5.0.1/5.0.1/gcc_64/lib -lQt5OpenGL -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread

QApplication: No such file or directory

I installed QT4 Creator in /usr/programs/qt , I add to PATH /usr/programs/qt/bin,
QTDIR=/usr/programs/qt,LD_LIBRARY_PATH=$QTDIR/lib, and also for MANPATH and export. Problem is that demo examples work fine, but when I create new project in other directory for example /home/Jane/ it doesn't work, I got errors like
/home/Jane/test-build-desktop/../test/main.cpp:1:
error: QApplication: No such file or
directory
/home/Jane/test-build-desktop/../test/main.cpp:2:
error: QLabel: No such file or
directory
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello Qt!");
label->show();
return app.exec();
}
Can anybody help me ?
Add to your .pro file:
QT += gui
I've the same problem. in my ".pro" file it was
QT -= gui
then I changed it to
QT += gui
and the problem solved
QApplication: No such file or directory ...
try to add
QT += widgets
at your .pro file. I had the same message... It looks like from 5.0 Qt-based applications does not like widgets by default...
The solution works for me, Qt 5.7
After added the following line to your .pro file:
QT += widgets
Right-click on your Qt Project and click "Run qmake"
Run qmake
After this when you re-complie your project, everything should be fine.
If your .pro file has this line:
QT -= gui
you need to delete it. It tells that the gui module to be removed from your app.
You can enable it by typing
QT += gui
but actually it is not needed since the gui module is enabled by default.
For Ubuntu 14.04 if you get the same error:
ABC$ make
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -fPIE -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -I. -I. -I/usr/include/qt5 -I/usr/include/qt5/QtGui -I/usr/include/qt5/QtCore -I. -o textpad.o textpad.cpp
textpad.cpp:1:24: fatal error: QApplication: No such file or directory
#include <QApplication>
^
compilation terminated.
make: *** [textpad.o] Error 1
Try qmake-qt4 and then make. Of course you can get all the QT4 libraries if its not present using:
sudo apt-get install libqt4-dev

Resources