Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Scroll a custom drawn widget



  • Hello,
    I have a QWidget on which I do custom drawing. I want to put it inside a scroll area but am not able to do so. The code is :
    displayArea = std::unique_ptr<MapDisplayArea>(new MapDisplayArea(this, mapReader.get()));

    QScrollArea* scroll = new QScrollArea();
    scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    scroll->setWidgetResizable(true);
    scroll->setWidget(displayArea.get());
    
    QBoxLayout* boxLayout = new QBoxLayout(QBoxLayout::TopToBottom);
    scroll->setLayout(boxLayout);
    this->setCentralWidget(scroll);
    

    DisplayArea is the widget where I draw. The window appears but doesn't scroll.
    Thanks,
    Regards



  • is displayArea->size() greater than the viewport size?



  • Hello,

    How do I get the viewport size ?

    Regards



  • How do I get the viewport size ?

    scroll->viewport()->size()



  • Does displayArea has valid size hint or minimum size that larger than the viewport size?
    (The viewport size is basically the scroll area size but without the scroll bars and frame.)

    widgetResizable : bool
    This property holds whether the scroll area should resize the view widget
    If this property is set to false (the default), the scroll area honors the size of its widget. Regardless of this property, you can programmatically resize the widget using widget()->resize(), and the scroll area will automatically adjust itself to the new size.
    If this property is set to true, the scroll area will automatically resize the widget in order to avoid scroll bars where they can be avoided, or to take advantage of extra space.

    Or you can set the property to false and set the size of displayArea manually (must be large enough).


  • Lifetime Qt Champion

    Hi,

    @Daniel-Santos said in Scroll a custom drawn widget:

    QBoxLayout* boxLayout = new QBoxLayout(QBoxLayout::TopToBottom);
    scroll->setLayout(boxLayout);

    Why are you setting a layout on your QScrollArea ? If you need one, it should be on the widget contained in your scroll area.



  • @VRonin The view port is the size of the window, so I guess that's why it doesn't scroll. Now question is : how do I set the view port to the new size ?

    Thanks



  • @SGaist You are right that didn't make any sense, I removed it. Anyway I got it working by implementing the method minimumSizeHint in the custom drawn widget.
    Thanks for your help


  • Lifetime Qt Champion

    Great !

    Then please mark the thread as solved so other forum users may know a solution has been found :-)


Log in to reply