KHistoryComboBox Class
A combobox for offering a history and completion. More...
Header: | #include <KHistoryComboBox> |
CMake: | find_package(KF6 REQUIRED COMPONENTS Completion) target_link_libraries(mytarget PRIVATE KF6::Completion) |
Inherits: | KComboBox |
Properties
- historyItems : QStringList
Public Functions
KHistoryComboBox(QWidget *parent = nullptr) | |
KHistoryComboBox(bool useCompletion, QWidget *parent = nullptr) | |
QStringList | historyItems() const |
bool | removeFromHistory(const QString &item) |
void | setHistoryItems(const QStringList &items) |
void | setHistoryItems(const QStringList &items, bool setCompletionList) |
(since 5.66) void | setIconProvider(std::function<QIcon (const QString &)> providerFunction) |
Public Slots
void | addToHistory(const QString &item) |
void | clearHistory() |
void | reset() |
Signals
void | cleared() |
Protected Functions
void | insertItems(const QStringList &items) |
bool | useCompletion() const |
Detailed Description
A combobox which implements a history like a unix shell. You can navigate through all the items by using the Up or Down arrows (configurable of course). Additionally, weighted completion is available. So you should load and save the completion list to preserve the weighting between sessions.
KHistoryComboBox obeys the HISTCONTROL environment variable to determine whether duplicates in the history should be tolerated in addToHistory() or not. During construction of KHistoryComboBox, duplicates will be disabled when HISTCONTROL is set to "ignoredups" or "ignoreboth". Otherwise, duplicates are enabled by default.
TODO qdoc
[Missing image html]
Property Documentation
historyItems : QStringList
Access functions:
QStringList | historyItems() const |
void | setHistoryItems(const QStringList &items) |
void | setHistoryItems(const QStringList &items, bool setCompletionList) |
Member Function Documentation
[explicit]
KHistoryComboBox::KHistoryComboBox(QWidget *parent = nullptr)
Constructs a "read-write" combobox. A read-only history combobox doesn't make much sense, so it is only available as read-write. Completion will be used automatically for the items in the combo.
The insertion-policy is set to NoInsert, you have to add the items yourself via the slot addToHistory. If you want every item added, use
connect( combo, SIGNAL( activated( const QString& )), combo, SLOT( addToHistory( const QString& )));
Use QComboBox::setMaxCount() to limit the history.
parent the parent object of this widget.
[explicit]
KHistoryComboBox::KHistoryComboBox(bool useCompletion, QWidget *parent = nullptr)
Same as the previous constructor, but additionally has the option to specify whether you want to let KHistoryComboBox handle completion or not. If set to true
, KHistoryComboBox will sync the completion to the contents of the combobox.
[slot]
void KHistoryComboBox::addToHistory(const QString &item)
Adds an item to the end of the history list and to the completion list. If maxCount() is reached, the first item of the list will be removed.
If the last inserted item is the same as item, it will not be inserted again.
If duplicatesEnabled() is false, any equal existing item will be removed before item is added.
Note: By using this method and not the Q and KComboBox insertItem() methods, you make sure that the combobox stays in sync with the completion. It would be annoying if completion would give an item not in the combobox, and vice versa.
See also removeFromHistory and QComboBox::setDuplicatesEnabled.
[slot]
void KHistoryComboBox::clearHistory()
Clears the history and the completion list.
[signal]
void KHistoryComboBox::cleared()
Emitted when the history was cleared by the entry in the popup menu.
QStringList KHistoryComboBox::historyItems() const
Returns the list of history items. Empty, when this is not a read-write combobox.
Note: Getter function for property historyItems.
See also setHistoryItems.
[protected]
void KHistoryComboBox::insertItems(const QStringList &items)
Inserts items into the combo, honoring setIconProvider() Does not update the completionObject.
Note: duplicatesEnabled() is not honored here.
Called from setHistoryItems()
bool KHistoryComboBox::removeFromHistory(const QString &item)
Removes all items named item.
Returns true
if at least one item was removed.
See also addToHistory.
[slot]
void KHistoryComboBox::reset()
Resets the current position of the up/down history. Call this when you manually call setCurrentItem() or clearEdit().
void KHistoryComboBox::setHistoryItems(const QStringList &items)
Inserts items into the combobox. items might get truncated if it is longer than maxCount()
Note: Setter function for property historyItems.
See also historyItems.
void KHistoryComboBox::setHistoryItems(const QStringList &items, bool setCompletionList)
Inserts items into the combobox. items might get truncated if it is longer than maxCount()
Set setCompletionList
to true, if you don't have a list of completions. This tells KHistoryComboBox to use all the items for the completion object as well. You won't have the benefit of weighted completion though, so normally you should do something like
KConfigGroup config(KSharedConfig::openConfig(), "somegroup"); // load the history and completion list after creating the history combo QStringList list; list = config.readEntry("Completion list", QStringList()); combo->completionObject()->setItems(list); list = config.readEntry("History list", QStringList()); combo->setHistoryItems(list); [...] // save the history and completion list when the history combo is // destroyed QStringList list; KConfigGroup config(KSharedConfig::openConfig(), "somegroup"); list = combo->completionObject()->items(); config.writeEntry("Completion list", list); list = combo->historyItems(); config.writeEntry("History list", list);
Be sure to use different names for saving with KConfig if you have more than one KHistoryComboBox.
Note: When setCompletionList is true, the items are inserted into the KCompletion object with mode KCompletion::Insertion and the mode is set to KCompletion::Weighted afterwards.
Note: Setter function for property historyItems.
See also historyItems, KComboBox::completionObject, KCompletion::setItems, and KCompletion::items.
[since 5.66]
void KHistoryComboBox::setIconProvider(std::function<QIcon (const QString &)> providerFunction)
Sets an icon provider, so that items in the combobox can have an icon.
The provider is a function that takes a QString and returns a QIcon
This function was introduced in 5.66.
[protected]
bool KHistoryComboBox::useCompletion() const
Returns if we can modify the completion object or not.