IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Qt Discussion :

[Qt] Appliquation du design pattern etat


Sujet :

Qt

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    17
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 17
    Par défaut [Qt] Appliquation du design pattern etat
    Bonjour à tous/toutes!

    J'aimerai appliquer le design pattern à une application Qt, me permettant après un clique sur un bouton spécifique, de changer l'intérieur de la fenêtre pour un tout autre aspect avec d'autres Widgets.

    Pour cela, j'ai issue une classe personnelle MyWidget de QWidget:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    //MYWIDGET_H:
     
    #ifndef MYWIDGET_H
    #define MYWIDGET_H
     
    #include "ClassUi.hpp"
     
    class MyWidget : public QWidget
    {
    Q_OBJECT
    public:
        MyWidget(QWidget *parent = 0);
        Ui_Form *EtatUi;
    private slots:
          void slotQuitter();
          void slotChangeDesignPattern1();
          void slotChangeDesignPattern2();
          void slotInformation();
    };
     
    #endif
     
     
    //MYWIDGET_CPP:
     
    #include "MyWidget.hpp"
     
    MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
    {
         EtatUi = new Ui_Etat1;
         EtatUi->setupUi(this);
         connect(EtatUi->pushButton, SIGNAL(clicked()), this, SLOT(slotInformation()));
         connect(EtatUi->pushButton_2, SIGNAL(clicked()), this, SLOT(slotChangeDesignPattern2()));
         connect(EtatUi->pushButton_3, SIGNAL(clicked()), qApp, SLOT(quit()));
    }
     
    void MyWidget::slotChangeDesignPattern1()
    {
         delete EtatUi;
         EtatUi = new Ui_Etat1;
         EtatUi->setupUi(this);
         connect(EtatUi->pushButton, SIGNAL(clicked()), this, SLOT(slotInformation()));
         connect(EtatUi->pushButton_2, SIGNAL(clicked()), this, SLOT(slotChangeDesignPattern2()));
         connect(EtatUi->pushButton_3, SIGNAL(clicked()), qApp, SLOT(quit()));
    }
     
    void MyWidget::slotChangeDesignPattern2()
    {
         delete EtatUi;
         EtatUi = new Ui_Etat2;
         EtatUi->setupUi(this);
         connect(EtatUi->pushButton_4, SIGNAL(clicked()), this, SLOT(slotChangeDesignPattern1()));
         connect(EtatUi->pushButton_5, SIGNAL(clicked()), qApp, SLOT(quit()));
    }
     
    void MyWidget::slotInformation()
    {
         QMessageBox mb("Informations","QMessage affichée. Contient les informations.",
         QMessageBox::Information, QMessageBox::Ok, QMessageBox::Default,
         QMessageBox::Default | QMessageBox::Default);
         mb.setButtonText(QMessageBox::Ok, "Fermer");
         mb.exec();
    }
     
     
    void MyWidget::slotQuitter()
    {
        qApp->quit();
    }
    Puis j'en demande l'éxecution et l'affichage (rien de spécial jusque là) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    //MAIN_CPP:
     
    #include "MyWidget.hpp"
     
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        MyWidget widget;
        widget.show();
        return app.exec();
    }
    Puis j'ai "codé" (avec uic en console) une classe issue de deux design que j'ai déclinée grace au polymorphisme (afin de choisir quel design insérer dans ma fenêtre).

    Dans MyWidget le design est contenu par "Ui_Form *EtatUi;":

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    //CLASSUI_HPP:
     
    #ifndef CLASSUI_H
    #define CLASSUI_H
     
     
    #include <QtCore/QVariant>
    #include <QtGui/QAction>
    #include <QtGui/QApplication>
    #include <QtGui/QButtonGroup>
    #include <QtGui/QPushButton>
    #include <QtGui/QWidget>
    #include <QtGui/QMessageBox>
    #include <QtGui/QLineEdit>
    #include <QtGui/QTextEdit>
     
    class Ui_Form
    {
    public:
     
        QPushButton *pushButton;
        QPushButton *pushButton_2;
        QPushButton *pushButton_3;
        QLineEdit *lineEdit;
        QLineEdit *lineEdit_2;
        QLineEdit *lineEdit_3;
        // /*-/*-/*-/*-/*-/*-/-*/-*/-*/*-/*-/-*/
        QTextEdit *textEdit;
        QPushButton *pushButton_4;
        QPushButton *pushButton_5;
        QLineEdit *lineEdit_4;
     
        Ui_Form() {}
     
        virtual ~Ui_Form() 
        {
        delete pushButton;
        delete pushButton_2;
        delete pushButton_3;
        delete lineEdit;
        delete lineEdit_2;
        delete lineEdit_3;
        delete pushButton_4;
        delete pushButton_5;
        delete textEdit;
        delete lineEdit_4;
        }
        virtual void setupUi(QWidget *Form) = 0;
        virtual void retranslateUi(QWidget *Form) = 0;
     
    };
     
    class Ui_Etat1 : public Ui_Form
    {
    public:
     
        Ui_Etat1() {
        }
     
        void setupUi(QWidget *Form)
        {
        Form->setObjectName(QString::fromUtf8("Form"));
        Form->resize(QSize(516, 165).expandedTo(Form->minimumSizeHint()));
        pushButton_2 = new QPushButton(Form);
        pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
        pushButton_2->setGeometry(QRect(10, 20, 131, 71));
        lineEdit = new QLineEdit(Form);
        lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
        lineEdit->setGeometry(QRect(10, 110, 501, 31));
        lineEdit_3 = new QLineEdit(Form);
        lineEdit_3->setObjectName(QString::fromUtf8("lineEdit_3"));
        lineEdit_3->setGeometry(QRect(162, 70, 221, 20));
        lineEdit_2 = new QLineEdit(Form);
        lineEdit_2->setObjectName(QString::fromUtf8("lineEdit_2"));
        lineEdit_2->setGeometry(QRect(310, 30, 191, 20));
        pushButton = new QPushButton(Form);
        pushButton->setObjectName(QString::fromUtf8("pushButton"));
        pushButton->setGeometry(QRect(180, 20, 81, 31));
        pushButton_3 = new QPushButton(Form);
        pushButton_3->setObjectName(QString::fromUtf8("pushButton_3"));
        pushButton_3->setGeometry(QRect(410, 60, 81, 31));
        retranslateUi(Form);
        QMetaObject::connectSlotsByName(Form);
        } // setupUi
     
        void retranslateUi(QWidget *Form)
        {
        Form->setWindowTitle(QApplication::translate("Form", "Design 1", 0, QApplication::UnicodeUTF8));
        pushButton_2->setText(QApplication::translate("Form", "Change To Form2", 0, QApplication::UnicodeUTF8));
        lineEdit->setText(QApplication::translate("Form", "You are here in the Form1 Design, to change of Design, click on PushButton named \"Change To Form2\"", 0, QApplication::UnicodeUTF8));
        lineEdit_3->setText(QApplication::translate("Form", "If you want to quit, click on this PushButton", 0, QApplication::UnicodeUTF8));
        lineEdit_2->setText(QApplication::translate("Form", "This PushButton open a QMessageBox", 0, QApplication::UnicodeUTF8));
        pushButton->setText(QApplication::translate("Form", "Information", 0, QApplication::UnicodeUTF8));
        pushButton_3->setText(QApplication::translate("Form", "QUITTER", 0, QApplication::UnicodeUTF8));
        Q_UNUSED(Form);
        } // retranslateUi
     
    };
     
    class Ui_Etat2 : public Ui_Form
    {
    public:
        Ui_Etat2() {}
     
        void setupUi(QWidget *Form)
        {
        Form->setObjectName(QString::fromUtf8("Form"));
        Form->resize(QSize(338, 148).expandedTo(Form->minimumSizeHint()));
        pushButton_4 = new QPushButton(Form);
        pushButton_4->setObjectName(QString::fromUtf8("pushButton_4"));
        pushButton_4->setGeometry(QRect(10, 20, 131, 71));
        textEdit = new QTextEdit(Form);
        textEdit->setObjectName(QString::fromUtf8("textEdit"));
        textEdit->setGeometry(QRect(150, 13, 181, 81));
        pushButton_5 = new QPushButton(Form);
        pushButton_5->setObjectName(QString::fromUtf8("pushButton_5"));
        pushButton_5->setGeometry(QRect(250, 110, 81, 31));
        lineEdit_4 = new QLineEdit(Form);
        lineEdit_4->setObjectName(QString::fromUtf8("lineEdit_4"));
        lineEdit_4->setGeometry(QRect(10, 120, 221, 20));
        retranslateUi(Form);
        QMetaObject::connectSlotsByName(Form);
        } // setupUi
     
        void retranslateUi(QWidget *Form)
        {
        Form->setWindowTitle(QApplication::translate("Form", "Design 2", 0, QApplication::UnicodeUTF8));
        pushButton_4->setText(QApplication::translate("Form", "Change To Form1", 0, QApplication::UnicodeUTF8));
        textEdit->setHtml(QApplication::translate("Form", "<html><head><meta name=\"qrichtext\" content=\"1\" /></head><body style=\" white-space: pre-wrap; font-family:MS Shell Dlg 2; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;\"><p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:8pt;\">You are here in the Form2 Design, to change of Design, click on PushButton named \"Change To Form1\"</p></body></html>", 0, QApplication::UnicodeUTF8));
        pushButton_5->setText(QApplication::translate("Form", "QUITTER", 0, QApplication::UnicodeUTF8));
        lineEdit_4->setText(QApplication::translate("Form", "If you want to quit, click on this PushButton", 0, QApplication::UnicodeUTF8));
        Q_UNUSED(Form);
        } // retranslateUi
     
    };
     
    #endif //CLASSUI_H
    Ainsi tout marche parfaitement en ce qui concerne le dessinage du design et sa supression mais pas la reconstruction du second design.

    Pourriez-vous m'aider?

    A l'aide de QMessageBox j'ai vu que l'éxecution de toutes les méthodes se faisait bien, ainsi que la redimension de la fenêtre.

    PS: je vous évite le fichier mocMyWidget.cpp issu de MyWidget.hpp par l'utilitaire moc .

  2. #2
    Rédacteur

    Avatar de Matthieu Brucher
    Profil pro
    Développeur HPC
    Inscrit en
    Juillet 2005
    Messages
    9 810
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations professionnelles :
    Activité : Développeur HPC
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2005
    Messages : 9 810
    Par défaut
    Tu lui as demandé de redessiner la fenêtre ?

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    17
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 17
    Par défaut
    Comment faire?

    EDIT: j'ai tenté avec update() et repaint(), sa compile mais ne résoud pas mon problème.

  4. #4
    Rédacteur

    Avatar de Matthieu Brucher
    Profil pro
    Développeur HPC
    Inscrit en
    Juillet 2005
    Messages
    9 810
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations professionnelles :
    Activité : Développeur HPC
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2005
    Messages : 9 810
    Par défaut
    update()

  5. #5
    Membre averti
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    17
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 17
    Par défaut
    Désolé j'ai pas vu ton message.

    J'ai déjà tenté le update() et même le repaint(), mais aucun des deux ne change mon problème!

    Cela vient d'ailleurs que l'idée de redessiner?

    EDIT: j'ai placé l'instruction "update();" à la fin de mes deux slots slotChangeDesignPattern[...].

  6. #6
    Rédacteur

    Avatar de Matthieu Brucher
    Profil pro
    Développeur HPC
    Inscrit en
    Juillet 2005
    Messages
    9 810
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations professionnelles :
    Activité : Développeur HPC
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2005
    Messages : 9 810
    Par défaut
    Bizarre... J'avais une fois eu le souci, c'était un update qui manquait.

Discussions similaires

  1. Quel design Pattern a appliquer?
    Par saimmmms dans le forum Langages de programmation
    Réponses: 0
    Dernier message: 14/07/2014, 15h17
  2. Quel design pattern appliquer?
    Par publicStaticVoidMain dans le forum Design Patterns
    Réponses: 7
    Dernier message: 14/08/2009, 13h41
  3. Réponses: 4
    Dernier message: 24/02/2009, 12h06
  4. Les Designs Patterns Entreprise
    Par boulon dans le forum Design Patterns
    Réponses: 4
    Dernier message: 01/09/2004, 19h16
  5. [Design Patterns] Architecture 3 tiers
    Par HPJ dans le forum Design Patterns
    Réponses: 1
    Dernier message: 29/07/2003, 11h49

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo