Mantid
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
MantidQt::API::WidgetScrollbarDecorator Class Reference

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...
 
WidgetScrollbarDecoratoroperator= (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...
 

Detailed Description

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:

  1. Create an instance of this class (usually as a class member)
private:
WidgetScrollbarDecorator : Adds scrollbar functionality to a QWidget.
  1. Initialise with the widget that needs scrollbars (usually this )
ExampleDialog::ExampleDialog() : m_scrollbars(this) {}
  1. Enable scrollbars (only after the UI has been set up!)
ui->setupUi(this); // MUST happen first
m_scrollbars.setEnabled(true);
void setEnabled(bool enable)
Enable or disable scrollable behaviour.

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.

Constructor & Destructor Documentation

◆ WidgetScrollbarDecorator() [1/2]

WidgetScrollbarDecorator::WidgetScrollbarDecorator ( QWidget *  target)
explicit

Constructor.

Parameters
targetThe 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.

◆ ~WidgetScrollbarDecorator()

WidgetScrollbarDecorator::~WidgetScrollbarDecorator ( )
virtual

Destructor.

Definition at line 50 of file WidgetScrollbarDecorator.cpp.

References m_offscreen.

◆ WidgetScrollbarDecorator() [2/2]

MantidQt::API::WidgetScrollbarDecorator::WidgetScrollbarDecorator ( const WidgetScrollbarDecorator )
delete

Member Function Documentation

◆ enabled()

bool WidgetScrollbarDecorator::enabled ( ) const

Check whether the target is currently scrollable.

Returns
true if the target is currently scrollable, false otherwise.

Definition at line 61 of file WidgetScrollbarDecorator.cpp.

References m_enabled.

Referenced by setEnabled().

◆ operator=()

WidgetScrollbarDecorator & MantidQt::API::WidgetScrollbarDecorator::operator= ( WidgetScrollbarDecorator  )
delete

◆ setEnabled()

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.

Parameters
enableWhether 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().

◆ setThresholdHeight()

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.

Parameters
heightMinimum height target may shrink to before scrollbars appear

Definition at line 131 of file WidgetScrollbarDecorator.cpp.

References height, and m_viewport.

◆ setThresholdSize()

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.

Parameters
widthMinimum width target may shrink to before scrollbars appear
heightMinimum height target may shrink to before scrollbars appear

Definition at line 147 of file WidgetScrollbarDecorator.cpp.

References height, and m_viewport.

◆ setThresholdWidth()

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.

Parameters
widthMinimum width target may shrink to before scrollbars appear

Definition at line 116 of file WidgetScrollbarDecorator.cpp.

References m_viewport.

Member Data Documentation

◆ m_enabled

bool MantidQt::API::WidgetScrollbarDecorator::m_enabled
private

Whether the target is currently scrollable.

Definition at line 107 of file WidgetScrollbarDecorator.h.

Referenced by enabled(), and setEnabled().

◆ m_layout

QVBoxLayout* MantidQt::API::WidgetScrollbarDecorator::m_layout
private

Main layout used when scrollable.

Definition at line 101 of file WidgetScrollbarDecorator.h.

Referenced by setEnabled(), and WidgetScrollbarDecorator().

◆ m_offscreen

QWidget* MantidQt::API::WidgetScrollbarDecorator::m_offscreen
private

Used to hold above widgets when disabled.

Definition at line 104 of file WidgetScrollbarDecorator.h.

Referenced by setEnabled(), WidgetScrollbarDecorator(), and ~WidgetScrollbarDecorator().

◆ m_scrollarea

QScrollArea* MantidQt::API::WidgetScrollbarDecorator::m_scrollarea
private

Used to provide scrolling functionality.

Definition at line 102 of file WidgetScrollbarDecorator.h.

Referenced by WidgetScrollbarDecorator().

◆ m_target

QWidget* MantidQt::API::WidgetScrollbarDecorator::m_target
private

The target widget that is given scrollbars.

Definition at line 106 of file WidgetScrollbarDecorator.h.

Referenced by setEnabled(), and WidgetScrollbarDecorator().

◆ m_viewport

QWidget* MantidQt::API::WidgetScrollbarDecorator::m_viewport
private

Single widget inside QScrollArea.

Definition at line 103 of file WidgetScrollbarDecorator.h.

Referenced by setEnabled(), setThresholdHeight(), setThresholdSize(), setThresholdWidth(), and WidgetScrollbarDecorator().


The documentation for this class was generated from the following files: