[PyQt] Model copying

Andreas Pakulat apaku at gmx.de
Fri Dec 13 13:34:47 GMT 2013


Hi,

On Fri, Dec 13, 2013 at 2:01 PM, Jorge Tornero - Listas <
jtorlistas at gmail.com> wrote:

> Thank you for reading this:
>
> I' ve populated a tablewidget and I want to reuse its (internal) model to
> pouplate a qcombobox . So far, I've done:
>
> 1) I create a qtableview and a model for it as newModel=tableWidget.model()
> 2) I set the model of my combo the same as the qtableview
> 3) I set combobox's view as the qtableview.
>
> Of course it works... but when I remove a item in the "original"
> tablewidget, it gets removed from the combo. I think it's amatter of
> assignmente, etc... but I've tried with copy() and deepcopy() and it throws
> an error:
>
> TypeError: PyQt4.QtCore.QAbstractTableModel represents a C++ abstract
> class and cannot be instantiated
>
> Any idea on how to proceed?
>

Sharing a model only makes sense if you want to make sure that all views
using it show the same data. That also means that all views will drop items
if they are removed from the model, this is intentional and not a bug.

Now if you don't want that, you actually don't want to share the model. So
what you need to do is create a copy of the data that the model uses and
then populate the combobox model with that. Since you use QTableWidget the
underlying data is whatever you used to populate the QTableWidgetItems.
That data is what you need to put into the combobox, you can't just use the
model of the tablewidget.

Andreas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20131213/ed01e08f/attachment.html>


More information about the PyQt mailing list