0

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

uchar
  • 2,552
  • 4
  • 29
  • 50

1 Answers1

3

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);
Nejat
  • 31,784
  • 12
  • 106
  • 138
  • I want to put the checkbox on the view not bellow it . My code add the checkbox but it doesn't draw the text of check box http://tinypic.com/view.php?pic=v5l7au&s=8#.U4eRVvmSzhY – uchar May 30 '14 at 20:00
  • 1
    This will put the checkbox on the view. You can also put the above code in the constructor of your custom view and replace `ui->myView` with `this`. – Nejat May 31 '14 at 04:28