Clickable QCheckBox how to?
-
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 -
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 -
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); }
-
@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.
-
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