Skip to content

Commit 897322a

Browse files
committed
Fix crash loading style from postgres db when there is no "layer_styles" table in the database
Fix #20155 -crash loading style from postgres db when there is no "layer_styles" table in the database
1 parent 225c922 commit 897322a

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/app/qgsvectorlayerloadstyledialog.cpp

+19-15
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@ QString QgsVectorLayerLoadStyleDialog::filePath() const
128128

129129
void QgsVectorLayerLoadStyleDialog::initializeLists( const QStringList &ids, const QStringList &names, const QStringList &descriptions, int sectionLimit )
130130
{
131+
// -1 means no ids
131132
mSectionLimit = sectionLimit;
132133
int relatedTableNOfCols = sectionLimit > 0 ? 2 : 1;
133-
int othersTableNOfCols = ids.count() - sectionLimit > 0 ? 2 : 1;
134+
int othersTableNOfCols = ( sectionLimit >= 0 && ids.count() - sectionLimit > 0 ) ? 2 : 1;
134135
QString twoColsHeader( QStringLiteral( "Name;Description" ) );
135136
QString oneColsHeader( QStringLiteral( "No styles found in the database" ) );
136137
QString relatedTableHeader = relatedTableNOfCols == 1 ? oneColsHeader : twoColsHeader;
@@ -141,24 +142,27 @@ void QgsVectorLayerLoadStyleDialog::initializeLists( const QStringList &ids, con
141142
mRelatedTable->setHorizontalHeaderLabels( relatedTableHeader.split( ';' ) );
142143
mOthersTable->setHorizontalHeaderLabels( othersTableHeader.split( ';' ) );
143144
mRelatedTable->setRowCount( sectionLimit );
144-
mOthersTable->setRowCount( ids.count() - sectionLimit );
145+
mOthersTable->setRowCount( sectionLimit >= 0 ? ( ids.count() - sectionLimit ) : 0 );
145146
mRelatedTable->setDisabled( relatedTableNOfCols == 1 );
146147
mOthersTable->setDisabled( othersTableNOfCols == 1 );
147148

148-
for ( int i = 0; i < sectionLimit; i++ )
149+
if ( sectionLimit > 0 )
149150
{
150-
QTableWidgetItem *item = new QTableWidgetItem( names.value( i, QString() ) );
151-
item->setData( Qt::UserRole, ids[i] );
152-
mRelatedTable->setItem( i, 0, item );
153-
mRelatedTable->setItem( i, 1, new QTableWidgetItem( descriptions.value( i, QString() ) ) );
154-
}
155-
for ( int i = sectionLimit; i < ids.count(); i++ )
156-
{
157-
int j = i - sectionLimit;
158-
QTableWidgetItem *item = new QTableWidgetItem( names.value( i, QString() ) );
159-
item->setData( Qt::UserRole, ids[i] );
160-
mOthersTable->setItem( j, 0, item );
161-
mOthersTable->setItem( j, 1, new QTableWidgetItem( descriptions.value( i, QString() ) ) );
151+
for ( int i = 0; i < sectionLimit; i++ )
152+
{
153+
QTableWidgetItem *item = new QTableWidgetItem( names.value( i, QString() ) );
154+
item->setData( Qt::UserRole, ids[i] );
155+
mRelatedTable->setItem( i, 0, item );
156+
mRelatedTable->setItem( i, 1, new QTableWidgetItem( descriptions.value( i, QString() ) ) );
157+
}
158+
for ( int i = sectionLimit; i < ids.count(); i++ )
159+
{
160+
int j = i - sectionLimit;
161+
QTableWidgetItem *item = new QTableWidgetItem( names.value( i, QString() ) );
162+
item->setData( Qt::UserRole, ids[i] );
163+
mOthersTable->setItem( j, 0, item );
164+
mOthersTable->setItem( j, 1, new QTableWidgetItem( descriptions.value( i, QString() ) ) );
165+
}
162166
}
163167
}
164168

0 commit comments

Comments
 (0)