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


  • Qt Champions 2016

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

    Kind regards.



  • @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



  • @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);
    }
    

  • Qt Champions 2016

    @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.