From 10:00 CET Friday 22nd November we will adjust how the server works to deal with some recently reported problems. Therefore there may be a load problem, if you experience more problems than usual trying to access the forum then please PM AndyS or any of the moderators so they can inform me.

Custom widget in QScrollArea

  • 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
    	virtual void paintEvent(QPaintEvent* e)
    		QPainter painter(this);
    		painter.fillRect(geometry(), Qt::red);
    class ScrollTest : public QMainWindow
    	ScrollTest(QWidget *parent = 0)	: QMainWindow(parent)
    		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));

    Here is a Screenshot: image

    What's wrong?

    Edit raven-worx: added code tags

  • Moderators

    did you try QScrollArea::setWidgetResizable(true)?

    But the question is what you want to achieve, because actually a scrollarea's purpose is to show scrollbars if needed.

    If setWidgetResizable() doesn't do what you want you need to subclass QScrollArea and reimplement it's resizeEvent() handler and resize the widget manually.

  • This is just a simple test dialog that shows my problem. 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.

    If i call setWidgetResizable(true) i don't see the scrollbars and only a portion of the widget.

  • The solution is:

    painter.fillRect(QRect(0,0, width(), height()), Qt::red);

    geometry() delivers negative x/y values.

Log in to reply