Skip to content

Commit d959384

Browse files
committed
Allow hiding clear button in QgsFilterLineEdit
1 parent d71453d commit d959384

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

python/gui/qgsfilterlineedit.sip

+18
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,30 @@ class QgsFilterLineEdit : QLineEdit
2727
*/
2828
QgsFilterLineEdit( QWidget* parent /TransferThis/ = 0, const QString& nullValue = QString::null );
2929

30+
/** Returns true if the widget's clear button is visible.
31+
* @see setShowClearButton()
32+
* @note added in QGIS 3.0
33+
*/
34+
bool showClearButton() const;
35+
36+
/** Sets whether the widget's clear button is visible.
37+
* @param visible set to false to hide the clear button
38+
* @see showClearButton()
39+
* @note added in QGIS 3.0
40+
*/
41+
void setShowClearButton( bool visible );
42+
3043
/** Returns the clear mode for the widget. The clear mode defines the behaviour of the
3144
* widget when its value is cleared. This defaults to ClearToNull.
3245
* @see setClearMode()
46+
* @note added in QGIS 3.0
3347
*/
3448
ClearMode clearMode() const;
3549

3650
/** Sets the clear mode for the widget. The clear mode defines the behaviour of the
3751
* widget when its value is cleared. This defaults to ClearToNull.
3852
* @see clearMode()
53+
* @note added in QGIS 3.0
3954
*/
4055
void setClearMode( ClearMode mode );
4156

@@ -59,6 +74,7 @@ class QgsFilterLineEdit : QLineEdit
5974
* @param defaultValue default value
6075
* @see defaultValue()
6176
* @see clearMode()
77+
* @note added in QGIS 3.0
6278
*/
6379
void setDefaultValue( const QString& defaultValue );
6480

@@ -67,6 +83,7 @@ class QgsFilterLineEdit : QLineEdit
6783
* is equal to ClearToDefault.
6884
* @see setDefaultValue()
6985
* @see clearMode()
86+
* @note added in QGIS 3.0
7087
*/
7188
QString defaultValue() const;
7289

@@ -101,6 +118,7 @@ class QgsFilterLineEdit : QLineEdit
101118

102119
/** Clears the widget and resets it to the null value.
103120
* @see nullValue()
121+
* @note added in QGIS 3.0
104122
*/
105123
virtual void clearValue();
106124

src/gui/qgsfilterlineedit.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
QgsFilterLineEdit::QgsFilterLineEdit( QWidget* parent, const QString& nullValue )
2727
: QLineEdit( parent )
28+
, mClearButtonVisible( true )
2829
, mClearMode( ClearToNull )
2930
, mNullValue( nullValue )
3031
, mFocusInEvent( false )
@@ -43,6 +44,15 @@ QgsFilterLineEdit::QgsFilterLineEdit( QWidget* parent, const QString& nullValue
4344
SLOT( onTextChanged( const QString& ) ) );
4445
}
4546

47+
void QgsFilterLineEdit::setShowClearButton( bool visible )
48+
{
49+
mClearButtonVisible = visible;
50+
if ( !visible )
51+
mClearHover = false;
52+
53+
update();
54+
}
55+
4656
void QgsFilterLineEdit::mousePressEvent( QMouseEvent* e )
4757
{
4858
if ( !mFocusInEvent )
@@ -145,7 +155,7 @@ void QgsFilterLineEdit::onTextChanged( const QString &text )
145155

146156
bool QgsFilterLineEdit::shouldShowClear() const
147157
{
148-
if ( !isEnabled() || isReadOnly() )
158+
if ( !isEnabled() || isReadOnly() || !mClearButtonVisible )
149159
return false;
150160

151161
switch ( mClearMode )

src/gui/qgsfilterlineedit.h

+21
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
3939
Q_PROPERTY( QString nullValue READ nullValue WRITE setNullValue )
4040
Q_PROPERTY( QString defaultValue READ defaultValue WRITE setDefaultValue )
4141
Q_PROPERTY( QString value READ value WRITE setValue NOTIFY valueChanged )
42+
Q_PROPERTY( bool showClearButton READ showClearButton WRITE setShowClearButton )
4243

4344
public:
4445

@@ -55,15 +56,30 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
5556
*/
5657
QgsFilterLineEdit( QWidget* parent = nullptr, const QString& nullValue = QString::null );
5758

59+
/** Returns true if the widget's clear button is visible.
60+
* @see setShowClearButton()
61+
* @note added in QGIS 3.0
62+
*/
63+
bool showClearButton() const { return mClearButtonVisible; }
64+
65+
/** Sets whether the widget's clear button is visible.
66+
* @param visible set to false to hide the clear button
67+
* @see showClearButton()
68+
* @note added in QGIS 3.0
69+
*/
70+
void setShowClearButton( bool visible );
71+
5872
/** Returns the clear mode for the widget. The clear mode defines the behaviour of the
5973
* widget when its value is cleared. This defaults to ClearToNull.
6074
* @see setClearMode()
75+
* @note added in QGIS 3.0
6176
*/
6277
ClearMode clearMode() const { return mClearMode; }
6378

6479
/** Sets the clear mode for the widget. The clear mode defines the behaviour of the
6580
* widget when its value is cleared. This defaults to ClearToNull.
6681
* @see clearMode()
82+
* @note added in QGIS 3.0
6783
*/
6884
void setClearMode( ClearMode mode ) { mClearMode = mode; }
6985

@@ -87,6 +103,7 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
87103
* @param defaultValue default value
88104
* @see defaultValue()
89105
* @see clearMode()
106+
* @note added in QGIS 3.0
90107
*/
91108
void setDefaultValue( const QString& defaultValue ) { mDefaultValue = defaultValue; }
92109

@@ -95,6 +112,7 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
95112
* is equal to ClearToDefault.
96113
* @see setDefaultValue()
97114
* @see clearMode()
115+
* @note added in QGIS 3.0
98116
*/
99117
QString defaultValue() const { return mDefaultValue; }
100118

@@ -129,6 +147,7 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
129147

130148
/** Clears the widget and resets it to the null value.
131149
* @see nullValue()
150+
* @note added in QGIS 3.0
132151
*/
133152
virtual void clearValue();
134153

@@ -158,6 +177,8 @@ class GUI_EXPORT QgsFilterLineEdit : public QLineEdit
158177

159178
private:
160179

180+
bool mClearButtonVisible;
181+
161182
ClearMode mClearMode;
162183

163184
QString mNullValue;

tests/src/python/test_qgsfilterlineedit.py

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ def testGettersSetters(self):
4242
self.assertEqual(w.defaultValue(), 'default')
4343
w.setClearMode(QgsFilterLineEdit.ClearToDefault)
4444
self.assertEqual(w.clearMode(), QgsFilterLineEdit.ClearToDefault)
45+
w.setShowClearButton(False)
46+
self.assertFalse(w.showClearButton())
47+
w.setShowClearButton(True)
48+
self.assertTrue(w.showClearButton())
4549

4650
def testNullValueHandling(self):
4751
""" test widget handling of null values """

0 commit comments

Comments
 (0)