[Qt-interest] How to remove items from QSqlTableModelvia QTableView
Sven Grunewaldt
strayer at olle-orks.org
Mon Apr 27 21:05:50 CEST 2009
Hi,
actually this doesn't seem to work and I again don't understand why ;)
My function:
---
void BerichteOrk::removeSelectedFromTableView(QTableView* view)
{
QAbstractItemModel* model = view->model();
const QModelIndexList tmp = view->selectionModel()->selectedIndexes();
QList<QPersistentModelIndex> indexes;
for (int i = 0; i < tmp.size(); i++)
{
qDebug() << tmp.at(i).internalId() << tmp.at(i).isValid() <<
tmp.at(i).row();
indexes.append(QPersistentModelIndex(tmp.at(i)));
qDebug() << tmp.at(i).internalId() << tmp.at(i).isValid();
}
qDebug() << "";
for (int i = 0; i < indexes.size(); i++)
{
qDebug() << indexes.at(i).internalId() <<
indexes.at(i).isValid() << indexes.at(i).row();
model->removeRow(indexes.at(i).row());
qDebug() << indexes.at(i).internalId() << indexes.at(i).isValid();
}
view->resizeColumnsToContents();
view->resizeRowsToContents();
view->horizontalHeader()->setStretchLastSection(true);
}
---
qDebug() output:
---
0 true 1
0 true
0 true 2
0 true
0 true 3
0 true
0 true 4
0 true
0 true 1
0 false
0 false -1
0 false
0 false -1
0 false
0 false -1
0 false
---
As I understand the documentation this _should_ work...
Regards,
Sven Grunewaldt
Am 27.04.2009 01:15, schrieb Tony Rietwyk:
> Hi Sven,
> Correct me if I'm wrong, but I don't think you should make any
> assumptions about the order of the indexes returned from selectedIndexes.
> I suggest converting them to persistent indexes first, that way
> subsequent indexes will get updated by the view as you delete them.
> Hope that helps,
> Tony.
>
> -----Original Message-----
> *From:* qt-interest-bounces at trolltech.com
> [mailto:qt-interest-bounces at trolltech.com] *On Behalf Of *Sven
> Grunewaldt
> *Sent:* Sunday, 26 April 2009 01:58
> *To:* qt-interest at trolltech.com
> *Subject:* Re: [Qt-interest] How to remove items from
> QSqlTableModelvia QTableView
>
> Haha, oh god. I can't believe I didn't notice this... Thanks! :)
> Sometimes you need another pair of eyes to see the obvious.
>
> Regards,
> Sven Grunewaldt
>
> Am 25.04.2009 17:50, schrieb Scott Aron Bloom:
>> Your algorithm fails for the same reason this algorithm fails:
>>
>> QList< int> values;
>> //make a list where value[ ii ] == ii
>> for( int ii = 0; ii< 10; ++ii )
>> values<< ii;
>>
>>
>> // remove value 3, 4, 5
>> values.removeAt( 3 );
>> values.removeAt( 4 );
>> values.removeAt( 5 );
>>
>> // which actually removes 3,5 and 7 (I think... :) )
>> The problem is, since you are removing index.row() 1 at a time, your row
>> value will most likely NOT be valid after the remove.
>>
>>
>> Scott
>>
>>
>>
>>> -----Original Message-----
>>> From:qt-interest-bounces at trolltech.com [mailto:qt-interest-
>>> bounces at trolltech.com] On Behalf Of Sven Grunewaldt
>>> Sent: Saturday, April 25, 2009 8:31 AM
>>> To:qt-interest at trolltech.com
>>> Subject: [Qt-interest] How to remove items from QSqlTableModel via
>>>
>> QTableView
>>
>>> Hi,
>>>
>>> I have a QTableView which lies on top of a QSqlTableModel and a Button
>>> to remove the currently selected rows. It seems that my implementation
>>> of the buttons function does something wrong.
>>> Sometimes not all selected items are removed and I just can't see why,
>>> maybe someone finds my mistake:
>>>
>>> void BerichteOrk::on_removeSchuleButton_clicked()
>>> {
>>> const QModelIndexList tmp =
>>> schuleView->selectionModel()->selectedIndexes();
>>>
>>> foreach (QModelIndex index, tmp)
>>> {
>>> schuleModel->removeRow(index.row());
>>> }
>>> }
>>>
>>> Regards,
>>> Sven Grunewaldt
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090427/d89205e4/attachment.html
More information about the Qt-interest-old
mailing list