[Qt-interest] How to remove items from QSqlTableModelvia QTableView

Tony Rietwyk tony.rietwyk at rightsoft.com.au
Mon Apr 27 01:15:16 CEST 2009


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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090427/ff50cc35/attachment.html 


More information about the Qt-interest-old mailing list