I have seen many examples on how to use a paintevent, but I just cannot get it to work.
I have in my .ui file a label named ‘image’ and I am trying to paint inside it. I fail miserably. In most of the examples I’ve seen they use the
QLabel::paintEvent(e)
but I cannot use this, I get:
error: cannot call member function 'virtual void QLabel::paintEvent(QPaintEvent*)' without object
And, when I use
ui->image->paintEvent(e);
I get
/usr/include/qt4/QtGui/qlabel.h:141: error: 'virtual void QLabel::paintEvent(QPaintEvent*)' is protected
I seem to be missing something… That’s the part of my code that I try to implement this:
void crop_my_image::paintEvent(QPaintEvent *e)
{
ui->image->paintEvent(e);
QPainter painter(ui->image);
painter.setPen(QPen(QBrush(QColor(0,0,0,180)),1,Qt::DashLine));
painter.setBrush(QBrush(QColor(255,255,255,120)));
painter.drawRect(selectionRect);
}
crop_my_image is of QDialog type!
PS: If, instead of ui->image->paintEvent(e); I use QDialog::paintEvent(e); I can successfully draw on my dialog, so I should be in the right path!
Thanks in advance for any answers!
You have to do exactly the same with label what you did with QDialog, which is create class which will inherit from QLabel and implement paintEvent function. Example:
And than you will be able to do:
But firstly, why event you want to invoke this method?