PDA

View Full Version : Qt4 help please



Mirge
June 13th, 2009, 03:06 AM
http://doc.qtsoftware.com/4.5/how-to-learn-qt.html

Quote: The best way to learn Qt is to read the official Qt book, C++ GUI Programming with Qt 4, Second Edition (http://www.amazon.com/gp/product/0132354160/ref=ase_trolltech/) (ISBN 0-13-235416-0). This book provides comprehensive coverage of Qt programming all the way from "Hello Qt" to advanced features such as multithreading, 2D and 3D graphics, networking, item view classes, and XML. (The first edition, which is based on Qt 4.1, is available online (http://www.qtrac.eu/C++-GUI-Programming-with-Qt-4-1st-ed.zip).)

So, I went to Barnes & Noble, shelled out the $60 + tax for it... started reading through it, and the very first example doesn't work. *Probably* my fault, but I am not sure what's wrong.



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


Errors:


g++ -Wall -g -o "qt1" "qt1.cpp" (in directory: /home/mike/cpp)
qt1.cpp:2:24: error: QApplication: No such file or directory
qt1.cpp:3:18: error: QLabel: No such file or directory
qt1.cpp: In function ‘int main(int, char**)’:
qt1.cpp:9: error: ‘QApplication’ was not declared in this scope
qt1.cpp:9: error: expected `;' before ‘app’
qt1.cpp:10: error: ‘QLabel’ was not declared in this scope
qt1.cpp:10: error: ‘label’ was not declared in this scope
qt1.cpp:10: error: expected type-specifier before ‘QLabel’
qt1.cpp:10: error: expected `;' before ‘QLabel’
qt1.cpp:12: error: ‘app’ was not declared in this scope
Compilation failed.

Mirge
June 13th, 2009, 03:12 AM
I installed the SDK, which I thought included everything I needed... building the framework from source just in case I was mistaken.

Mirge
June 13th, 2009, 03:50 AM
Wow I thought ./configure took a long time. I'm *still* waiting for make to finish.

gorilla
June 13th, 2009, 05:36 AM
g++ -Wall -g -o "qt1" "qt1.cpp"
is not right. Read your book again, I'm sure they tell you how to correctly build Qt4 code.
The easiest way is:


# inside your project dir:
qmake -project
qmake
make

Mirge
June 13th, 2009, 05:52 AM
Thanks, I just started reading it... like I said, it was probably my fault. I noticed that Qt Creator generated code that was different from what the book is having me type out... kinda makes me worry that the book is already out-dated, but I guess I'll find out shortly. I wouldn't think it's out-dated since Qt recommends it still as the best way to learn.

One of the appendixes explains the install for various platforms.. I'm running sudo make install now. make took ~1.5hr..

Mirge
June 13th, 2009, 05:57 AM
is not right. Read your book again, I'm sure they tell you how to correctly build Qt4 code.
The easiest way is:


# inside your project dir:
qmake -project
qmake
make

Awesome :). Thanks a bunch, gorilla! My first Qt4 app!



#include <QApplication>
#include <QLabel>

using namespace std;

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello, Qt!");
label->show();
return app.exec();
}

gorilla
June 13th, 2009, 06:13 AM
I noticed that Qt Creator generated code that was different from what the book is having me type out... kinda makes me worry that the book is already out-dated,
Don't worry, Qt Creator's code skeletons are just a bit different but equivalent style, and add some real-life boilerplate code the book leaves out for clarity. Shouldn't matter for you for now, just replace the autogenerated skeleton code with the book's code.