QT 1: Construire des GUI en C++ MASSAVIOL Mathieu BRESSAND Jérémy 13 octobre 2015
1 Introduction Présentation de Qt Qt en entreprise Modules pour créer des interfaces graphiques avec Qt Qt Quick Qt GUI Qt Widgets 2 Classe mère QObject Slots et signaux Organiser l'agencement de ses widgets avec les layouts QAbstractButton Recueillir des inputs
Présentation de Qt API orientée objet première version en 1995 framework multiplateforme Qt GUI, Qt Widgets, Qt Network, Qt SQL... Qt Creator (IDE)
Qt en entreprise Entreprises utilisant Qt Adobe Archos Boeing Google la Nasa Logiciels éveloppés avec Qt Adobe Photoshop Elements Google Earth Skype
Le module Qt Quick 1 Présentation API QML API C++ 2 Langage QML
Le module Qt QUI 1 Application Bureau 2 Dessin 2D QGuiApplication QWindow QEvent 3 Intégration OpenGL 4 Qt GUI avant Qt 5.0
Le module Qt Widgets 1 Les widgets 2 La gestion des événements 3 Choisir Qt Widgets
La classe mère QObject
La classe QWidget QWidget hérite de deux classes : QObject pour le côté "mécanique"(gestion des évènements) QPaintDevice pour le côté graphique
Les méthodes sur les widgets Méthodes de QWidget bool isvisible() const void move(int x, int y) void resize(int w, int h) void setfont(const QFont &) QWidget* parentwidget() const...
Les objets façon Qt
Connexion signal slot Liaison signal/slot Lier le signal d'un objet au slot d'un autre(ou lui même) permet de dénir l'action qui sera réalisée sur l'objet 2 quand l'objet 1 reçoit le signal.
Code code QObject::connect(m_bouton, SIGNAL(clicked()), application, SLOT(quit()));
La classe QLayout Les sous-classes de QLayout QHBoxLayout et QVBoxLayout QGridLayout QFormLayout
La classe QHBoxLayout 1 #i n c l u d e <Q A p p l i c a t i o n > #i n c l u d e <QPushButton> 3 #i n c l u d e <QHBoxLayout> i n t main ( i n t argc, c h a r a r g v [ ] ) 5 { Q A p p l i c a t i o n app ( argc, a r g v ) ; 7 QWidget f e n e t r e ; 9 QPushButton bouton1 = new QPushButton ( "1" ) ; QPushButton bouton2 = new QPushButton ( "2" ) ; 11 QPushButton bouton3 = new QPushButton ( "3" ) ; 13 QHBoxLayout l a y o u t = new QHBoxLayout ; 15 l a y o u t >addwidget ( bouton1 ) ; l a y o u t >addwidget ( bouton2 ) ; 17 l a y o u t >addwidget ( bouton3 ) ; 19 f e n e t r e. s e t L a y o u t ( l a y o u t ) ; f e n e t r e. show ( ) ; 21 r e t u r n app. e x e c ( ) ; 23 } Listing 1 Utilisation de la classe QHBoxLayout
La classe QVBoxLayout 1 #i n c l u d e <Q A p p l i c a t i o n > #i n c l u d e <QPushButton> 3 #i n c l u d e <QVBoxLayout> 5 i n t main ( i n t argc, c h a r a r g v [ ] ) { 7 Q A p p l i c a t i o n app ( argc, QWidget f e n e t r e ; a r g v ) ; 9 QPushButton bouton1 = new QPushButton ( "1" ) ; 11 QPushButton bouton2 = new QPushButton ( "2" ) ; QPushButton bouton3 = new QPushButton ( "3" ) ; 13 QVBoxLayout l a y o u t = new QVBoxLayout ; 15 l a y o u t >addwidget ( bouton1 ) ; 17 l a y o u t >addwidget ( bouton2 ) ; l a y o u t >addwidget ( bouton3 ) ; 19 f e n e t r e. s e t L a y o u t ( l a y o u t ) ; 21 f e n e t r e. show ( ) ; 23 r e t u r n app. e x e c ( ) ; } Listing 2 Utilisation de la classe QVBoxLayout
La classe QGridLayout 1 #i n c l u d e <Q A p p l i c a t i o n > #i n c l u d e <QPushButton> 3 #i n c l u d e <QGridLayout> 5 i n t main ( i n t argc, c h a r a r g v [ ] ) { 7 Q A p p l i c a t i o n app ( argc, QWidget f e n e t r e ; a r g v ) ; 9 QPushButton bouton1 = new QPushButton ( "1" ) ; 11 QPushButton bouton2 = new QPushButton ( "2" ) ; QPushButton bouton3 = new QPushButton ( "3" ) ; 13 QGridLayout l a y o u t = new QGridLayout ; 15 l a y o u t >addwidget ( bouton1, 0, 0) ; 17 l a y o u t >addwidget ( bouton2, 0, 1) ; l a y o u t >addwidget ( bouton3, 1, 0, 1, 2) ; 19 21 f e n e t r e. s e t L a y o u t ( l a y o u t ) ; f e n e t r e. show ( ) ; 23 r e t u r n app. e x e c ( ) ; 25 } Listing 3 Utilisation de la classe QGridLayout
La classe QFormLayout #i n c l u d e <Q A p p l i c a t i o n > 2 #i n c l u d e <QLineEdit > #i n c l u d e <QFormLayout> 4 6 i n t main ( i n t argc, c h a r a r g v [ ] ) { 8 Q A p p l i c a t i o n app ( argc, QWidget f e n e t r e ; a r g v ) ; 10 12 QLineEdit nom = new QLineEdit ; QLineEdit prenom = new QLineEdit ; QLineEdit age = new QLineEdit ; 14 QFormLayout l a y o u t = new QFormLayout ; 16 l a y o u t >addrow ( " Votre nom", nom) ; 18 l a y o u t >addrow ( " Votre prenom ", prenom ) ; l a y o u t >addrow ( " Votre age ", age ) ; 20 f e n e t r e. s e t L a y o u t ( l a y o u t ) ; 22 f e n e t r e. show ( ) ; 24 r e t u r n app. e x e c ( ) ; } Listing 4 Utilisation de la classe QFormLayout
La classe QAbstractButton Les sous-classes de QAbstractButton QPushButton QCheckBox QRadioButton
La classe QCheckBox #i n c l u d e <Q A p p l i c a t i o n > 2 #i n c l u d e <QtWidgets> 4 i n t main ( i n t argc, c h a r a r g v [ ] ) { 6 Q A p p l i c a t i o n app ( argc, QWidget f e n e t r e ; a r g v ) ; 8 QGroupBox groupbox = new QGroupBox ( "A quoi jouez vous?", & f e n e t r e ) ; 10 QCheckBox l o l = new QCheckBox ( " League o f l e g e n d ", &f e n e t r e ) ; 12 QCheckBox h e a r t h = new QCheckBox ( " H e a r t h s t o n e ", &f e n e t r e ) ; QCheckBox hero = new QCheckBox ( " Heroes o f the storm ", & f e n e t r e ) ; 14 QVBoxLayout vbox = new QVBoxLayout ; 16 vbox >addwidget ( l o l ) ; 18 vbox >addwidget ( h e a r t h ) ; vbox >addwidget ( h e r o ) ; 20 groupbox >s e t L a y o u t ( vbox ) ; 22 groupbox >move ( 5, 5) ; groupbox >r e s i z e ( 2 0 0, 150) ; 24 f e n e t r e. show ( ) ; 26 r e t u r n app. e x e c ( ) ; 28 } Listing 5 Utilisation de la classe QHBoxLayout
La classe QRadioButton 1 #i n c l u d e <Q A p p l i c a t i o n > #i n c l u d e <QtWidgets> 3 i n t main ( i n t argc, c h a r a r g v [ ] ) 5 { Q A p p l i c a t i o n app ( argc, a r g v ) ; 7 QWidget f e n e t r e ; 9 QGroupBox groupbox = new QGroupBox ( " Votre j e u p r e f e r e?", & f e n e t r e ) ; 11 QRadioButton b r o f o r c e = new QRadioButton ( " B r o f o r c e " ) ; QRadioButton r o c k e t = new QRadioButton ( " Rocket League " ) ; 13 QRadioButton s o u l = new QRadioButton ( " S o u l c a l i b u r " ) ; 15 s o u l >s e t C h e c k e d ( t r u e ) ; 17 QVBoxLayout vbox = new QVBoxLayout ; 19 vbox >addwidget ( b r o f o r c e ) ; vbox >addwidget ( r o c k e t ) ; 21 vbox >addwidget ( s o u l ) ; 23 groupbox >s e t L a y o u t ( vbox ) ; groupbox >move ( 5, 5) ; 25 groupbox >r e s i z e ( 2 0 0, 150) ; 27 f e n e t r e. show ( ) ; 29 r e t u r n app. e x e c ( ) ; } Listing 6 Utilisation de la classe QVBoxLayout
Sources http ://doc.qt.io/qt-5/index.html https ://openclassrooms.com/courses/programmez-avec-lelangage-c/introduction-a-qt https ://fr.wikipedia.org/wiki/qt http ://qt.developpez.com/