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 .