Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Overriding the use of Qt.DisplayRole in a QTreeView (or any other item view)
Forum Updated to NodeBB v4.3 + New Features

Overriding the use of Qt.DisplayRole in a QTreeView (or any other item view)

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    elgur
    wrote on last edited by
    #1

    Hi

    I have been trying to figure how I can make the treeview use some other role than the default DisplayRole when it's fetching data from a model for displaying.

    The reason for this is that I use my QAbstractItemModel for both a main QTreeView and a few QTableViews.
    In the treeview I have only 1 column and want to return a concatenation of 2-3 columns for it's display role.
    The table views have multiple columns and I want to return them respectively from the data function.

    It's the problem with using the same DisplayRole for both types of views since I have no way of knowing what kind of view is requesting the data from the model's data function, so I thought the simplest solution might be to use custom roles for them (e.g. 'TreeDisplayRole' and 'TableDisplayRole').

    I haven't been able to find anything about this. Neither how the view decides to use DisplayRole or how to override that functionality.

    I guess I could use a proxy model for each type of view for simply changing 'DisplayRole' to my custom role before sending the call to the underlying model's data function, but I'm thinking that might be overkill. I'm hoping I can do this by subclassing the item views at most.

    Thanks for reading and for all future replies.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      It's not an overkill! Use a proxy model right between the tree view and the original model.

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • E Offline
        E Offline
        elgur
        wrote on last edited by
        #3

        [quote author="peppe" date="1326633043"]It's not an overkill! Use a proxy model right between the tree view and the original model.[/quote]

        I actually started with subclassing QItemDelegate, overriding the paint function and I'm realizing that might be a more complex solution. I have successfully made it display the text the way I want it, but I haven't been able to make it show the graphical theme you get by default (mouse over effects and such).
        Looks like I don't have access to some of the functions like 'textRectangle' and 'decorationRectangle' in PyQt (I just noticed the language bindings board, a mod may move this) so it might be a little tricky.

        Would the "QIdentityProxyModel":http://developer.qt.nokia.com/doc/qt-4.8/qidentityproxymodel.html be a good choice for this?

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved