Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, May 2007
qtableview delete rows


Message 1 in thread

Dear All,
can someone clarify me a bit how it works when I want to delete a set of
unsorted rows from QTableView? I have items to be deleted, call deleteRows
with proper information. Inside model's deleteRows
I cann begin..endDeleteRows how it is supposed to be, however the table
doesn't get correctly updated. Firstly, the persistent editors I have in the
first column stay as they are even if I explicitly call
closePersistenceEditor. Seconds, except of the very last row the others are
not deleted from the table, they are just left blank.

shall I call something else to update the tableview?

thanksalot
d.

Message 2 in thread

Hi Dejfson,

I haven't used openPersistentEditor() or closePersistenteditor() 
before...but basic QTableView removal is something like this..

QTableView *temp = static_cast<QTableView*>(currentWidget());
// retrieve QTableView object, I had more than one

QItemSelectionModel *selectionModel = temp->selectionModel();
// retrieve selectionModel, each QTableView object has one
  
QModelIndexList indexes = selectionModel->selectedRows();
// obtain selected items, if you have multiple selection enabled

QModelIndex index;

foreach(index, indexes)   // loop through and remove them
{
        int row = index.row();
        table->removeRows(row, 1, QModelIndex());
}

Hope this helps,
K.



dejfson Belohrad wrote:
> Dear All,
> can someone clarify me a bit how it works when I want to delete a set 
> of unsorted rows from QTableView? I have items to be deleted, call 
> deleteRows with proper information. Inside model's deleteRows
> I cann begin..endDeleteRows how it is supposed to be, however the 
> table doesn't get correctly updated. Firstly, the persistent editors I 
> have in the first column stay as they are even if I explicitly call
> closePersistenceEditor. Seconds, except of the very last row the 
> others are not deleted from the table, they are just left blank.
>
> shall I call something else to update the tableview?
>
> thanksalot
> d.
>

--
 [ signature omitted ] 

Message 3 in thread

Hi Dejfson,

I can't see what the problem is by looking at your current code. It 
would be great if you could send more code...maybe parts of your 
W_layerSettings (I think this your TableModel?) code..or something.

Thanks,
K.

P/s - You might want to copy your code to the list as well so more 
people can help you out :)

dejfson wrote:
> Thanks for response. Honestly, i'm doing it in completely the same 
> way. My implementation is following:
>
>
> this is my big-fancy delete button on one dialog box:
>
> void W_layerSettings::deleteLayer()
> {
>     QModelIndexList selection = d_layersView->selectionModel()->selectedRows();
>
>     QListIterator<QModelIndex> it (selection);
>     it.toBack();
>     while (
> it.hasPrevious())
>     {
>         const QModelIndex& idx = it.previous();
>         D_objectLayers::layerData *dat = (D_objectLayers::layerData*)idx.internalPointer();
>
>         // close persistent editor if layer is deletable:
>
>         if (dat)
>             if (dat->b_canBeDeleted)
>             {
>                 d_layersView->closePersistentEditor(idx);
>                 if (qp_model->removeRows(idx.row(), 1, idx) == FALSE)
>
>                 {
>                     qp_statusBar->showMessage (tr ("Some layers could not be removed because they contain linked objects"), 5000);
>                 }
>             }
>     }
> }
>
>
> -------------- and this is implementation of removeRows in model:
>
> bool W_layerSettings_model::removeRows ( int row, int count, const QModelIndex &idx  ) 
> {
> 	Q_ASSERT_X (count == 1, "W_layerSettings_model::removeRows", "multiple row removal not supported for the moment. Implement me!");
>
>
>         D_objectLayers::layerData *currentLayer = (D_objectLayers::layerData *)idx.internalPointer();
> 	Q_ASSERT_X(currentLayer, "W_layerSettings_model::removeRows", "current layer not defined");
>
> 	beginRemoveRows ( QModelIndex (), row, row);
> 	bool result = qp_layers->deleteLayer (currentLayer);
> 	endRemoveRows();
> 	return result;
> }
>
>
>
> ------------------>
> but this behaves exactly how i wrote: it doesn't delete the lines in 
> the tableview (it just lefts them empty) and it doesn't close
> persistent editor.

--
 [ signature omitted ]