Qt : QScroolArea paintEvent

Qt : QScroolArea PaintEvent

I want to create paintEvent in QScroolArea. This is a tutorial how to implement QScroolArea paintEvent method.  I have confused how to create drawingEvent in the QScroolArea. So, after searching and try some of method, I create tutorial  how QScroolArea implement paintEvent. This is the steps how to implemnt QScroolArea paintEvent method :

  1. Create a widget. Implement paintEvent mehtod in this widget.
  2. Create QScroolArea. add the last widget int this QScroorArea.

This is a simple method how QScroolArea implement paintEvent method.

void drawingWidget::paintEvent(QPaintEvent *event)
{
    QStylePainter painter(this);
    painter.drawPixmap(0, 0, pixmap);
}

void drawingWidget::drawingData()
{
    pixmap = QPixmap(ncol, nrow);   //set widget size
    resize(ncol, nrow);             //resize drawing Widget size
	pixmap.fill(this, 0, 0);

    QPainter paint(&pixmap);        //create painter object
	paint.initFrom(this);

    QLinearGradient linearGrad(0.0, 0.0, nrow, ncol);   //create linear gradient
    linearGrad.setColorAt(0.2, Qt::blue);
    linearGrad.setColorAt(1, Qt::green);
    paint.setBrush(linearGrad);
    paint.fillRect(rect(), linearGrad);

    update();
}

I am create a method drawingData to draw a QLinearGradient at the widget. I am adding paintEvent method in the widget, so when we change size (resize window), the program will automatic redrawing picture. This is the output from this QScroolArea paintEvent method :

paintQScroolArea
paintQScroolArea

You can get the complete code how to implementated QScroolArea paintEvent at here

 

Add a Comment

Your email address will not be published. Required fields are marked *