1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29
30#include <QComboBox>
31#include <QDebug>
32#include <QLineEdit>
33#include <QMainWindow>
34#include <QVBoxLayout>
35#include <QWizard>
36#include <QWizardPage>
37
38#include <QtTest/QtTest>
39
40class taskQTBUG_25691 : public QWizard
41{
42 Q_OBJECT
43public:
44 taskQTBUG_25691( QWidget * parent = 0 );
45
46 ~taskQTBUG_25691(void);
47};
48
49class taskQTBUG_25691Page1 : public QWizardPage
50{
51 Q_OBJECT
52public:
53 taskQTBUG_25691Page1( QWidget * parent = 0 );
54
55 ~taskQTBUG_25691Page1(void);
56};
57
58class taskQTBUG_25691Page2 : public QWizardPage
59{
60 Q_OBJECT
61public:
62 taskQTBUG_25691Page2( QWidget * parent = 0 );
63
64 virtual void initializePage(void);
65
66 ~taskQTBUG_25691Page2(void);
67
68private:
69 QVBoxLayout * layout;
70 QLineEdit * field0_value;
71 QLineEdit * field1_value;
72 QLineEdit * field2_value;
73};
74
75
76taskQTBUG_25691::taskQTBUG_25691( QWidget * parent )
77 : QWizard( parent )
78{
79 this->addPage( page: new taskQTBUG_25691Page1 );
80 this->addPage( page: new taskQTBUG_25691Page2 );
81 this->show();
82}
83
84taskQTBUG_25691::~taskQTBUG_25691(void)
85{
86}
87
88taskQTBUG_25691Page1::taskQTBUG_25691Page1( QWidget * parent )
89 : QWizardPage( parent )
90{
91 QComboBox * field0_needed = new QComboBox( this );
92 field0_needed->addItem( atext: "No" );
93 field0_needed->addItem( atext: "Yes" );
94 field0_needed->setCurrentIndex(0);
95 this->registerField( name: "field0_needed", widget: field0_needed );
96
97 QComboBox * field1_needed = new QComboBox( this );
98 field1_needed->addItem( atext: "No" );
99 field1_needed->addItem( atext: "Yes" );
100 field1_needed->setCurrentIndex(0);
101 this->registerField( name: "field1_needed", widget: field1_needed );
102
103 QComboBox * field2_needed = new QComboBox( this );
104 field2_needed->addItem( atext: "No" );
105 field2_needed->addItem( atext: "Yes" );
106 field2_needed->setCurrentIndex(0);
107 this->registerField( name: "field2_needed", widget: field2_needed );
108
109 QVBoxLayout * layout = new QVBoxLayout;
110 layout->addWidget( field0_needed );
111 layout->addWidget( field1_needed );
112 layout->addWidget( field2_needed );
113 this->setLayout( layout );
114}
115
116taskQTBUG_25691Page1::~taskQTBUG_25691Page1(void)
117{
118}
119
120taskQTBUG_25691Page2::taskQTBUG_25691Page2( QWidget * parent )
121 : QWizardPage( parent )
122{
123 this->layout = new QVBoxLayout;
124 this->setLayout( this->layout );
125
126 this->field0_value = 0;
127 this->field1_value = 0;
128 this->field2_value = 0;
129}
130
131void taskQTBUG_25691Page2::initializePage(void)
132{
133 QWizard * wizard = this->wizard();
134 bool field0_needed = wizard->field( name: "field0_needed" ).toBool();
135 bool field1_needed = wizard->field( name: "field1_needed" ).toBool();
136 bool field2_needed = wizard->field( name: "field2_needed" ).toBool();
137
138 if ( field0_needed && this->field0_value == 0 ){
139 this->field0_value = new QLineEdit( "field0_default" );
140 this->registerField( name: "field0_value", widget: this->field0_value );
141 this->layout->addWidget( this->field0_value );
142 } else if ( ! field0_needed && this->field0_value != 0 ){
143 this->layout->removeWidget( w: this->field0_value );
144 delete this->field0_value;
145 this->field0_value = 0;
146 }
147
148 if ( field1_needed && this->field1_value == 0 ){
149 this->field1_value = new QLineEdit( "field1_default" );
150 this->registerField( name: "field1_value", widget: this->field1_value );
151 this->layout->addWidget( this->field1_value );
152 } else if ( ! field1_needed && this->field1_value != 0 ){
153 this->layout->removeWidget( w: this->field1_value );
154 delete this->field1_value;
155 this->field1_value = 0;
156 }
157
158 if ( field2_needed && this->field2_value == 0 ){
159 this->field2_value = new QLineEdit( "field2_default" );
160 this->registerField( name: "field2_value", widget: this->field2_value );
161 this->layout->addWidget( this->field2_value );
162 } else if ( ! field2_needed && this->field2_value != 0 ){
163 this->layout->removeWidget( w: this->field2_value );
164 delete this->field2_value;
165 this->field2_value = 0;
166 }
167}
168
169taskQTBUG_25691Page2::~taskQTBUG_25691Page2(void)
170{
171}
172
173void taskQTBUG_25691_fieldObjectDestroyed2(void)
174{
175 QMainWindow mw;
176 taskQTBUG_25691 wb( &mw );
177
178 wb.setField( name: "field0_needed", value: true );
179 wb.setField( name: "field1_needed", value: true );
180 wb.setField( name: "field2_needed", value: true );
181 wb.next(); // Results in registration of all three field_Nvalue fields
182 wb.back(); // Back up to cancel need for field1_value
183 wb.setField( name: "field1_needed", value: false ); // cancel need for field1_value
184 wb.next(); // Results in destruction of field field1_value's widget
185 wb.next(); // Commit wizard's results
186
187 // Now collect the value from fields that was not destroyed.
188 QString field0_value = wb.field( name: "field0_value" ).toString();
189 QCOMPARE( field0_value, QString("field0_default") );
190
191 QString field2_value = wb.field( name: "field2_value" ).toString();
192 QCOMPARE( field2_value, QString("field2_default") );
193}
194
195#include "tst_qwizard_2.moc"
196

source code of qtbase/tests/auto/widgets/dialogs/qwizard/tst_qwizard_2.cpp