Monday, 16 December 2013

Write hello word Qt app on Ubuntu

Without using Qt Creator IDE, I just use my favorite editor - Emacs and command lines to create a hello wold project.

1. Make sure /usr/bin/qmake point to the one comes with installed Qt 5.2.0.
/usr/bin# mv qmake qmake_bk
/usr/bin# ln -s /home/likewise-open/CHN/shu6889/Qt5.2.0/5.2.0/gcc_64/bin/qmake ./qmake
2. Write code
create helloworld folder, add main.cc file
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
int main(int argc, char *argv[]) {
  QApplication app(argc, argv);
  QLabel hello("<center>Welcome to my first WikiHow Qt program</center>");
  hello.setWindowTitle("My First WikiHow Qt Program");
  hello.resize(400, 400);
  hello.show();
  return app.exec();
}
Above code comes from this article: http://www.wikihow.com/Create-Your-First-Qt-Program-on-Ubuntu-Linux. I changed include statements because I am using Qt 5.2.0.

3. Generate project file
In helloworld folder, run the following command:
qmake -project
Now, I get helloworld.pro file.
######################################################################
# Automatically generated by qmake (3.0) ?? 12? 16 15:49:40 2013
######################################################################
TEMPLATE = app
TARGET = helloworld
INCLUDEPATH += .
# Input
SOURCES += main.cc

4. Compile it
Just execute qmake without args again. Get Makefile file now.
qmake
make

Got a link error: -lGL, install mesa
 apt-get install libgl1-mesa-dev
But I get a few link errors now
main.o: In function `main':
main.cc:(.text.startup+0x27): undefined reference to `QApplication::QApplication(int&, char**, int)'
main.cc:(.text.startup+0x56): undefined reference to `QLabel::QLabel(QString const&, QWidget*, QFlags<Qt::WindowType>)'
main.cc:(.text.startup+0x84): undefined reference to `QWidget::setWindowTitle(QString const&)'
main.cc:(.text.startup+0xa9): undefined reference to `QWidget::resize(QSize const&)'
main.cc:(.text.startup+0xb1): undefined reference to `QWidget::show()'
main.cc:(.text.startup+0xb6): undefined reference to `QApplication::exec()'
main.cc:(.text.startup+0xc1): undefined reference to `QLabel::~QLabel()'
main.cc:(.text.startup+0xc9): undefined reference to `QApplication::~QApplication()'
main.cc:(.text.startup+0xe5): undefined reference to `QApplication::~QApplication()'
main.cc:(.text.startup+0x106): undefined reference to `QLabel::~QLabel()'
main.cc:(.text.startup+0x118): undefined reference to `QLabel::~QLabel()'
collect2: error: ld returned 1 exit status
make: *** [helloworld] Error 1

Add this line into helloworld.pro file
 QT += widgets
make it again. It works.
Run the helloworld binary, see Qt window now.

No comments:

Followers

Contributors