KTreeWidgetSearchLine Class

This class makes it easy to add a search line for filtering the items in listviews based on a simple text search. More...

Header: #include <KTreeWidgetSearchLine>
CMake: find_package(KF6 REQUIRED COMPONENTS ItemViews)
target_link_libraries(mytarget PRIVATE KF6::ItemViews)
Inherits: QLineEdit

Properties

Public Functions

KTreeWidgetSearchLine(QWidget *parent = nullptr, QTreeWidget *treeWidget = nullptr)
KTreeWidgetSearchLine(QWidget *parent, const QList<QTreeWidget *> &treeWidgets)
Qt::CaseSensitivity caseSensitivity() const
bool keepParentsVisible() const
QList<int> searchColumns() const
QTreeWidget *treeWidget() const
QList<QTreeWidget *> treeWidgets() const

Public Slots

void addTreeWidget(QTreeWidget *treeWidget)
void removeTreeWidget(QTreeWidget *treeWidget)
void setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)
void setKeepParentsVisible(bool value)
void setSearchColumns(const QList<int> &columns)
void setTreeWidget(QTreeWidget *treeWidget)
void setTreeWidgets(const QList<QTreeWidget *> &treeWidgets)
virtual void updateSearch(const QString &pattern = QString())

Signals

void caseSensitivityChanged(Qt::CaseSensitivity caseSensitivity)
void hiddenChanged(QTreeWidgetItem *, bool)
void keepParentsVisibleChanged(bool keepParentsVisible)
(since 5.0) void searchUpdated(const QString &searchString)

Protected Functions

virtual bool canChooseColumnsCheck()
virtual void connectTreeWidget(QTreeWidget *)
virtual void disconnectTreeWidget(QTreeWidget *)
virtual bool itemMatches(const QTreeWidgetItem *item, const QString &pattern) const
virtual void updateSearch(QTreeWidget *treeWidget)

Detailed Description

No changes to the application other than instantiating this class with appropriate QTreeWidgets should be needed.

Property Documentation

caseSensitity : Qt::CaseSensitivity

Access functions:

Qt::CaseSensitivity caseSensitivity() const
void setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)

Notifier signal:

void caseSensitivityChanged(Qt::CaseSensitivity caseSensitivity)

keepParentsVisible : bool

Access functions:

bool keepParentsVisible() const
void setKeepParentsVisible(bool value)

Notifier signal:

void keepParentsVisibleChanged(bool keepParentsVisible)

Member Function Documentation

[explicit] KTreeWidgetSearchLine::KTreeWidgetSearchLine(QWidget *parent = nullptr, QTreeWidget *treeWidget = nullptr)

Constructs a KTreeWidgetSearchLine with treeWidget being the QTreeWidget to be filtered.

If treeWidget is null then the widget will be disabled until listviews are set with setTreeWidget(), setTreeWidgets() or added with addTreeWidget().

KTreeWidgetSearchLine::KTreeWidgetSearchLine(QWidget *parent, const QList<QTreeWidget *> &treeWidgets)

Constructs a KTreeWidgetSearchLine with treeWidgets being the list of pointers to QTreeWidgets to be filtered.

If treeWidgets is empty then the widget will be disabled until listviews are set with setTreeWidget(), setTreeWidgets() or added with addTreeWidget().

[slot] void KTreeWidgetSearchLine::addTreeWidget(QTreeWidget *treeWidget)

Adds a QTreeWidget to the list of listviews filtered by this search line. If treeWidget is null then the widget will be disabled.

See also treeWidget(), setTreeWidgets(), and removeTreeWidget().

[virtual protected] bool KTreeWidgetSearchLine::canChooseColumnsCheck()

Checks columns in all listviews and decides whether choosing columns to filter on makes any sense.

Returns false if either of the following is true: * there are no listviews connected, * the listviews have different numbers of columns, * the listviews have only one column, * the listviews differ in column labels.

Otherwise it returns true.

See also setSearchColumns().

Qt::CaseSensitivity KTreeWidgetSearchLine::caseSensitivity() const

Returns true if the search is case sensitive. This defaults to false.

Note: Getter function for property caseSensitity.

See also setCaseSensitivity() and setCaseSensitive().

[signal] void KTreeWidgetSearchLine::caseSensitivityChanged(Qt::CaseSensitivity caseSensitivity)

Note: Notifier signal for property caseSensitity.

[virtual protected] void KTreeWidgetSearchLine::connectTreeWidget(QTreeWidget *)

Connects signals of this listview to the appropriate slots of the search line.

[virtual protected] void KTreeWidgetSearchLine::disconnectTreeWidget(QTreeWidget *)

Disconnects signals of a listviews from the search line.

[signal] void KTreeWidgetSearchLine::hiddenChanged(QTreeWidgetItem *, bool)

This signal is emitted whenever an item gets hidden or unhidden due to it not matching or matching the search string.

[virtual protected] bool KTreeWidgetSearchLine::itemMatches(const QTreeWidgetItem *item, const QString &pattern) const

Returns true if item matches the search pattern. This will be evaluated based on the value of caseSensitive(). This can be overridden in subclasses to implement more complicated matching schemes.

bool KTreeWidgetSearchLine::keepParentsVisible() const

If this is true (the default) then the parents of matched items will also be shown.

Note: Getter function for property keepParentsVisible.

See also setKeepParentsVisible().

[signal] void KTreeWidgetSearchLine::keepParentsVisibleChanged(bool keepParentsVisible)

Note: Notifier signal for property keepParentsVisible.

[slot] void KTreeWidgetSearchLine::removeTreeWidget(QTreeWidget *treeWidget)

Removes a QTreeWidget from the list of listviews filtered by this search line. Does nothing if treeWidget is 0 or is not filtered by the quick search line.

See also listVew(), setTreeWidgets(), and addTreeWidget().

QList<int> KTreeWidgetSearchLine::searchColumns() const

Returns the current list of columns that will be searched. If the returned list is empty all visible columns will be searched.

See also setSearchColumns.

[signal, since 5.0] void KTreeWidgetSearchLine::searchUpdated(const QString &searchString)

This signal is emitted when user finished entering filter text or when he made a pause long enough, after the QTreeWidget items got filtered

searchString is the text currently entered by the user

This function was introduced in 5.0.

[slot] void KTreeWidgetSearchLine::setCaseSensitivity(Qt::CaseSensitivity caseSensitivity)

Make the search case sensitive or case insensitive.

Note: Setter function for property caseSensitity.

See also caseSensitivity() and caseSenstivity().

[slot] void KTreeWidgetSearchLine::setKeepParentsVisible(bool value)

When a search is active on a list that's organized into a tree view if a parent or ancesestor of an item is does not match the search then it will be hidden and as such so too will any children that match.

If this is set to true (the default) then the parents of matching items will be shown.

Warning: setKeepParentsVisible(true) does not have the expected effect on items being added to or removed from the view while a search is active. When a new search starts afterwards the behavior will be normal.

Note: Setter function for property keepParentsVisible.

See also keepParentsVisible.

[slot] void KTreeWidgetSearchLine::setSearchColumns(const QList<int> &columns)

Sets the list of columns to be searched. The default is to search all, visible columns which can be restored by passing columns as an empty list. If listviews to be filtered have different numbers or labels of columns this method has no effect.

See also searchColumns.

[slot] void KTreeWidgetSearchLine::setTreeWidget(QTreeWidget *treeWidget)

Sets the QTreeWidget that is filtered by this search line, replacing any previously filtered listviews. If treeWidget is null then the widget will be disabled.

See also treeWidget() and setTreeWidgets().

[slot] void KTreeWidgetSearchLine::setTreeWidgets(const QList<QTreeWidget *> &treeWidgets)

Sets QTreeWidgets that are filtered by this search line, replacing any previously filtered listviews. If treeWidgets is empty then the widget will be disabled.

See also treeWidgets(), addTreeWidget(), and setTreeWidget().

QTreeWidget *KTreeWidgetSearchLine::treeWidget() const

Returns the listview that is currently filtered by the search. If there are multiple listviews filtered, it returns 0.

See also setTreeWidget() and treeWidgets().

QList<QTreeWidget *> KTreeWidgetSearchLine::treeWidgets() const

Returns the list of pointers to listviews that are currently filtered by the search.

See also setTreeWidgets(), addTreeWidget(), and treeWidget().

[virtual protected] void KTreeWidgetSearchLine::updateSearch(QTreeWidget *treeWidget)

Updates search to only make visible appropriate items in treeWidget.

If treeWidget is null then nothing is done.

[virtual slot] void KTreeWidgetSearchLine::updateSearch(const QString &pattern = QString())

Updates search to only make visible the items that match pattern. If s is null then the line edit's text will be used.