PDA

View Full Version : Problem in studying the official Qt book



franky
30th October 2016, 15:25
Hello all,

I'm studying C++ GUI Programming using Qt 4 second edition book. It's apparently the only official book of Qt but it has many defects, for example, it talks about the programs but it doesn't tell us how to create them!

Do you have this book? If so, please have a look at the Hello program of the first chapter and the programs of the second chapter, like finddialog and GoToCell. How to make those programs run please?
I'm using Qt creator 3.6.0.

Thanks in advance.

kbsk007
30th October 2016, 16:48
This book is written for Qt 4.xx and you are using Qt 5.xx
To get run this example follow next step:

1. create a directory Hello

2. copy the files
(hello.pro)


TEMPLATE = app
SOURCES = hello.cpp

-and-
(hello.cpp)


#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();
}

-in this directory.

3. in Creator change the Settings: Tools->Create and Run->Kits->User Defined click on Desktop and change
Qt-Version: from Qt 5.x in Qt 4.x

anda_skoa
30th October 2016, 16:52
You can alternatively build with Qt5 if you add this line to the .pro file


QT += widgets


Cheers,
_

kbsk007
30th October 2016, 17:03
anda_skoa
adding QT += widgets in *.pro, does it work for all Qt 4 programs?

Radek
30th October 2016, 17:11
Simply run hello.exe :)

If I guess correctly, your problem consists in creating the profile file for your project. Therefore:
(1) Open your Creator.
(2) Select a new project.
(3) Select a Qt widget app.
(4) Add a new file to the project.
(5) Select a .cpp file (say hello.cpp)
(6) You get an empty file, perhaps with some pre-typed defines. You can delete all that and enter the contents of hello.cpp from your book.
(7) Save
(8) Build
(9) Run :) Your exe is the hello.exe in your debug or release directory of the hello project. You can run from the creator, you can run the hello.exe directly from the directory.

See some tutorial on creating a project in Qt (they are many on the internet). Learn what is a profile file (the .pro file), what is it for, where you find it in your project and how to create/update it yourself. You need to understand this if you want to write projects in Qt.

anda_skoa
30th October 2016, 19:18
adding QT += widgets in *.pro, does it work for all Qt 4 programs?
It is a necessary adjustment for programs that use QtWidgets, because QtWidgets was part of the QtGui library in Qt4 and moved to its own library with Qt5.

It is not necessary for programs that don't use widgets.

There are also other changes that might be necessary as some API changed between Qt4 and Qt5.

Cheers,
_

kbsk007
30th October 2016, 21:55
anda_skoa thanks