PDA

View Full Version : QSqlQueryModel insertColumn() returns false



kasper360
25th March 2011, 11:55
I am trying to add a new column to a QSqlQueryModel and populate it with data not related to its database. There are 2 columns from the database query. I tried the following code to check if column insertion was successful.


QModelIndex testIndex = atsFieldModel->index(1, 1);
if(atsFieldModel->insertColumn(2, testIndex.parent()))
QMessageBox::information(this, tr("Successful"), tr("column inserted!"));

But the messagebox does not appear. I also used insertColumns() method but it didn't work either. Please explain to me what I did wrong here. Thank you.

norobro
25th March 2011, 14:10
From QSqlQueryModel::insertColumns() :
The parent parameter must always be an invalid QModelIndex, since the model does not support parent-child relationships.

EDIT: Commented without trying your code. Looks like testIndex.parent() does return an invalid QModelIndex. Try:
atsFieldModel->insertColumn(2)Both work for me.

kasper360
28th March 2011, 04:58
OMG, it was right there in the documentation, I must have missed it. It worked perfectly. Thanx a lot norobro :)