Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

Qt and Windows CE


An Image Viewer Example

To demonstrate how you can use Qt, I present an image viewer called "Qt Image Viewer" (Figure 1) and internationalize it for three different languages. It has its own custom look-and-feel—and users can change both the language and look-and-feel while the application is running. The complete source code for the application is available www.ddj.com/code/.

Qt comes with a UI builder tool for designing UIs. You can, of course, code the GUI by hand, but you can save time doing it in Qt Designer instead. Qt Designer stores the UI in an XML file, which generates C++ code that you can include in your project. Figure 2 shows the Qt Designer with the .ui file for Image Viewer.

For this application, I use the QLabel, QLineEdit, and QPushButton widgets (controls). I use QLabel for the actual images and for textual information (such as the name of the current image and its dimensions). I use QLineEdit to display the name of the current directory, and QPushButton for user interactions in the application. Giving the widgets sensible names like previousImage, nextButton, and currentDirectory, it is simpler later on when using these widgets in the application code.

Figure 1: Qt Image Viewer with custom style running on Windows Mobile 6.0.

[Click image to view at full size]

Figure 2: Qt Designer loaded with imageviewer.ui.

Once you define the UI, you combine it with the application code. The generated C++ code created from the .ui file in Figure 2 results in a header file called ui_imageviewer.h. To use it, just include it in the code. Here, I subclass a QWidget and initialize the GUI in the constructor:


class PreviewWindow : public QWidget
{
  Q_OBJECT
public:
  PreviewWindow() : QWidget(), current(1) {
    ui.setupUi(this);
   ...
}


The PreviewWindow class has the following members:


Ui::ImageViewer ui;
QStringList imageFileList;
QPixmap mainPixmap;
int current;

UI::ImageViewer ui is an instance of the class generated by Qt Designer. By calling ui.setupUi(this), I initialize my own class (the PreviewWindow class) with the GUI I created in Qt Designer. At this point, I can run the application. While it would look like what I designed in Qt Designer, I also need to connect the widgets with program logic to make the application functional.

When you change one widget in GUI programming, you often want another widget to be notified. Generally, you want objects of any kind to be able to communicate with one another. For example, if users click previousButton, you want PreviewWindow's previousImage() function to be called.

Qt's solution for this is "signals" and "slots" (similar to Delegates/Events in C#):

  • Signals are emitted when a particular event occurs. Qt's widgets have many predefined signals, but you can always subclass widgets to add your own signals to them.
  • Slots are functions called in response to particular signals.

Qt's widgets have many predefined slots, but it is common practice to subclass widgets and add your own slots so that you can handle the signals you're interested in. For the PreviewWindow class, I've added these slots:


void nextImage() { ... }
void previousImage() { ... }
void changeDirectory() { ... }
void toggleStyleSheet() { ... }
void setTranslator(QObject *object) { ... }


To connect the signals from the different widgets to these slots, you only need to call the connect() function in the constructor of the widget. Here, I connect the clicked() signals from previousButton and nextButton:


// connect next and previous
connect(ui.previousButton,     SIGNAL(clicked()), 
      this, SLOT(previousImage()));
connect(ui.nextButton,     SIGNAL(clicked()), 
      this, SLOT(nextImage()));


The rest of the constructor code connects the other buttons, scans the current directory for any images, and loads them for preview if any exist.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.