A quick recipe on how to get started with Qt in Ubuntu Linux using a simple example program.
1. Obtain QT4
Run these from the command line:
1 2 | $ sudo apt-get update $ sudo apt-get install libqt4-dev qt4-qmake cmake r-base-dev libcurl4-gnutls-dev |
2. Create and edit the qmake project file
To generate and edit the project file itself use something like:
1 2 3 | $ mkdir qt $ cd qt/ $ cat > main.pro & |
This example project file, main.pro, is essentially a set of declarations required by qmake to enable you to build your application.
Using a text editor of your choice, just copy this into your main.pro text file and save:
1 2 3 4 5 6 7 8 | TEMPLATE = app SOURCES = main.cpp HEADERS = CONFIG += qt warn_on release TARGET = main QT += gui QT += widgets CONFIG += qt |
3. Create the example application
First chttp://www.informit.com/articles/article.aspx?p=412354&seqNum=4reate your sample main.cpp. I often prefer the cat
command to create new files:
1 | $ cat > main.cpp & |
And using your own Linux-based text editor of choice, create this simple Qt example and save as main.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <QtWidgets> #include <QtCore> #include <QLabel> int main( int argc, char *argv[] ) { QApplication *my= new QApplication( argc, argv ); QMainWindow x; QString str = "Hello" ; QLabel *label = new QLabel( str, 0 ); x.setCentralWidget( label ); x.show(); return my->exec(); } |
4. Use qmake to create the example project
1 | $ qmake -o Makefile main.pro |
5. Build and run the project
1 2 | $ make $ ./main |
Giving you your first Qt dialog: