Getting "Hello world" to work on Qt5
-
Hi! Please forgive me, but I am a complete newbie with Qt and am having silly troubles. I have downloaded Qt 5.0.2 and installed it on my Windows XP laptop. The installation is the default, and includes MinGW. I have the book "C++ GUI Programming with Qt4" (yes, 4, not 5), and I am trying to get the first simple test program to go. The code in "hello.cpp" is:
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello Qt!");
label->show();
return app.exec();
}I have learned to make it <QtWidgets/QApplication>, not <QApplication>, etc.
I have run qmake -project to get hello.pro, which is:
######################################################################
Automatically generated by qmake (3.0) Wed 19. Jun 18:52:41 2013
######################################################################
TEMPLATE = app
TARGET = hello
INCLUDEPATH += .Input
SOURCES += hello.cpp
Then "qmake hello.pro" seems to run successfully (or at least silently).
I have learned that I need to set up a file "make.bat" that says:
@echo off
mingw32-make %*Having done that I get the following errors on running "make"
E:\hello>make
mingw32-make -f Makefile.Release
mingw32-make[1]: Entering directory 'E:/hello'
g++ -Wl,-s -Wl,-subsystem,windows -mthreads -o release\hello.exe release/hello.o -lmingw32 -lqtmain -LE:\Qt-5.0.2\5.0.2\mingw47_32\lib -lQt5Gui -lQt5Core -llibEGL -llibGLESv2 -lgdi32 -luser32
release/hello.o:hello.cpp:(.text+0x59): undefined reference to_imp___ZN12QApplicationC1ERiPPci' release/hello.o:hello.cpp:(.text+0xbe): undefined reference to _imp___ZN6QLabelC1ERK7QStringP7QWidget6QFlagsIN2Qt10WindowTypeEE' release/hello.o:hello.cpp:(.text+0xfe): undefined reference to
_imp___ZN7QWidget4showEv'
release/hello.o:hello.cpp:(.text+0x10c): undefined reference to_imp___ZN12QApplication4execEv' release/hello.o:hello.cpp:(.text+0x122): undefined reference to
_imp___ZN12QApplicationD1Ev'
release/hello.o:hello.cpp:(.text+0x1be): undefined reference to `_imp___ZN12QApplicationD1Ev'
collect2.exe: error: ld returned 1 exit status
Makefile.Release:77: recipe for target 'release\hello.exe' failed
mingw32-make[1]: *** [release\hello.exe] Error 1
mingw32-make[1]: Leaving directory 'E:/hello'
makefile:34: recipe for target 'release' failed
mingw32-make: *** [release] Error 2
I am running all this from a Command prompt on my Windows XP laptop. The PATH is:
PATH=E:\Qt-5.0.2\5.0.2\mingw47_32\bin;E:\Qt-5.0.2/Tools/MinGW\bin;C:\DOS\BIN;C:\MinGW\bin;C:\DOS\CL;C:\DOS\EMACS;C:\WINDOWS;C:\WINDOWS\SYSTEM32;C:\GNUPLOT\BIN
I would be very grateful if someone would take the time to tell me, using simple words, how I could get this test program to work. It is safe to assume that I know nothing, so please be very clear what has to be done.
Thanks!
Martin
-
Hi martin,
As a complete newb, I would make it easier on yourself and use the SDK. You can download from:
http://qt-project.org/downloads
You can choose the one with the compiler you want, and it comes with Qt Creator and will allow you to create project templates with the "new project" option.
Once you download and install it you just open QtCreator. Then do
File->new file/project --> Qt GUI application.
Then select all the defaults and you are done!
Then you can just add your label code into the main.cpp main() function and then run it. You will have the same as you are trying to do with none of that hassle.
In one minute I created a GUI app put your label code into it and had a hello world app running :)
Otherwise if you want to go on the manual path, you can still do this and then look at how the project is setup it may help you...
-
Hi Martin,
I agree with code_fodder. Using Qt Creator will be a lot easier.
[quote author="ee_martin" date="1371665147"]I have learned to make it <QtWidgets/QApplication>, not <QApplication>, etc.[/quote]<QApplication> is better than <QtWidgets/QApplication> -- it will make your code more portable in the future.
-
Hi Martin,
Some useful links:
"https://github.com/mutse/qt5-book-code":https://github.com/mutse/qt5-book-code
"Transition_from_Qt_4.x_to_Qt5":http://qt-project.org/wiki/Transition_from_Qt_4.x_to_Qt5 -
Folks,
Thanks so much for your advice. I'm on holiday right now with few facilities, but I'll be trying out your ideas next week.I have to admit I'm a bit of a control freak. You'll understand what I mean when I say that virtually all my programming is done in assembler (time-critical applications of small microcontrollers). I'd like to be able to work at the command line level, so I do want to chase that up. However, the SDK does sound like the lesser-pain route!
Let's hope that totally-out-of-my-control (Eeeek!) method works ...
Martin
-
lol...good luck :)
-
Hi,
You are missing:
@QT += widgets@
in your pro file.
Since you want to use the command line, the qmake manual will be a good starting point