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. Clickable QCheckBox how to?

Clickable QCheckBox how to?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qcheckboxqradiobuttonqt5.5.1
6 Posts 3 Posters 3.7k 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.
  • H Offline
    H Offline
    HPCTECH
    wrote on last edited by HPCTECH
    #1

    Hello,

    I want to know how can I make QCheckBox clickable for whole widget area (see the image below plz)
    http://i.imgur.com/38LsxH0.png

    kshegunovK H 2 Replies Last reply
    0
    • H HPCTECH

      Hello,

      I want to know how can I make QCheckBox clickable for whole widget area (see the image below plz)
      http://i.imgur.com/38LsxH0.png

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @HPCTECH
      Hello,
      How are building that interface - what is the widget containing the text?

      Kind regards.

      Read and abide by the Qt Code of Conduct

      H 1 Reply Last reply
      0
      • kshegunovK kshegunov

        @HPCTECH
        Hello,
        How are building that interface - what is the widget containing the text?

        Kind regards.

        H Offline
        H Offline
        HPCTECH
        wrote on last edited by
        #3

        @kshegunov

        How are building that interface

        it's a widget based

        what is the widget containing the text?

        The text comes from QCheckBox itself ... see the source code from here:
        https://bugreports.qt.io/secure/attachment/53027/AndroidQCheckboxExtendWidget.7z

        1 Reply Last reply
        0
        • H HPCTECH

          Hello,

          I want to know how can I make QCheckBox clickable for whole widget area (see the image below plz)
          http://i.imgur.com/38LsxH0.png

          H Offline
          H Offline
          HPCTECH
          wrote on last edited by
          #4

          @HPCTECH

          I use a dump solution as following so I wonder does anyone suggest better one?

          bool MainWindow::eventFilter(QObject *object, QEvent *event)
          {
              if (object==ui->checkBox) {
                  QMouseEvent*mouseEvent = static_cast<QMouseEvent*>(event);
                  if (event->type()==QEvent::MouseButtonPress && mouseEvent->button()==Qt::LeftButton) {
          
                      if (ui->checkBox->isChecked())
                          ui->checkBox->setChecked(false);
                      else
                          ui->checkBox->setChecked(true);
                      return true;
                  }
                  return false;
              }
              return QMainWindow::eventFilter(object,event);
          }
          
          kshegunovK 1 Reply Last reply
          0
          • H HPCTECH

            @HPCTECH

            I use a dump solution as following so I wonder does anyone suggest better one?

            bool MainWindow::eventFilter(QObject *object, QEvent *event)
            {
                if (object==ui->checkBox) {
                    QMouseEvent*mouseEvent = static_cast<QMouseEvent*>(event);
                    if (event->type()==QEvent::MouseButtonPress && mouseEvent->button()==Qt::LeftButton) {
            
                        if (ui->checkBox->isChecked())
                            ui->checkBox->setChecked(false);
                        else
                            ui->checkBox->setChecked(true);
                        return true;
                    }
                    return false;
                }
                return QMainWindow::eventFilter(object,event);
            }
            
            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            @HPCTECH
            Well I did it in a similar fashion, here's my code:

            MainWindow::MainWindow(QWidget *parent) :
            	QMainWindow(parent),
            	ui(new Ui::MainWindow)
            {
            	ui->setupUi(this);
            	ui->checkBox->installEventFilter(this);
            }
            
            bool MainWindow::eventFilter(QObject * object, QEvent * event)
            {
            	if (event->type() == QEvent::MouseButtonRelease)
            	{
            		QCheckBox * box = qobject_cast<QCheckBox *>(object);
            		box->setChecked(!box->isChecked());
            
            		return true;
            	}
            
            	return false;
            }
            

            Aside from that I don't know any better way. It seems that the checkbox will not handle the events that are generated on a blank background.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            1
            • D Offline
              D Offline
              DavidEGrayson
              wrote on last edited by DavidEGrayson
              #6

              QCheckBox inherits from QAbstractButton, which provides a hitButton method that controls which parts of the button are clickable. All you need to do is make a new subclass of QCheckBox and override hitButton so it always returns true:

              class BigHitCheckBox : public QCheckBox
              {
                bool hitButton(const QPoint & pos) const override
                {
                  Q_UNUSED(pos);
                  return true;
                }
              };
              

              This is much simpler than the solutions previously mentioned, and it should play well with the hovering, disabling, and tristate features of the checkbox without any extra effort.

              By the way, this makes the entire checkbox widget clickable, but it doesn't make other widgets outside the checkbox clickable. I don't think the OP needed that.

              --David

              1 Reply Last reply
              2

              • Login

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