Qt for beginners Exercise 1 basis: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Add "cleanup" tag)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
[toc align_right="yes" depth="3"]
[toc align_right="yes" depth="3"]
[[Category:Qt_for_beginners]]
[[Category:Qt_for_beginners]]

Revision as of 16:49, 3 March 2015

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

[toc align_right="yes" depth="3"]

Qt for beginners — Exercise 1 : basis

<<< Signals and slots 2 | Summary | Finding information in the documentation >>>

Note: Unfortunately, the images are no longer available. See the official "Getting Started with Qt Widgets":http://doc.qt.io/qt-5/gettingstartedqt.html page for an alternative tutorial.

Widgets

Radio button is a standard GUI component. It is often used to make a unique choice from a list. In Qt, the Doc:QRadioButton is used to create radio buttons.

Thanks to a nice heritance, a QRadioButton just behaves like a QPushButton. All properties of the QPushButton are also the same in the QRadioButton, and everything that was learnt in the second chapter can be reused here

  • text
  • icon
  • tooltip

By default, QRadioButtons are not grouped, so many of them can be checked at the same time. In order to have the "exclusive" behaviour of many radio buttons, we need to use Doc:QButtonGroup. This class can be used like this

// We allocate a new button group,
// and attached it to the parent object
// note that the parent object might be
// the main window, or "this"
QButtonGroup '''buttonGroup = new QButtonGroup(object);

// Add buttons in the button group
buttonGroup->addButton(button1);
buttonGroup->addButton(button2);
buttonGroup->addButton(button3);

What we want is to create a menu picker. In a window, a list of yummy plates should be displayed with radio buttons, and a push button that is used to select the chosen plate should be displayed.

Obviously, nothing will happen (now) when the buttons are clicked.

menu chooser

h2. Signals and slots

Here is an example about signals and slots. We are going to write an application with two buttons. The first button should display information about Qt, like in the following dialog

About Qt

We provide you the following code to complete :

#include <QApplication>
#include <QPushButton>

int main(int argc, char'''*argv)
{
 QApplication app (argc, argv);

QWidget window;
 window.setFixedSize(100, 80);

QPushButton *buttonInfo = new QPushButton("Info", &amp;window);
 buttonInfo->setGeometry(10, 10, 80, 30);

QPushButton *buttonQuit = new QPushButton("Quit", &amp;window);
 buttonQuit->setGeometry(10, 40, 80, 30);

window.show();

// Add your code here

return app.exec();
}

In order to display the information about Qt, you should use the following method

void QApplication::aboutQt();

You can also add icons on the buttons, or resize them. Obviously, the "Quit" button should be more important, so why not make it bigger ?

The answers can be found here. But we really recommend you to try and figure it out by yourself how to solve these exercises.