Mantid
|
WidgetScrollbarDecorator : Adds scrollbar functionality to a QWidget. More...
#include <WidgetScrollbarDecorator.h>
Public Member Functions | |
bool | enabled () const |
Check whether the target is currently scrollable. More... | |
WidgetScrollbarDecorator & | operator= (WidgetScrollbarDecorator)=delete |
void | setEnabled (bool enable) |
Enable or disable scrollable behaviour. More... | |
void | setThresholdHeight (int height) |
Set the height, in pixels, at which scrollbars should appear. More... | |
void | setThresholdSize (int width, int height) |
Set the size, in pixels, at which scrollbars should appear. More... | |
void | setThresholdWidth (int width) |
Set the width, in pixels, at which scrollbars should appear. More... | |
WidgetScrollbarDecorator (const WidgetScrollbarDecorator &)=delete | |
WidgetScrollbarDecorator (QWidget *target) | |
Constructor. More... | |
virtual | ~WidgetScrollbarDecorator () |
Destructor. More... | |
Private Attributes | |
bool | m_enabled |
Whether the target is currently scrollable. More... | |
QVBoxLayout * | m_layout |
Main layout used when scrollable. More... | |
QWidget * | m_offscreen |
Used to hold above widgets when disabled. More... | |
QScrollArea * | m_scrollarea |
Used to provide scrolling functionality. More... | |
QWidget * | m_target |
The target widget that is given scrollbars. More... | |
QWidget * | m_viewport |
Single widget inside QScrollArea. More... | |
WidgetScrollbarDecorator : Adds scrollbar functionality to a QWidget.
Mainly intended to make QDialogs
and QMainWindows
scrollable since this feature is missing in Qt. This allows particularly large dialogs to be used on laptops and other devices with small screens.
Both horizontal and vertical scrollbars are provided. Scrollbars are only visible when needed (when the dialog/widget is too small to display all widgets at their minimum size). Qt automatically attempts to resize dialogs to fit on the screen that they show up on.
Usage:
this
)Details:
The reason scrollbars should only be enabled after the UI is set up is that this decorator works by pulling the main layout of the target widget out (along with all widgets inside of it), adding its own layout and scrollarea widgets to the target, and finally placing the original layout inside of the scrollarea.
For Designer-created dialogs, this means that setupUi(this) must be called before scrollbars are enabled. Widgets can still be accessed as usual via the ui
object. This includes adding, removing, moving, etc widgets, as long as it is done using ui->mainLayout
rather than this->layout()
.
For manually created dialogs, this means that you have to at least create a main layout for your dialog before enabling the scrollbars. Note that after enabling scrollbars, this->layout()
will no longer be the layout that you created. So, if you want to access your layout or add more widgets later, you should keep a pointer to it and work with that.
Definition at line 77 of file WidgetScrollbarDecorator.h.
|
explicit |
Constructor.
target | The target widget to be extended with scrollbar functionality. |
Definition at line 22 of file WidgetScrollbarDecorator.cpp.
References m_layout, m_offscreen, m_scrollarea, m_target, and m_viewport.
|
virtual |
|
delete |
bool WidgetScrollbarDecorator::enabled | ( | ) | const |
Check whether the target is currently scrollable.
Definition at line 61 of file WidgetScrollbarDecorator.cpp.
References m_enabled.
Referenced by setEnabled().
|
delete |
void WidgetScrollbarDecorator::setEnabled | ( | bool | enable | ) |
Enable or disable scrollable behaviour.
Enable or disable scrollable behaviour on the target.
This works by shuffling layouts using a sparsely documented feature of QWidget::setLayout(). Normally, you cannot remove a layout once it is set without deleting it (and all contained widgets along with it). You also cannot call setLayout() on a widget that already has a layout.
However, if the layout you pass to setLayout() is already set on a different widget, that layout and all contained widgets are reparented, effectively removing it from the widget it was on. But, for this to work, you need a layout-less widget to call setLayout() on.
Since this class works with three widgets (the target widget, the viewport inside of the scrollable area, and an offscreen dummy widget) and only two layouts (the layout of the target widget and the layout that contains the scrollarea), there is always one widget that has no layout.
When scrolling is enabled, m_offscreen is empty. When scrolling is disabled, m_viewport is empty.
enable | Whether the target should be scrollable or not. |
Definition at line 87 of file WidgetScrollbarDecorator.cpp.
References enabled(), m_enabled, m_layout, m_offscreen, m_target, and m_viewport.
Referenced by MantidQt::CustomDialogs::StartLiveDataDialog::initLayout().
void WidgetScrollbarDecorator::setThresholdHeight | ( | int | height | ) |
Set the height, in pixels, at which scrollbars should appear.
This overrides the default behaviour of preferring to shrink widgets until they reach their minimum size before enabling scrollbars. Note that scrollbars will be enabled before reaching this size setting if the minimum size of all widgets is reached first.
Set to 0 to reset to default behaviour.
height | Minimum height target may shrink to before scrollbars appear |
Definition at line 131 of file WidgetScrollbarDecorator.cpp.
References height, and m_viewport.
void WidgetScrollbarDecorator::setThresholdSize | ( | int | width, |
int | height | ||
) |
Set the size, in pixels, at which scrollbars should appear.
This overrides the default behaviour of preferring to shrink widgets until they reach their minimum size before enabling scrollbars. Note that scrollbars will be enabled before reaching this size setting if the minimum size of all widgets is reached first.
Set to (0, 0) to reset to default behaviour.
width | Minimum width target may shrink to before scrollbars appear |
height | Minimum height target may shrink to before scrollbars appear |
Definition at line 147 of file WidgetScrollbarDecorator.cpp.
References height, and m_viewport.
void WidgetScrollbarDecorator::setThresholdWidth | ( | int | width | ) |
Set the width, in pixels, at which scrollbars should appear.
This overrides the default behaviour of preferring to shrink widgets until they reach their minimum size before enabling scrollbars. Note that scrollbars will be enabled before reaching this size setting if the minimum size of all widgets is reached first.
Set to 0 to reset to default behaviour.
width | Minimum width target may shrink to before scrollbars appear |
Definition at line 116 of file WidgetScrollbarDecorator.cpp.
References m_viewport.
|
private |
Whether the target is currently scrollable.
Definition at line 107 of file WidgetScrollbarDecorator.h.
Referenced by enabled(), and setEnabled().
|
private |
Main layout used when scrollable.
Definition at line 101 of file WidgetScrollbarDecorator.h.
Referenced by setEnabled(), and WidgetScrollbarDecorator().
|
private |
Used to hold above widgets when disabled.
Definition at line 104 of file WidgetScrollbarDecorator.h.
Referenced by setEnabled(), WidgetScrollbarDecorator(), and ~WidgetScrollbarDecorator().
|
private |
Used to provide scrolling functionality.
Definition at line 102 of file WidgetScrollbarDecorator.h.
Referenced by WidgetScrollbarDecorator().
|
private |
The target widget that is given scrollbars.
Definition at line 106 of file WidgetScrollbarDecorator.h.
Referenced by setEnabled(), and WidgetScrollbarDecorator().
|
private |
Single widget inside QScrollArea.
Definition at line 103 of file WidgetScrollbarDecorator.h.
Referenced by setEnabled(), setThresholdHeight(), setThresholdSize(), setThresholdWidth(), and WidgetScrollbarDecorator().