Results 1 to 16 of 16

Thread: drawContents in QScrollArea (Qt4)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawContents in QScrollArea (Qt4)

    You are not using QScrollArea correctly. You need to follow 1 of two paths

    1. Use QScrollArea, you will want to subclass the widget that the QScrollArea is showing, and implement the paintEvent and sizeHint(), the scrollbars will be shown depending on the size of the widget.

    2. Use QAbstractScrollArea, with this one, you will want to paint inside the QAbstractScrollArea's paintEvent(with a painter opened on the viewport), you will be responsible for setting the scrollbar page step, range, and value.

    Matt

  2. #2
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: drawContents in QScrollArea (Qt4)

    Hi, I have implemented sizeHint() of the widget that the QScrollArea is showing, as the previous post coments, and it returns the size of the image. But the scroll bars are still wrong (when I move them to the bottom, the image is not in the bottom). The only way that I have been successful is making a resize of the widget in it's constructor to the size of the image, and the same resize in the paintEvent. If I don't put any of both, the scrollbars are still wrong. Anybody could explain me why or what am I doing wrong? Thanxs and sorry for my poor english.

  3. #3
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: drawContents in QScrollArea (Qt4)

    Maybe you are setting your sizeHint too late, e.g. loading of image is done after layouting the widget?
    It's nice to be important but it's more important to be nice.

  4. #4
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: drawContents in QScrollArea (Qt4)

    The image in the widget is painted in the paintEvent method with qpainter. I dont' understand what you mean with: "setting your sizeHint too late". I only have implemented it, I have to call it anywhere directly??

  5. #5
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: drawContents in QScrollArea (Qt4)

    The QLayout calls the sizeHint at some point, usually directly before QWidget::show is called. The question is wether your sizeHint can give the right size at this point. It could be the wrong size because your image is loaded to late.

    I don't understand atm which size hint you reimplement because there is no widget (if you draw the image on your own in paintEvent of QAbstractScrollArea).
    It's nice to be important but it's more important to be nice.

  6. #6
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: drawContents in QScrollArea (Qt4)

    Sorry, I don't draw the image in paintEvent of QAbstractScrollArea, I draw it in the paintEvent of the widget.

  7. #7
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: drawContents in QScrollArea (Qt4)

    So you will likely run into the problem I noticed. Together with your second thread with the rubberband, I strongly suggest to switch over to the other solution: Use QAbstractScollArea and do all the painting and scrolling yourself although it might be not as easy as your attempt with the two widgets.
    It's nice to be important but it's more important to be nice.

  8. #8
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: drawContents in QScrollArea (Qt4)

    Hi again, I have found that if I enable the option setWidgetResizable of the ScrollArea, the pixmap is always shown because the scrollarea automatically resizes the widget that contains the scrollarea to the viewport's size. This is very useful if the pixamp is smaller than the viewport because in this case I draw the pixmap centered in the widget. And, even if the window chages it's size, the pixamp is always centered. The problem is that if the pixmap is larger than the viewport and setWidgetResizable is activated, the scroll bars are not shown. The solution is to disable setWidgetResizable and resize the widget to the size of the pixmap. I would like to know where I would have to put the instructions to: when the size of the viewport is smaller than the pixmap, disable setWidgetResizable and resize the widget to the size of the pixmap, otherwise enable the option setWidgetResizable. A possible soultion that I have tried is, in the paintEvent of the widget, obtain the pointer to the scrollarea (it's parent) and with this pointer consult the size of the viewport, but the porgram crashes. I think that's because in the first time that it acess to the scroll area isn't still initialized but it's just a suposition. I think that this method, if I could find the way to make it running, could be best way. Could anyone help me please or just tell me if this is posible? Thanks.
    Last edited by SkripT; 13th January 2006 at 18:39.

  9. #9
    Join Date
    Jan 2006
    Location
    Catalonia
    Posts
    266
    Thanks
    44
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: drawContents in QScrollArea (Qt4)

    Hi all, finally I have solved the problem puting the code in the paintEvent of the ScrollArea. If anybody is interested in the code take look at the thread: "QRubberband painting in the scroll area widget" by myself.
    Last edited by SkripT; 14th January 2006 at 10:58.

Similar Threads

  1. Replies: 1
    Last Post: 13th September 2008, 11:00
  2. Dynamic updates of a QWidget in a QScrollArea
    By plamkata in forum Qt Programming
    Replies: 2
    Last Post: 20th July 2008, 23:45
  3. QScrollArea
    By sabeesh in forum Qt Programming
    Replies: 7
    Last Post: 2nd November 2007, 10:33
  4. QScrollArea and resizing
    By rarefluid in forum Qt Programming
    Replies: 8
    Last Post: 22nd July 2007, 14:18
  5. Replies: 2
    Last Post: 8th October 2006, 20:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.