PDA

View Full Version : Custom widget in QScrollArea



ElRudi
14th January 2016, 12:46
Hi,

i create a widget in a QScrollArea that should fill the entire widget with red color, but when i scroll to the right, i see that it's not filled completely. The frame is drawn correctly. Here is my code:


#include <QMainWindow>
#include <QPainter>
#include <QScrollArea>

class MyWidget : public QWidget
{
public:

virtual void paintEvent(QPaintEvent* e)
{
QPainter painter(this);
painter.fillRect(geometry(), Qt::red);
}
};

class ScrollTest : public QMainWindow
{
Q_OBJECT

public:
ScrollTest(QWidget *parent = 0) : QMainWindow(parent)
{
setGeometry(QRect(100,100,230,230));

MyWidget* widget = new MyWidget;
widget->setGeometry(QRect(0,0, 250, 250));
widget->setStyleSheet("border: 3px solid blue;");

QScrollArea* area = new QScrollArea(this);
area->setGeometry(QRect(0,0, 200, 200));
area->setWidget(widget);
}
};



This is a simple test dialog. The real widget is an image viewer/editor. I tried QLabel to draw the image, but this is very slow when you zoom in with large images. What i want to do is to zoom and scroll through the image.

What's wrong?

anda_skoa
14th January 2016, 13:42
Have you tried filling (0, 0, width, height)?

Maybe geometry() has negative x/y

Cheers,
_

ElRudi
14th January 2016, 13:53
That's it! :) Thank you!