Allow QgsScrollArea to maintain horizontal width of area for

child widgets, and apply scroll area to vertical contents only
This commit is contained in:
Nyall Dawson 2019-05-02 09:37:02 +10:00
parent 5a57336880
commit ffe02c37b6
3 changed files with 42 additions and 0 deletions

View File

@ -45,11 +45,23 @@ QScrollArea itself or its child viewport().
%Docstring
Returns ``True`` if a scroll recently occurred within
the QScrollArea or its child viewport()
%End
void setVerticalOnly( bool verticalOnly );
%Docstring
Sets whether the scroll area only applies vertical.
If set to ``True``, then scroll area children will resize horizontally to match the width of
the scroll area widget.
.. versionadded:: 3.8
%End
protected:
virtual void wheelEvent( QWheelEvent *event );
virtual void resizeEvent( QResizeEvent *event );
};

View File

@ -34,6 +34,13 @@ void QgsScrollArea::wheelEvent( QWheelEvent *e )
QScrollArea::wheelEvent( e );
}
void QgsScrollArea::resizeEvent( QResizeEvent *event )
{
if ( mVerticalOnly && widget() )
widget()->setFixedWidth( event->size().width() );
QScrollArea::resizeEvent( event );
}
void QgsScrollArea::scrollOccurred()
{
mTimer.setSingleShot( true );
@ -45,6 +52,16 @@ bool QgsScrollArea::hasScrolled() const
return mTimer.isActive();
}
void QgsScrollArea::setVerticalOnly( bool verticalOnly )
{
mVerticalOnly = verticalOnly;
if ( mVerticalOnly )
setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
if ( mVerticalOnly && widget() )
widget()->setFixedWidth( size().width() );
}
///@cond PRIVATE
ScrollAreaFilter::ScrollAreaFilter( QgsScrollArea *parent, QWidget *viewPort )

View File

@ -61,12 +61,25 @@ class GUI_EXPORT QgsScrollArea : public QScrollArea
*/
bool hasScrolled() const;
/**
* Sets whether the scroll area only applies vertical.
*
* If set to TRUE, then scroll area children will resize horizontally to match the width of
* the scroll area widget.
*
* \since QGIS 3.8
*/
void setVerticalOnly( bool verticalOnly );
protected:
void wheelEvent( QWheelEvent *event ) override;
void resizeEvent( QResizeEvent *event ) override;
private:
QTimer mTimer;
ScrollAreaFilter *mFilter = nullptr;
bool mVerticalOnly = false;
};
#ifndef SIP_RUN