[PyQt] Question regarding single model and multiple views...

Matt Newell newellm at blur.com
Thu Oct 1 01:37:21 BST 2009


On Wednesday 30 September 2009 17:08:40 bvz wrote:
> Hello,
>
> I am new(ish) to PyQt and I am trying to build a simple app.
>
> The app has several tables that I want to point to a single model (which
> will load and store all the data).
>
> Each table is responsible for displaying a different set of columns from
> the model.  I.e. table 1 will show columns 1-4, table 2 columns 5-11,
> etc... The app does not know how many tables there will be ahead of time,
> nor does it know how many columns will be displayed in each table.  Both of
> these are set by reading a config file.
>
> I have a version working where I use a QTableWidget and manually sync the
> data from the "model" (not actually a subclass of QAbstractTableModel). 
> But there are some issues with this and, anyway, it isn't the "correct" way
> of working as far as I can tell.
>
> I really want to update the app to use the QAbstractTableModel (no
> delegates).  I mostly understand the process and am pretty far along with
> one exception.  How do I translate the "actual" column index in the table
> to the associated index in the model.  I.e. if the user updates a cell in
> column 3 of the 2nd table, that might actually be a bit of data that is in
> column 7 of the model.  I need to be able to map the table column to the
> model column and vice versa.
>
> So I am wondering, is there some way the table view can send along an
> offset every time it tries to send data to or from the model?  If I can get
> that info, I can always translate inside the model and return/set the
> correct data.
>
> Thanks.
The indexes should always match up.  Just use QTableView::setColumnHidden to 
hide the columns that aren't needed for each view.

If for some reason that didn't work it would be somewhat trivial to write a 
wrapper model that simply provides a subset of the real model.  Something 
like QSortFilterProxyModel, only a much simpler implementation as the mapping 
is always a constant operation.

Matt



More information about the PyQt mailing list