[PyQt] Want to change multiple cells in QTableView at once in
custom Model/View
Hans-Peter Jansen
hpj at urpla.net
Mon Oct 5 10:54:05 BST 2009
Am Montag 05 Oktober 2009 schrieb bvz:
> The selectionModel will return which cells are selected... but it is a
> method of the QTableView, not the QAbstractTableModel.
>
> So how do I get this information into my setData method in my model? As
> far as I can see, regardless of what info I can get from the
> selectionModel, the setData method will only recognize one cell as being
> selected and will then only update one cell (even though I may have many
> selected).
While I didn't investigate, this sounds like you're mixing the concepts of
current item with the those of the selection model.
Since selection is immanently related to the views, you may structure your
app accordingly - probably by inheriting your views from a common base, and
do the selection work there - or - add a new selection flag to your model,
and manage that from your views.
Pete
> Hans-Peter Jansen-2 wrote:
> > Am Samstag 03 Oktober 2009 schrieb bvz:
> >> I had a version of my spreadsheet style app (in which I handled all of
> >> the updating of the table manually) that allowed me to set the value
> >> of multiple cells at once.
> >>
> >> (if the user selected multiple cells and entered, say, "bob" then all
> >> the selected cells would be updated to contain "bob".)
> >>
> >> I managed to do this by using a for cellIndex in
> >> self.selectedIndexes() loop inside a method called by the
> >> QTableWidget.itemChanged(QTableWidgetItem *) signal.
> >>
> >>
> >> Old code:
> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> >> self.blockSignals(True)
> >> value = cell.text()
> >> dataType = cell.dataType
> >> for cellIndex in self.selectedIndexes():
> >> currentCell = self.item(cellIndex.row(),
> >> cellIndex.column()) if currentCell.dataType == dataType: # Only update
> >> cells of the same data type
> >> currentCell.update_parameter_object(value)
> >> self.blockSignals(False)
> >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> >>
> >> Now I have it all set up to use the model/view system and it is all
> >> much better. Except that I can't seem to figure out how to mimic that
> >> one feature. The data() method that I reimplemented in the model only
> >> receives the index of the currently selected cell. Is there any way
> >> of finding out in the model which cells are selected in the view?
> >
> > That sounds, like you're after self.selectionModel()...
> >
> > Pete
> > _______________________________________________
> > PyQt mailing list PyQt at riverbankcomputing.com
> > http://www.riverbankcomputing.com/mailman/listinfo/pyqt
More information about the PyQt
mailing list