I have a problem on the behavior of QCheckBox. I am under Qt5.3.2/MinGW
I create a QGraphicsScene where i add severals QCheckBox via QGraphicsProxyWidget.
To be able to do drag and drop, zoom in / out, I reimplemented the virtual method eventFilter(QObject, QEvent) into QMainWindows and I apply it to my scene:
scene->installEventFilter(this);
But I can’t catch QCheckBox signal with QObject::connect :
connect(checkBox, &QCheckBox::clicked, [=](bool value){
qDebug() << "checkBox->objectName() : " << checkBox->objectName();
qDebug() << "value : " << value; });
If I don’t apply event filter on my scene, my QCheckBox work perfectly.
If now I apply event filter on my QCheckbox :
checkBox->installEventFilter(this);
I receive the event of the first one QCheckBox selected. But if I click on another QCheckBox it is the event of the first QCheckBox selected who is received. QCheckBox status are not modified too and I need to change status into QMainWindows::eventfilter :
if(checkbox->isChecked()) checkbox->setChecked(false);
else if(!checkbox->isChecked()) checkbox->setChecked(true);
MainWindow.cpp :
void MainWindow::Display()
{
scene = new QGraphicsScene();
scene->setObjectName("scene");
scene->installEventFilter(this);
for(int i=;i<10;++i){
QCheckBox *checkBox = new QCheckBox("ID"+QString::number(i));
checkBox->setObjectName("checkBox"+QString::number(i));
checkBox->installEventFilter(this);
checkBox->setChecked(true);
QString style = "QCheckBox {background : white;}";
checkBox->setStyleSheet(style);
// connect(checkBox, &QCheckBox::clicked, [=](bool value){
// qDebug() << "checkBox->objectName() : " << checkBox->objectName();
// qDebug() << "value : " << value; });
QGraphicsProxyWidget* proxyWidget = scene->addWidget(checkBox);
proxyWidget->setObjectName("proxyWidget"+QString::number(i));
proxyWidget->setScale(2);
proxyWidget->setPos(QPointF(i*50, 24));
proxyWidget->setFlag(QGraphicsItem::ItemIsSelectable);
proxyWidget->setZValue(2.0);
}
scene->setSceneRect(QRect(0, 0, 500, 200));
ui->graphicsView->setScene(scene);
ui->graphicsView->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
}
bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
qDebug() << "object->objectName() : " << object->objectName();
qDebug() << "event->type() : " << event->type();
if (event->type() == QEvent::MouseButtonDblClick)
{
if(object->objectName().contains("checkBox",Qt::CaseInsensitive))
{
QCheckBox *checkbox = dynamic_cast<QCheckBox *>(object);
if(checkbox != nullptr)
{
event->setAccepted(true);
if(checkbox->isChecked()) checkbox->setChecked(false);
else if(!checkbox->isChecked()) checkbox->setChecked(true);
checkbox->update();
return true;
}
else return false;
}
else return false;
}
else
{
qDebug() << "default return";
// standard event processing
return QObject::eventFilter(object, event);
}
}
What is it that I'm doing wrong? Thank you.