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. QAbstractItemDelegate::paint() : custom Delegate, how to alter the value displayed in cell - Solved
Forum Updated to NodeBB v4.3 + New Features

QAbstractItemDelegate::paint() : custom Delegate, how to alter the value displayed in cell - Solved

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 4.1k 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.
  • G Offline
    G Offline
    galrub
    wrote on last edited by
    #1

    hello all,
    there is probably an easy way to do this: the thing is that in the DB the values of a 'mytype' column are from 1 to 5. these
    values represent constants in the program, so, here is what I have:
    @
    void MyTypeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    const QModelIndex &index) const {
    int value = index.model()->data(index, Qt::DisplayRole).toInt();
    switch(value){
    case MyTypes::Type_A:
    //change displayed value to "type A"
    break;
    //.... and so on
    };
    }
    @

    is there a way to do this?
    thx,
    G

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      I think it would be better to put an [[Doc:QAbstractProxyModel]] subclass inbetween (or maybe a [[Doc:QSortFilterProxyModel]] subclass, you'll have to check which one fits best).

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        For Qt 4.7, use a [[Doc:QSortFilterProxyModel]] as your base, for 4.8, a [[Doc:QIdentityProxyModel]]. You don't want to implement the item mapping yourself, I think.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          galrub
          wrote on last edited by
          #4

          well, I solved it by using the painter:
          @
          void MyTypeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
          const QModelIndex &index) const {
          int value = index.model()->data(index, Qt::DisplayRole).toInt();
          QString text;
          switch(value){
          case MyTypes::Type_A:
          text = "type A";
          break;
          //.... and so on
          };
          QRect r = option.rect.adjusted(2, 2, -2, -2);
          painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignVCenter|Qt::AlignLeft|Qt::TextWordWrap, text, &r);
          @

          it is a quick and dirty solution but will do for the time table I have for this, a proxy model might be a better solution, but, the delegate also handles the editing part of the cell, so, it's a good 'all in one' solution

          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