Skip to content

Commit ffe02c3

Browse files
committed
Allow QgsScrollArea to maintain horizontal width of area for
child widgets, and apply scroll area to vertical contents only
1 parent 5a57336 commit ffe02c3

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

python/gui/auto_generated/qgsscrollarea.sip.in

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,23 @@ QScrollArea itself or its child viewport().
4545
%Docstring
4646
Returns ``True`` if a scroll recently occurred within
4747
the QScrollArea or its child viewport()
48+
%End
49+
50+
void setVerticalOnly( bool verticalOnly );
51+
%Docstring
52+
Sets whether the scroll area only applies vertical.
53+
54+
If set to ``True``, then scroll area children will resize horizontally to match the width of
55+
the scroll area widget.
56+
57+
.. versionadded:: 3.8
4858
%End
4959

5060
protected:
5161
virtual void wheelEvent( QWheelEvent *event );
5262

63+
virtual void resizeEvent( QResizeEvent *event );
64+
5365

5466
};
5567

src/gui/qgsscrollarea.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ void QgsScrollArea::wheelEvent( QWheelEvent *e )
3434
QScrollArea::wheelEvent( e );
3535
}
3636

37+
void QgsScrollArea::resizeEvent( QResizeEvent *event )
38+
{
39+
if ( mVerticalOnly && widget() )
40+
widget()->setFixedWidth( event->size().width() );
41+
QScrollArea::resizeEvent( event );
42+
}
43+
3744
void QgsScrollArea::scrollOccurred()
3845
{
3946
mTimer.setSingleShot( true );
@@ -45,6 +52,16 @@ bool QgsScrollArea::hasScrolled() const
4552
return mTimer.isActive();
4653
}
4754

55+
void QgsScrollArea::setVerticalOnly( bool verticalOnly )
56+
{
57+
mVerticalOnly = verticalOnly;
58+
if ( mVerticalOnly )
59+
setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
60+
61+
if ( mVerticalOnly && widget() )
62+
widget()->setFixedWidth( size().width() );
63+
}
64+
4865
///@cond PRIVATE
4966

5067
ScrollAreaFilter::ScrollAreaFilter( QgsScrollArea *parent, QWidget *viewPort )

src/gui/qgsscrollarea.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,25 @@ class GUI_EXPORT QgsScrollArea : public QScrollArea
6161
*/
6262
bool hasScrolled() const;
6363

64+
/**
65+
* Sets whether the scroll area only applies vertical.
66+
*
67+
* If set to TRUE, then scroll area children will resize horizontally to match the width of
68+
* the scroll area widget.
69+
*
70+
* \since QGIS 3.8
71+
*/
72+
void setVerticalOnly( bool verticalOnly );
73+
6474
protected:
6575
void wheelEvent( QWheelEvent *event ) override;
76+
void resizeEvent( QResizeEvent *event ) override;
6677

6778
private:
6879
QTimer mTimer;
6980
ScrollAreaFilter *mFilter = nullptr;
81+
bool mVerticalOnly = false;
82+
7083
};
7184

7285
#ifndef SIP_RUN

0 commit comments

Comments
 (0)