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. custom qstyleditemdelegate (checkbox) displays number instead of checkbox
Forum Updated to NodeBB v4.3 + New Features

custom qstyleditemdelegate (checkbox) displays number instead of checkbox

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 2 Posters 931 Views
  • 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.
  • A Offline
    A Offline
    andi456
    wrote on last edited by andi456
    #1

    Hi, [set to solved, although the delegate does not work, but concrete question is answered.]

    in order to paint the surrounding rectangle of the checkbox in different colours according to some value, I had to subclass QStyledItemDelegate. At first glance, it seems to work, but when I try to check or uncheck the box a number is displayed instead of the box, I've just checked. (Actually there remains a unchanged checkbox in the center of the cell.) [It is integrated in an tableview.]

    The number seems to be the Qt::CheckState constant. But how could I debug this strange behaviour?

    Kind regards,

    Andreas

    JonBJ 1 Reply Last reply
    0
    • A andi456

      Hi, [set to solved, although the delegate does not work, but concrete question is answered.]

      in order to paint the surrounding rectangle of the checkbox in different colours according to some value, I had to subclass QStyledItemDelegate. At first glance, it seems to work, but when I try to check or uncheck the box a number is displayed instead of the box, I've just checked. (Actually there remains a unchanged checkbox in the center of the cell.) [It is integrated in an tableview.]

      The number seems to be the Qt::CheckState constant. But how could I debug this strange behaviour?

      Kind regards,

      Andreas

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @andi456
      Sounds like: somewhere in your delegate's paint() you are being asked to show the checkbox with a specified state but you are instead treating it as a number to show as the value?

      A 1 Reply Last reply
      0
      • JonBJ JonB

        @andi456
        Sounds like: somewhere in your delegate's paint() you are being asked to show the checkbox with a specified state but you are instead treating it as a number to show as the value?

        A Offline
        A Offline
        andi456
        wrote on last edited by
        #3

        @JonB Yes, I'm also suspecting the paint method. I copied some code from an example somewhere....

                auto stateFlag = opt.checkState == Qt::Checked ? QStyle::State_On : QStyle::State_Off;
                opt.state.setFlag(stateFlag, true);
        

        Maybe it's this snippet, which causes the trouble.

        JonBJ 1 Reply Last reply
        0
        • A andi456

          @JonB Yes, I'm also suspecting the paint method. I copied some code from an example somewhere....

                  auto stateFlag = opt.checkState == Qt::Checked ? QStyle::State_On : QStyle::State_Off;
                  opt.state.setFlag(stateFlag, true);
          

          Maybe it's this snippet, which causes the trouble.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @andi456
          I think you should show a minimal example of the paint() override you have defined.

          A 1 Reply Last reply
          0
          • JonBJ JonB

            @andi456
            I think you should show a minimal example of the paint() override you have defined.

            A Offline
            A Offline
            andi456
            wrote on last edited by
            #5

            @JonB Ok, here are the relevant parts of the paint method:

             void CheckBoxZBuchDeckDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
               QStyleOptionViewItem opt = option;
            //some code to colorize the background...
               initStyleOption(&opt, index);
               auto * widget = opt.widget;
               auto * style = widget->style();
               int zeile = index.row();
               if ((style) && (zeile < index.model()->rowCount() - 1)) {
                    opt.state.setFlag(QStyle::State_HasFocus, false);
                    opt.features.setFlag(QStyleOptionViewItem::HasDisplay, false);
                    opt.features.setFlag(QStyleOptionViewItem::HasDecoration, false);
                    opt.features.setFlag(QStyleOptionViewItem::HasCheckIndicator, false);
                    style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
                    qDebug() << "Checkbox delegate paint method called";
                    opt.rect = this->getCheckBoxRect(opt);
                    auto stateFlag = opt.checkState == Qt::Checked ? QStyle::State_On : QStyle::State_Off;
                    opt.state.setFlag(stateFlag, true);
                    style->drawPrimitive(QStyle::PE_IndicatorViewItemCheck, &opt, painter, widget);
                } else {
                   painter->setPen(Qt::black);
                   int spalte = index.column();
                   int sum_Flags = this->get_intFromVariant(index.siblingAtColumn(spalte).data(Qt::DisplayRole));
                   QString cell_text = QString::number(sum_Flags);
                   painter->drawText(opt.rect, Qt::AlignCenter, cell_text);
               }
            }
            

            The last line shows the sum of all flags set by the checkbox...

            JonBJ 1 Reply Last reply
            0
            • A andi456

              @JonB Ok, here are the relevant parts of the paint method:

               void CheckBoxZBuchDeckDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
                 QStyleOptionViewItem opt = option;
              //some code to colorize the background...
                 initStyleOption(&opt, index);
                 auto * widget = opt.widget;
                 auto * style = widget->style();
                 int zeile = index.row();
                 if ((style) && (zeile < index.model()->rowCount() - 1)) {
                      opt.state.setFlag(QStyle::State_HasFocus, false);
                      opt.features.setFlag(QStyleOptionViewItem::HasDisplay, false);
                      opt.features.setFlag(QStyleOptionViewItem::HasDecoration, false);
                      opt.features.setFlag(QStyleOptionViewItem::HasCheckIndicator, false);
                      style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
                      qDebug() << "Checkbox delegate paint method called";
                      opt.rect = this->getCheckBoxRect(opt);
                      auto stateFlag = opt.checkState == Qt::Checked ? QStyle::State_On : QStyle::State_Off;
                      opt.state.setFlag(stateFlag, true);
                      style->drawPrimitive(QStyle::PE_IndicatorViewItemCheck, &opt, painter, widget);
                  } else {
                     painter->setPen(Qt::black);
                     int spalte = index.column();
                     int sum_Flags = this->get_intFromVariant(index.siblingAtColumn(spalte).data(Qt::DisplayRole));
                     QString cell_text = QString::number(sum_Flags);
                     painter->drawText(opt.rect, Qt::AlignCenter, cell_text);
                 }
              }
              

              The last line shows the sum of all flags set by the checkbox...

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @andi456 said in custom qstyleditemdelegate (checkbox) displays number instead of checkbox:

              if ((style) && (zeile < index.model()->rowCount() - 1)) {

              This should be zeile <= index.model()->rowCount() - 1 or zeile < index.model()->rowCount(). With your code whenever the index passed in is the last one (index.model()->rowCount() - 1) you go into the else and paint the number, hence the behaviour you see?

              This is why it's always good to show code!

              A 1 Reply Last reply
              0
              • JonBJ JonB

                @andi456 said in custom qstyleditemdelegate (checkbox) displays number instead of checkbox:

                if ((style) && (zeile < index.model()->rowCount() - 1)) {

                This should be zeile <= index.model()->rowCount() - 1 or zeile < index.model()->rowCount(). With your code whenever the index passed in is the last one (index.model()->rowCount() - 1) you go into the else and paint the number, hence the behaviour you see?

                This is why it's always good to show code!

                A Offline
                A Offline
                andi456
                wrote on last edited by
                #7

                @JonB No, I don't think so, because I want to have a checkbox in all cells except in the last row. As the rows are indexed based on zero, the index of the last line would be rowCount -1.

                JonBJ 1 Reply Last reply
                0
                • A andi456

                  @JonB No, I don't think so, because I want to have a checkbox in all cells except in the last row. As the rows are indexed based on zero, the index of the last line would be rowCount -1.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @andi456
                  My (next) guess: you have

                  opt.features.setFlag(QStyleOptionViewItem::HasCheckIndicator, false);
                  ...
                  style->drawPrimitive(QStyle::PE_IndicatorViewItemCheck, &opt, painter, widget);
                  

                  [BTW, that should be QStyle::PE_IndicatorItemViewItemCheck now.]
                  So you ask it to show the checkstate value but have no check indicator, maybe that shows it is a number?

                  Maybe you need to restore that flag before painting the checkbox, or take a look at, say, https://forum.qt.io/topic/4277/solved-painting-in-custom-delegate where the option is copied to a modifiedOption for the checkbox?

                  A 2 Replies Last reply
                  0
                  • JonBJ JonB

                    @andi456
                    My (next) guess: you have

                    opt.features.setFlag(QStyleOptionViewItem::HasCheckIndicator, false);
                    ...
                    style->drawPrimitive(QStyle::PE_IndicatorViewItemCheck, &opt, painter, widget);
                    

                    [BTW, that should be QStyle::PE_IndicatorItemViewItemCheck now.]
                    So you ask it to show the checkstate value but have no check indicator, maybe that shows it is a number?

                    Maybe you need to restore that flag before painting the checkbox, or take a look at, say, https://forum.qt.io/topic/4277/solved-painting-in-custom-delegate where the option is copied to a modifiedOption for the checkbox?

                    A Offline
                    A Offline
                    andi456
                    wrote on last edited by
                    #9

                    @JonB Yes the HasCheckIndicator flag was wrong. Now the checkbox seems to work, but is being displayed twice, so I guess, I need to center it somehow in the paint method.

                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @andi456
                      My (next) guess: you have

                      opt.features.setFlag(QStyleOptionViewItem::HasCheckIndicator, false);
                      ...
                      style->drawPrimitive(QStyle::PE_IndicatorViewItemCheck, &opt, painter, widget);
                      

                      [BTW, that should be QStyle::PE_IndicatorItemViewItemCheck now.]
                      So you ask it to show the checkstate value but have no check indicator, maybe that shows it is a number?

                      Maybe you need to restore that flag before painting the checkbox, or take a look at, say, https://forum.qt.io/topic/4277/solved-painting-in-custom-delegate where the option is copied to a modifiedOption for the checkbox?

                      A Offline
                      A Offline
                      andi456
                      wrote on last edited by
                      #10

                      @JonB Tried to implement the hints given in the post you linked. It did center the checkBoxes shown initially, but the checkBox shown, if I try to change the data, is being drawn to the left of the initial checkBox.

                      Does one need to adjust the createEditor function too?

                      1 Reply Last reply
                      0
                      • A andi456 has marked this topic as solved on

                      • Login

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