Skip to content

Commit f0c8fe6

Browse files
committed
Ensure data defined button displayed fields are always up to date
Fix #14809
1 parent f8f3b21 commit f0c8fe6

2 files changed

Lines changed: 46 additions & 35 deletions

File tree

src/gui/qgsdatadefinedbutton.cpp

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,47 @@ QgsDataDefinedButton::~QgsDataDefinedButton()
101101
mCheckedWidgets.clear();
102102
}
103103

104+
void QgsDataDefinedButton::updateFieldLists()
105+
{
106+
mFieldNameList.clear();
107+
mFieldTypeList.clear();
108+
109+
if ( mVectorLayer )
110+
{
111+
// store just a list of fields of unknown type or those that match the expected type
112+
Q_FOREACH ( const QgsField& f, mVectorLayer->fields() )
113+
{
114+
bool fieldMatch = false;
115+
// NOTE: these are the only QVariant enums supported at this time (see QgsField)
116+
QString fieldType;
117+
switch ( f.type() )
118+
{
119+
case QVariant::String:
120+
fieldMatch = mDataTypes.testFlag( String );
121+
fieldType = tr( "string" );
122+
break;
123+
case QVariant::Int:
124+
fieldMatch = mDataTypes.testFlag( Int ) || mDataTypes.testFlag( Double );
125+
fieldType = tr( "integer" );
126+
break;
127+
case QVariant::Double:
128+
fieldMatch = mDataTypes.testFlag( Double );
129+
fieldType = tr( "double" );
130+
break;
131+
case QVariant::Invalid:
132+
default:
133+
fieldMatch = true; // field type is unknown
134+
fieldType = tr( "unknown type" );
135+
}
136+
if ( fieldMatch || mDataTypes.testFlag( AnyType ) )
137+
{
138+
mFieldNameList << f.name();
139+
mFieldTypeList << fieldType;
140+
}
141+
}
142+
}
143+
}
144+
104145
void QgsDataDefinedButton::init( const QgsVectorLayer* vl,
105146
const QgsDataDefined* datadefined,
106147
const DataTypes& datatypes,
@@ -155,44 +196,11 @@ void QgsDataDefinedButton::init( const QgsVectorLayer* vl,
155196
mActionDataTypes->setText( tr( "Field type: " ) + mDataTypesString );
156197
}
157198

158-
if ( mVectorLayer )
159-
{
160-
// store just a list of fields of unknown type or those that match the expected type
161-
Q_FOREACH ( const QgsField& f, mVectorLayer->fields() )
162-
{
163-
bool fieldMatch = false;
164-
// NOTE: these are the only QVariant enums supported at this time (see QgsField)
165-
QString fieldType;
166-
switch ( f.type() )
167-
{
168-
case QVariant::String:
169-
fieldMatch = mDataTypes.testFlag( String );
170-
fieldType = tr( "string" );
171-
break;
172-
case QVariant::Int:
173-
fieldMatch = mDataTypes.testFlag( Int ) || mDataTypes.testFlag( Double );
174-
fieldType = tr( "integer" );
175-
break;
176-
case QVariant::Double:
177-
fieldMatch = mDataTypes.testFlag( Double );
178-
fieldType = tr( "double" );
179-
break;
180-
case QVariant::Invalid:
181-
default:
182-
fieldMatch = true; // field type is unknown
183-
fieldType = tr( "unknown type" );
184-
}
185-
if ( fieldMatch || mDataTypes.testFlag( AnyType ) )
186-
{
187-
mFieldNameList << f.name();
188-
mFieldTypeList << fieldType;
189-
}
190-
}
191-
}
192-
199+
updateFieldLists();
193200
updateGui();
194201
}
195202

203+
196204
void QgsDataDefinedButton::updateDataDefined( QgsDataDefined *dd ) const
197205
{
198206
if ( !dd )
@@ -230,6 +238,8 @@ void QgsDataDefinedButton::mouseReleaseEvent( QMouseEvent *event )
230238
void QgsDataDefinedButton::aboutToShowMenu()
231239
{
232240
mDefineMenu->clear();
241+
// update fields so that changes made to layer's fields are reflected
242+
updateFieldLists();
233243

234244
bool hasExp = !getExpression().isEmpty();
235245
bool hasField = !getField().isEmpty();

src/gui/qgsdatadefinedbutton.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ class GUI_EXPORT QgsDataDefinedButton: public QToolButton
338338
void showExpressionDialog();
339339
void showAssistant();
340340
void updateGui();
341+
void updateFieldLists();
341342

342343
const QgsVectorLayer* mVectorLayer;
343344
QStringList mFieldNameList;

0 commit comments

Comments
 (0)