CoR

Hello World in Qt using CMake and OpenEmbedded

So Qt is a really nice library, lot of functionalities with a nice architecture, so can you use it to build portable projects, and eventually embedded projects ?

A simple program

Let’s write something simple, a Hello World for example, but using a Qt-compliant way to do things Wink
You can download all files from this exemple here: HelloWorldQt .

In any C++ file, you must have a main function, but you usually want to keep this as minimal as possible. In a regular Qt program, it would only build a QApplication, a QWindow (or an inheritance) and execute it.

Here is the typical content of a main.cpp file:

#include <QtGui/QApplication>
#include "MainWindow.h"
 
int main(int argc, char *argv[])
{
 QApplication a(argc, argv);
 MainWindow w;
 w.show();
 return a.exec();
}

As you can see, it import a MainWindow.h where a MainWindow class is defined. The way to build that file depends highly on your IDE.
I never been that fond of Qt Creator, nor Visual Studio, nor anything WYSIWYG, I’m stuck with this idea since I’ve seen the output of Dreamweaver, I thinks that’s also why I like LaTeX that much.
Qt Creator, or even the Qt integration for Eclipse, are producing a nice code behind. Actually, you are not even stuck with this tools since all is based on a .ui file that is XML-based.
It might not be a bad idea to use this tools, in order to make some quick programs and look at the code produced, to have an idea on how it does things, and then to write all things by yourself.

Let’s look at the MainWindow.h:

#include <QtGui/QMainWindow>
#include <QPushButton>
#include "HelloWidget.h"
 
class MainWindow : public QMainWindow {
 Q_OBJECT
 
public:
 MainWindow(QWidget *parent = 0);
 ~MainWindow();
 
protected:
HelloWidget * _helloWidget;
QPushButton * _button;
};

The plan is to build an application with a small text, and each time you click on a button, another letter of the text appear.

You have to keep in mind that the view part of a program is a tree: a node can have one or multiple children that are nodes too. In Qt, a node inherits QWidget.
Both QPushButton and HelloWidget inherit QWidget at some degree, but the last class is actually a custom widget.

As you can see, there is weird Q_OBJECT thing in the class, this is a macro that must be in every Qt widget that you declare.

Let’s take a look at HelloWidget.h to know how that’s done:

#include <QLabel>
#include <QString>
 
class HelloWidget : public QLabel {
  Q_OBJECT
 
public:
 HelloWidget(QWidget * parent = 0);
 ~HelloWidget();
 
public slots:
 void showNewLetter();
 
protected:
 QString _msg;
 unsigned short _pos;
};

Our custom widget inherits from QLabel, a standard widget that allow text to be displayed.

The “public slots” part is not standard C++, it is related to the Qt signal/slot system, which is a powerful yet simple to use implement of the Observer pattern that allow any object to throw signals that can be cached by any other objects using a slot.
A simple example is that a button has a signal named ‘clicked’. By connecting this signal to a slot, any time you click that button, the slot will be active. More information on the Qt doc.

A QMainWindow is a bit a special kind of Widget, but the construction is still quite straightforward:

MainWindow::MainWindow(QWidget *parent) {
 // Making a central Widget for our Window
 QWidget * centralWidget = new QWidget(this);
 setCentralWidget(centralWidget);
 
 // Building our widgets
 _helloWidget = new HelloWidget(this);
 _button = new QPushButton(this);
 _button->setText("++");
 
 connect(_button, SIGNAL(clicked()), _helloWidget, SLOT(showNewLetter()));
 
 // Setting the content and disposition of our central Widget
 QVBoxLayout * layout = new QVBoxLayout();
 layout->addWidget(_helloWidget);
 layout->addWidget(_button);
 centralWidget->setLayout(layout);
}

A Layout is an object that hold the content of a widget, and that know how to display it (order, position, …).
A QMainWindow is bit special in the way that it already has a Layout, and you don’t want to replace it, because you can’t.
There is multiple parts, a MenuBar, a StatusBar, and a CentralWidget. For that simple example, we are only going to care about the last one.

The call to connect link the signal ‘clicked’ of our button to the slot ‘showNewLetter’ we defined earlier.

Let’s finish by the methods in HelloWidget.cpp:

#include "HelloWidget.h"
 
HelloWidget::HelloWidget(QWidget * parent) :
 QLabel(parent),
 _msg("Hello World !"),
 _pos(0) {
}
 
HelloWidget::~HelloWidget() {
}
 
void HelloWidget::showNewLetter() {
 _pos++;
 setText(_msg.left(_pos));
}

Yes, it’s a really simple Widget ! Each time showNewLetter is called, it shows another letter of the message.
Should I have checked if _pos doesn’t get higher than the number of letter in the string ? In C, sure, but the ‘left’ method in QString already do that for us when we extract the left part of the string. We are doing OOP, Tell Don’t Ask !

Pages: 1 2 3

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...