Results 1 to 7 of 7

Thread: QAbstractProxyModel: adding a virtual column

  1. #1
    Join Date
    Aug 2008
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QAbstractProxyModel: adding a virtual column

    What I have:


    What I want:
    • A single QLabel that displays a concatenation of some of the person's information formatted in a way that is different from the original model. For instance: "John Doe (1 yr, 4 mos; M; 3/31/07)"
    • When any of the information from the original model is updated, the QLabel should be updated immediately


    To achieve this I think subclassing QAbstractProxyModel is the correct thing to do. I want the subclassed proxy model to have one extra column then the source model -- that extra column will be the concatenation of the person's information.

    The problem is, I can't figure out how to do this....

    so... How do I:
    • Create a proxymodel that has one extra vitual column?
    • The rest of the columns are a one to one match of the source models columns
    • If any of the columns are updated in the source model, they are immediately reflected in the proxy model (including the virtual column)


    I know I can connect the source models dataChanged event to a function to manually update the QLabel... but I want to learn how to do it this way (because there are other parts in my program that this could be useful).

    I've already looked at the below post, but unfortunatly I'm going to need a little more hand holding then that post provides.
    http://www.qtcentre.org/forum/f-qt-p...ractProxyModel

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QAbstractProxyModel: adding a virtual column

    You don't need a proxy here. Simply connect to the data widget mapper's currentIndexChanged() signal with a custom slot and set the text of the label there. Also connect the same slot to the dataChanged() (and possibly modelReset()) signal of the model.

  3. #3
    Join Date
    Aug 2008
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QAbstractProxyModel: adding a virtual column

    You're right. For this specific instance I don't *need* a proxy. But I want to figure out how to do it with a proxy anyway.

    The reason why is there are other places in my program that would greatly benefit from a proxy model that had a virtual extra column. For instance I have a QTableView that displays data for a QSqlTableModel. In the view, I wish for an extra column that does a summation of the values of a few other columns. A proxy model would be ideal for this instance.... unless there is a better idea that I'm not thinking of.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QAbstractProxyModel: adding a virtual column

    If you insist on using a proxy then adding a column to a model is very simple. You need to return a larger columnCount() than for the original model. Furthermore you need to modify the mapToSource() method to account for the additional column (it should return an invalid index for the additional column). Furthermore you need to reimplament data() to return data for the column.

  5. #5
    Join Date
    Aug 2008
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QAbstractProxyModel: adding a virtual column

    Right... but a widget mapped to that virtual column does not get updated when when the columns that the virtual column consists of gets updated.

    There needs to be some kind of signal for the model to mention that the virtual column has changed whenever any of the columns associated to the virtual column changes... And thats where I'm stuck...

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QAbstractProxyModel: adding a virtual column

    You need to reimplement setData() and emit a dataChanged() signal for the artificial index whenever any of its dependencies get updated. Alternatively simply monitor for dataChanged() signal from the source model and if any of the changed indices affect the artificial column, emit dataChanged() for it.

  7. The following user says thank you to wysota for this useful post:

    ultim8 (28th August 2008)

  8. #7
    Join Date
    Aug 2008
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QAbstractProxyModel: adding a virtual column

    oh... that makes complete sense. I didn't realize that the dataChanged signal would trigger a call to update a widget mapped to the model... but now that I think about it... of coarse it would.

    Thanks for your help.

Similar Threads

  1. hidden QListView column suddenly visible
    By edb in forum Qt Programming
    Replies: 10
    Last Post: 27th January 2006, 09:00

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.