Why would QSortFilterProxyModel.setData() return false?
I have created a QSortFilterProxyModel off of my QSqlTableModel and have successfully added an extra column to it so that I can add a difference field that would be a waste of space in the database table.
However, the setData function off of the proxy model instance is returning false. No other compile or runtime errors are occuring.
QSortFilterProxyModel *balanceProxyModel = new QSortFilterProxyModel(this);
balanceProxyModel->setSourceModel(balanceModel);
// add a Diff column
balanceProxyModel->insertColumn(6);
balanceProxyModel->setHeaderData(
6, Qt::Horizontal, QObject::tr("Difference"));
for (int i = 0; i < balanceProxyModel->rowCount(); ++i) {
float budget = balanceProxyModel->index(i, 4).data().toFloat();
float actual = balanceProxyModel->index(i, 5).data().toFloat();
float difference = budget - actual;
if (balanceModel->setData(
balanceModel->index(i, 6), QVariant(difference)) == false) {
qDebug() << ATLINE << ":"
<< "diff not added!!!";
}
For each record I am getting "diff not added!!!" since setData is failing (returning false.)
Thanks.
--update 20110907_0754-- I tried ixM's suggestion, and fixed a minor error, but it didn't fix the issue. I'm still getting "diff not added" (or false back from the setData function.)
if (balanceProxyModel->setData(
balanceProxyModel->index(i, 6), QVariant(difference)) == false) {
qDebug() << ATLINE << ":"
<< "diff not added!!!";
}
--update 20110907_2205-- I added the following (by the way, the Difference field does show up):
qDebug() << "balanceProxyModel->insertColumn(6);";
qDebug() << balanceProxyModel->insertColumn(6);
And got the following results to stdout:
Debug: balanceProxyModel->insertColumn(6);
Debug: true
Later, I added the following to the loop:
qDebug() << "balanceModel->index(i, 6);";
qDebug() << balanceModel->index(i, 6);
to stdout:
Debug: balanceModel->index(i, 6);
Debug: QModelIndex(128,6,0x0,QSqlTableModel(0x1d10bf0) )
--update 20110908_2153-- Hmm, I didn't consider setting properties on the column. Here's the result of your test:
qDebug()
<< (bool)(balanceProxyModel->flags(diffModelIndex) & Qt::ItemIsEditable)
<< (bo开发者_高级运维ol)(balanceProxyModel->flags(diffModelIndex) & Qt::ItemIsEnabled);
results:
Debug: false true
From the test that ixM suggested, the results are showing that Qt::ItemIsEditable is false. That answers my actual question. Of course I'm left wondering just how to set that flag. The model index class has no method that I can see that allows setting the flags. Perhaps at this point I should look at subclassing the proxy class to get it to allow editable columns.
Maybe you should make balanceProxyModel->setData(balanceProxyModel->index(i,6))
I think you're using the wrong model there ;)
When encountering such errors, you should always check what index is returned by the index method (which, in this case, is probably an invalid index).
Edit> What does qDebug() << balanceProxyModel->insertColumn(6);
say? The man says it inserts the column before the given column pos. Maybe you should try with 7 instead of 6? What does qDebug() << balanceProxyModel->index(i, 6);
in the loop?
Edit 2> Ok the only thing I can think of, is that a flag is missing for that column. Could you paste the result of qDebug() << (bool)(balanceProxyModel->flags() & Qt::ItemIsEditable) << (bool)(balanceProxyModel->flags() & Qt::ItemIsEnabled);
?
Last Edit> Yes, the only way that I know to change the flags is to subclass the model that you're using. There are some things in Qt that are a little bit disappointing :/
精彩评论