[Qt-interest] How to remove items from QSqlTableModelvia QTableView
Sven Grunewaldt
strayer at olle-orks.org
Tue Apr 28 01:28:47 CEST 2009
Hi Alex,
because of this I created QPersistentModelIndexes, as they should be
updated by the model when the rows are changed. Sadly, they seem to get
invalid after the first one is removed, which I don't understand...at all...
Regards,
Sven Grunewaldt
Am 27.04.2009 21:29, schrieb Malyushytsky, Alex:
> According to my understanding it does not work, because the way you remove rows changes indexing.
> For example if u need to remove rows 3,4,7, and are trying to remove 3 first, row 4 becomes 3, 7 becomes 6.
>
> To fix problem try to remove the rows in decrementing order, something like
> for ( int row = tmp.size()-1; row>-1; row--)
> { ..........
> }
>
> Also, according to the documentation selectedIndexes() do not contain duplicates, but is not sorted.
> So you would need to pre-sort it.
>
> I would say that even you can make this approach work, it is not at efficient.
> What I would do, instead of using selectedIndexes() I would iterate (in reverse direction) through the each index,
> check if it is selected, remove it.
>
> Hope this helps,
> Alex
>
>
>
> From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Sven Grunewaldt
> Sent: Monday, April 27, 2009 12:06 PM
> To: qt-interest at trolltech.com
> Subject: Re: [Qt-interest] How to remove items from QSqlTableModelvia QTableView
>
> 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
>
>
>
>
> ---------------------------------------------------------------------------------------------------
> Weidlinger Associates, Inc. made the following annotations.
>
> "This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you."
>
> "Please consider our environment before printing this email."
>
More information about the Qt-interest-old
mailing list