Solved No scrollbars when put QWidget derived class object in QScrollArea
-
class
Canvas
is derived fromQWidget
, and paint inCanvas::paintEvent
.
The problem is after callingCanvas::resize
to make canvas larger thanQScrollArea
, the scrollbars don't appear. -
@jronald said in No scrollbars when put QWidget derived class object in QScrollArea:
I've tried it, resize still doesn't work, and setFixedSize works.
Here is what the manual says:
If a standard QWidget is used for the child widget, it may be necessary to call QWidget::setMinimumSize() to ensure that the contents of the widget are shown correctly within the scroll area.
This means the proper way is to use
setMinimumSize()
instead ofsetFixedSize()
. However, one of the two is necessary if you use QWidget directly or derive from QWidget without overriding the default behavior. -
@jronald
because the widget doesnt aquire more space.
resize the widget to the painted sizeor did you place your canvas widget inside another container widget?!
-
@raven-worx said in No scrollbars when put QWidget derived class object in QScrollArea:
resize the widget to the painted size
In the handler of "Load" button, the function below is called, the the image is obviously larger than
scrollArea
.void Canvas::Init() { if (__pixmap.load("/path/to/test.jpeg")) { resize(__pixmap.size()); } }
@raven-worx said in No scrollbars when put QWidget derived class object in QScrollArea:
did you place your canvas widget inside another container widget?!
-
Solved by implement
Canvas::Init
as belowvoid Canvas::Init() { if (__pixmap.load("/path/to/test.jpeg")) { setFixedSize(__pixmap.size()); // calling resize here doesn't work } }
Why don't calling
resize
instead ofsetFixedSize
work? -
@jronald said in No scrollbars when put QWidget derived class object in QScrollArea:
Why don't calling resize instead of setFixedSize work?
how do you add the canvas to your scrollarea?
-
@raven-worx said in No scrollbars when put QWidget derived class object in QScrollArea:
how do you add the canvas to your scrollarea?
Drag and drop in designer.
-
From the screenshot you provided your
canvas
is inside aQWidget
with a layout. Layouts will adapt the size of their children. Callingresize()
on a children does not help when it is inside a layout. That is whysetFixedSize()
works as it communicates the intended size to the surrounding layout. You should get rid ofscrollAreaWidgetContents
and placecanvas
directly into thescrollArea
. -
@SimonSchroeder said in No scrollbars when put QWidget derived class object in QScrollArea:
From the screenshot you provided your
canvas
is inside aQWidget
with a layout. Layouts will adapt the size of their children. Callingresize()
on a children does not help when it is inside a layout. That is whysetFixedSize()
works as it communicates the intended size to the surrounding layout. You should get rid ofscrollAreaWidgetContents
and placecanvas
directly into thescrollArea
.I've tried it,
resize
still doesn't work, andsetFixedSize
works.
-
@jronald said in No scrollbars when put QWidget derived class object in QScrollArea:
I've tried it, resize still doesn't work, and setFixedSize works.
Here is what the manual says:
If a standard QWidget is used for the child widget, it may be necessary to call QWidget::setMinimumSize() to ensure that the contents of the widget are shown correctly within the scroll area.
This means the proper way is to use
setMinimumSize()
instead ofsetFixedSize()
. However, one of the two is necessary if you use QWidget directly or derive from QWidget without overriding the default behavior.