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. [SOLVED] - QStyledItemDelegate - set text color when row is selected
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] - QStyledItemDelegate - set text color when row is selected

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 10.0k 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.
  • M Offline
    M Offline
    maximus
    wrote on last edited by
    #1

    Hi,

    I have a custom QStyleItemDelegate, and would like to have the default display behavior on all the cells. (selected = blue background, white text, ...)
    I have 3 custom widget for editing, and in the paint method I just display some text instead of the widget when theses cell are not being edited.

    Here is the current Ui:
    https://www.dropbox.com/s/vkxb9n0om15m821/DelegateProb.png

    What I would like is my custom cell to behave like the other normal ones; white text when selected (already working), black text when a cell on the same row is being edited (not working). I have checked the paint code of QStyledItemDelegate.cpp but it's not helping much.

    I found this but it's not the same use-case like my QTableView:
    http://stackoverflow.com/questions/18568594/correct-highlighting-with-qt-custom-delegates
    Would appreciate some help ;)

    Here is my paint Delegate method :

    @void IntervalDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {

    QStyleOptionViewItemV4 opt = option;
    initStyleOption(&opt, index);
    
    
    
    if (option.state & QStyle::State_Selected) {
        painter->fillRect(option.rect, option.palette.highlight());
        painter->setPen(Qt::white);
        painter->setBrush(option.palette.highlightedText());
    }
    else
    {
        painter->setPen(QPen(option.palette.foreground(), 0));
        painter->setBrush(qvariant_cast<QBrush>(index.data(Qt::ForegroundRole)));
    }
    
    
    if (option.state & QStyle::State_HasFocus) {  /// Whats the state when editing a cell on the same row?
         painter->setPen(Qt::red);
    }
    
    
    
    /// -------------------------------------  POWER -----------------------------------------------
    if (index.column() == 2) {
        Interval interval = index.model()->data(index, Qt::DisplayRole).value<Interval>();
        QString stepTypePower = Interval::getStepTypeToString(interval.getPowerStepType());
        int ftpStart = interval.getFTP_start() *100;
        int ftpEnd = interval.getFTP_end() *100;
        int range = interval.getFTP_range();
        double leftTarget = interval.getLeftPowerTarget();
    
        QString toShow = stepTypePower;
        if (interval.getPowerStepType() == Interval::PROGRESSIVE) {
            toShow += " [" + QString::number(ftpStart) + "-" + QString::number(ftpEnd) + "]";
        }
        else if (interval.getPowerStepType() == Interval::FLAT) {
            toShow += " [" + QString::number(ftpStart) + "]";
        }
    
        /// Show balance percentage
        if (leftTarget != -1) {
            int targetLeftPercent = leftTarget*100;
            toShow += tr("\nLeft:") + QString::number(targetLeftPercent) + tr(" Right:") + QString::number(100-targetLeftPercent);
        }
        QStyledItemDelegate::paint(painter, option, index); /// to get gray background when row selected..
        painter->drawText(option.rect, Qt::AlignCenter | Qt::AlignVCenter, toShow );
    
    }
    
    /// ------------------------------------- Cadence -----------------------------------------------
    else if (index.column() == 3) {
        Interval interval = index.model()->data(index, Qt::DisplayRole).value<Interval>();
        QString stepTypeCadence = Interval::getStepTypeToString(interval.getCadenceStepType());
        int cadenceStart = interval.getCadence_start();
        int cadenceEnd = interval.getCadence_end();
        int range = interval.getCadence_range();
    
        QString toShow = stepTypeCadence;
        if (interval.getCadenceStepType() == Interval::PROGRESSIVE) {
            toShow += " [" + QString::number(cadenceStart) + "-" + QString::number(cadenceEnd) + "]";
        }
        else if (interval.getCadenceStepType() == Interval::FLAT) {
            toShow += " [" + QString::number(cadenceStart) + "]";
        }
        QStyledItemDelegate::paint(painter, option, index); /// to get gray background when row selected..
        painter->drawText(option.rect, Qt::AlignCenter | Qt::AlignVCenter, toShow );
    }
    
    /// ------------------------------------- Heart rate -----------------------------------------------
    else if (index.column() == 4) {
        Interval interval = index.model()->data(index, Qt::DisplayRole).value<Interval>();
        QString stepTypeHeartrate = Interval::getStepTypeToString(interval.getHRStepType());
        int hrStart = interval.getHR_start() * 100;
        int hrEnd = interval.getHR_end() * 100;
        int range = interval.getHR_range();
    
        QString toShow = stepTypeHeartrate;
        if (interval.getHRStepType() == Interval::PROGRESSIVE) {
            toShow += " [" + QString::number(hrStart) + "-" + QString::number(hrEnd) + "]";
        }
        else if (interval.getHRStepType() == Interval::FLAT) {
            toShow += " [" + QString::number(hrStart) + "]";
        }
        QStyledItemDelegate::paint(painter, option, index); /// to get gray background when row selected..
        painter->drawText(option.rect, Qt::AlignCenter | Qt::AlignVCenter, toShow );
    }
    
    
    else {
        QStyledItemDelegate::paint(painter, option, index);
    }
    

    }@


    Free Indoor Cycling Software - https://maximumtrainer.com

    1 Reply Last reply
    0
    • M Offline
      M Offline
      maximus
      wrote on last edited by
      #2

      If I comment
      @QStyledItemDelegate::paint(painter, option, index); @
      before
      @painter->drawText(option.rect, Qt::AlignCenter | Qt::AlignVCenter, toShow );@
      on the Power cell for example, I get this result:
      https://www.dropbox.com/s/q9nu5jsvhr9rxq7/delgatev22.png


      Free Indoor Cycling Software - https://maximumtrainer.com

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        It looks like you are using the wrong tool here since you are only drawing text. Why not provide directly the right value from your model ? Or use a QIdentityProxyModel to do the needed transformation ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maximus
          wrote on last edited by
          #4

          You are a genius !
          I moved all the code from the delegate to my model data function, and use the same code for "role == Qt::DisplayRole".
          It's working 100% perfect and no hacking needed in the paint method haha.
          Only thing I can't do here is set the text alignement to centered, it is left aligned by default

          thanks you!!!! :)


          Free Indoor Cycling Software - https://maximumtrainer.com

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Qt::TextAlignmentRole ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • M Offline
              M Offline
              maximus
              wrote on last edited by
              #6

              Thanks SGaist, i'm beginning to understand the use of a model now :)
              Really powerful!


              Free Indoor Cycling Software - https://maximumtrainer.com

              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