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. How to draw checkable checkbox with moved indicator?
Forum Updated to NodeBB v4.3 + New Features

How to draw checkable checkbox with moved indicator?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 656 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.
  • Q Offline
    Q Offline
    qwe3
    wrote on 2 Jul 2021, 17:55 last edited by
    #1

    Hi,

    I would like to draw checkable checkbox with indicator in position (0, heightOfCheckBox()/2 - heightOfIndicator()/2 ).

    I have a class:

    #include "mywidget.h"
    #include <QStyle>
    #include <QStyleOptionComboBox>
    
    myWidget::myWidget(QWidget *parent):QCheckBox(parent)
    {
        setFixedSize(200,50);
        setCheckable(true);
    }
    
    void myWidget::paintEvent(QPaintEvent *event)
    {
        QPainter painter(this);
        painter.fillRect(rect(), Qt::red);
        QStyleOptionComboBox option;
        option.initFrom(this);
        option.rect = QRect(0,rect().height()/2-5,10,10);
        style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, &painter, this);
    }
    
    bool myWidget::hitButton(const QPoint &pos) const
    {
        return rect().contains(pos);
    }
    

    And I can draw that moved indicator, but when I clicked in checkBox, it doesn't change his state. What is a proper solution?

    J 1 Reply Last reply 2 Jul 2021, 18:03
    0
    • Q qwe3
      2 Jul 2021, 17:55

      Hi,

      I would like to draw checkable checkbox with indicator in position (0, heightOfCheckBox()/2 - heightOfIndicator()/2 ).

      I have a class:

      #include "mywidget.h"
      #include <QStyle>
      #include <QStyleOptionComboBox>
      
      myWidget::myWidget(QWidget *parent):QCheckBox(parent)
      {
          setFixedSize(200,50);
          setCheckable(true);
      }
      
      void myWidget::paintEvent(QPaintEvent *event)
      {
          QPainter painter(this);
          painter.fillRect(rect(), Qt::red);
          QStyleOptionComboBox option;
          option.initFrom(this);
          option.rect = QRect(0,rect().height()/2-5,10,10);
          style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, &painter, this);
      }
      
      bool myWidget::hitButton(const QPoint &pos) const
      {
          return rect().contains(pos);
      }
      

      And I can draw that moved indicator, but when I clicked in checkBox, it doesn't change his state. What is a proper solution?

      J Offline
      J Offline
      JonB
      wrote on 2 Jul 2021, 18:03 last edited by JonB 7 Feb 2021, 18:10
      #2

      @qwe3
      This is not my area, but for a checkbox why are you going QStyleOptionComboBox option; instead of QStyleOptionButton? I think QStyle::State_On; shows the check mark. Which is what I think you are asking about.

      1 Reply Last reply
      2
      • Q Offline
        Q Offline
        qwe3
        wrote on 2 Jul 2021, 18:11 last edited by qwe3 7 Feb 2021, 18:21
        #3

        I have other problem too: when I change numbers in option.rect the indicator change position ( x is not in 0 ):

        option.rect = QRect(0,rect().height()/2-10,20,20);
        

        Yeah, I think QStyleOptionButton is better. But I would like to automatic change check mark when I click on checkBox. But maybe I have to reimplement mousePressEvent and call repaint() and in paintEvent() have if-statement to change check mark?

        M 1 Reply Last reply 3 Jul 2021, 08:12
        0
        • Q qwe3
          2 Jul 2021, 18:11

          I have other problem too: when I change numbers in option.rect the indicator change position ( x is not in 0 ):

          option.rect = QRect(0,rect().height()/2-10,20,20);
          

          Yeah, I think QStyleOptionButton is better. But I would like to automatic change check mark when I click on checkBox. But maybe I have to reimplement mousePressEvent and call repaint() and in paintEvent() have if-statement to change check mark?

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 3 Jul 2021, 08:12 last edited by mrjj 7 Mar 2021, 08:17
          #4

          @qwe3

          Hi
          The normal paintEvent is like

          void QCheckBox::paintEvent(QPaintEvent *)
          {
              QStylePainter p(this);
              QStyleOptionButton opt;
              initStyleOption(&opt);
              p.drawControl(QStyle::CE_CheckBox, opt);
          }
          
          and drawControl uses 
                 subopt.rect = subElementRect(isRadio ? SE_RadioButtonIndicator
                                                           : SE_CheckBoxIndicator, btn, widget);
          

          to get the location of the "hot area" where you can click it. so if you change the option.rect, it will not work as expected as it's not the whole area that is used for hit testing.
          4

          • Also currently I'm not sure it will be aware of being checked.
            Try adding
            option.state |= QStyle::State_On; and see.
            not sure it works for QStyleOptionComboBox as normally one uses QStyleOptionButton.

          https://code.woboq.org/qt5/qtbase/src/widgets/styles/qcommonstyle.cpp.html#1313

          • But I'm a bit confused about the actual goal since you inherited a CheckBox.

          Do you want a special button-like widget were once checked, it will show the checkmark in some position you want. Or should be like a normal QCheckBox and you just want to center the Checkmark?

          Im asking as QCheckBox already center on y ?
          alt text

          1 Reply Last reply
          1

          1/4

          2 Jul 2021, 17:55

          • Login

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