Search code examples
c++qtqgraphicsviewqcheckboxqstyle

How to add checkbox in the bottom left corner of QGraphicsView?


I want to add a checkbox in the left corner of QGraphicsView .

I tried painting it directly in painEvent of my view .

void BhGraphicsView::paintEvent(QPaintEvent* event)
{
    QGraphicsView::paintEvent(event);
    QStyleOptionButton opt;
    opt.state = QStyle::State_Active | QStyle::State_Enabled;
    opt.rect = QRect(x,y,300,300);
    QPainter painter(viewport());
    //histogram_cbox_ is a QCheckBox
    histogram_cbox_->style()->drawControl(QStyle::CE_CheckBox, &opt, &painter);
  // ....
 }

I didn't find QStyleOptionCheckBox so I used QStyleOptionButton .

But the problem is it doesn't show the text of the QCheckbox .

How can I draw a clickable checkbox on a QGraphicsView ?!

enter image description here


Solution

  • You can add a QCheckBox to the QGraphicsView in a layout :

    QCheckBox * checkBox = new QCheckBox();
    QGridLayout * layout = new QGridLayout(ui->myView);
    layout->addWidget(checkBox,0,0,0,0,Qt::AlignBottom | Qt::AlignLeft);