Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Custom widget in QScrollArea

Custom widget in QScrollArea

Scheduled Pinned Locked Moved Solved General and Desktop
qt4.8.6qscrollarea
4 Posts 2 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ElRudi
    wrote on 14 Jan 2016, 10:44 last edited by raven-worx
    #1

    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);
    	}
    };
    

    Here is a Screenshot: image

    What's wrong?

    Edit raven-worx: added code tags

    1 Reply Last reply
    0
    • R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 14 Jan 2016, 12:09 last edited by
      #2

      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.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • E Offline
        E Offline
        ElRudi
        wrote on 14 Jan 2016, 12:18 last edited by ElRudi
        #3

        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.

        1 Reply Last reply
        0
        • E Offline
          E Offline
          ElRudi
          wrote on 14 Jan 2016, 12:55 last edited by
          #4

          The solution is:

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

          geometry() delivers negative x/y values.

          1 Reply Last reply
          0

          4/4

          14 Jan 2016, 12:55

          • Login

          • Login or register to search.
          4 out of 4
          • First post
            4/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved