@@ -52,12 +52,14 @@ QVariant QgsValueRelationWidgetWrapper::value() const
5252 }
5353 }
5454
55+ const int nofColumns = columnCount ();
56+
5557 if ( mTableWidget )
5658 {
5759 QStringList selection;
5860 for ( int j = 0 ; j < mTableWidget ->rowCount (); j++ )
5961 {
60- for ( int i = 0 ; i < config ( QStringLiteral ( " NofColumns " ) ). toInt () ; ++i )
62+ for ( int i = 0 ; i < nofColumns ; ++i )
6163 {
6264 QTableWidgetItem *item = mTableWidget ->item ( j, i );
6365 if ( item )
@@ -151,12 +153,14 @@ void QgsValueRelationWidgetWrapper::setValue( const QVariant &value )
151153
152154 QTableWidgetItem *lastChangedItem = nullptr ;
153155
156+ const int nofColumns = columnCount ();
157+
154158 // This block is needed because item->setCheckState triggers dataChanged gets back to value()
155159 // and iterate over all items again! This can be extremely slow on large items sets.
156160 mTableWidget ->blockSignals ( true );
157161 for ( int j = 0 ; j < mTableWidget ->rowCount (); j++ )
158162 {
159- for ( int i = 0 ; i < config ( QStringLiteral ( " NofColumns " ) ). toInt () ; ++i )
163+ for ( int i = 0 ; i < nofColumns ; ++i )
160164 {
161165 QTableWidgetItem *item = mTableWidget ->item ( j, i );
162166 if ( item )
@@ -230,6 +234,10 @@ void QgsValueRelationWidgetWrapper::setFeature( const QgsFeature &feature )
230234 }
231235}
232236
237+ int QgsValueRelationWidgetWrapper::columnCount () const
238+ {
239+ return qMax ( 1 , config ( QStringLiteral ( " NofColumns" ) ).toInt () );
240+ }
233241
234242void QgsValueRelationWidgetWrapper::populate ( )
235243{
@@ -258,24 +266,21 @@ void QgsValueRelationWidgetWrapper::populate( )
258266 }
259267 else if ( mTableWidget )
260268 {
269+ const int nofColumns = columnCount ();
270+
261271 if ( mCache .size () > 0 )
262272 {
263- const int nofCols = config ( QStringLiteral ( " NofColumns" ) ).toInt ();
264- const int denom = nofCols != 0 ? nofCols : 1 ;
265- mTableWidget ->setRowCount ( ( mCache .size () + nofCols - 1 ) / denom );
273+ mTableWidget ->setRowCount ( ( mCache .size () + nofColumns - 1 ) / nofColumns );
266274 }
267275 else
268276 mTableWidget ->setRowCount ( 1 );
269- if ( config ( QStringLiteral ( " NofColumns" ) ).toInt () > 0 )
270- mTableWidget ->setColumnCount ( config ( QStringLiteral ( " NofColumns" ) ).toInt () );
271- else
272- mTableWidget ->setColumnCount ( 1 );
277+ mTableWidget ->setColumnCount ( nofColumns );
273278
274279 whileBlocking ( mTableWidget )->clear ();
275280 int row = 0 , column = 0 ;
276281 for ( const QgsValueRelationFieldFormatter::ValueRelationItem &element : qgis::as_const ( mCache ) )
277282 {
278- if ( column == config ( QStringLiteral ( " NofColumns " ) ). toInt () )
283+ if ( column == nofColumns )
279284 {
280285 row++;
281286 column = 0 ;
@@ -304,11 +309,13 @@ void QgsValueRelationWidgetWrapper::populate( )
304309
305310void QgsValueRelationWidgetWrapper::showIndeterminateState ()
306311{
312+ const int nofColumns = columnCount ();
313+
307314 if ( mTableWidget )
308315 {
309316 for ( int j = 0 ; j < mTableWidget ->rowCount (); j++ )
310317 {
311- for ( int i = 0 ; i < config ( QStringLiteral ( " NofColumns " ) ). toInt () ; ++i )
318+ for ( int i = 0 ; i < nofColumns ; ++i )
312319 {
313320 whileBlocking ( mTableWidget )->item ( j, i )->setCheckState ( Qt::PartiallyChecked );
314321 }
0 commit comments