KCompletionBase Class

An abstract base class for adding a completion feature into widgets. More...

Header: #include <KCompletionBase>
CMake: find_package(KF6 REQUIRED COMPONENTS Completion)
target_link_libraries(mytarget PRIVATE KF6::Completion)
Inherited By:

KComboBox and KLineEdit

Public Types

enum KeyBindingType { TextCompletion, PrevCompletionMatch, NextCompletionMatch, SubstringCompletion }

Public Functions

KCompletionBase()
KCompletion *compObj() const
KCompletion::CompletionMode completionMode() const
KCompletion *completionObject(bool handleSignals = true)
bool emitSignals() const
bool handleSignals() const
bool isCompletionObjectAutoDeleted() const
(since 5.0) QList<QKeySequence> keyBinding(KCompletionBase::KeyBindingType item) const
void setAutoDeleteCompletionObject(bool autoDelete)
virtual void setCompletedItems(const QStringList &items, bool autoSuggest = true) = 0
virtual void setCompletedText(const QString &text) = 0
virtual void setCompletionMode(KCompletion::CompletionMode mode)
virtual void setCompletionObject(KCompletion *completionObject, bool handleSignals = true)
void setEmitSignals(bool emitRotationSignals)
void setEnableSignals(bool enable)
virtual void setHandleSignals(bool handle)
bool setKeyBinding(KCompletionBase::KeyBindingType item, const QList<QKeySequence> &key)
void useGlobalKeyBindings()

Protected Functions

KCompletionBase *delegate() const
(since 5.0) KCompletionBase::KeyBindingMap keyBindingMap() const
void setDelegate(KCompletionBase *delegate)
void setKeyBindingMap(KCompletionBase::KeyBindingMap keyBindingMap)

Detailed Description

This is a convenience class that provides the basic functions needed to add text completion support into widgets. All that is required is an implementation for the pure virtual function setCompletedText(). Refer to KLineEdit or KComboBox to see how easily such support can be added using this as a base class.

Member Type Documentation

enum KCompletionBase::KeyBindingType

Constants that represent the items whose shortcut key binding is programmable. The default key bindings for these items are defined in KStandardShortcut.

ConstantValueDescription
KCompletionBase::TextCompletion0Text completion (by default Ctrl-E).
KCompletionBase::PrevCompletionMatch1Switch to previous completion (by default Ctrl-Up).
KCompletionBase::NextCompletionMatch2Switch to next completion (by default Ctrl-Down).
KCompletionBase::SubstringCompletion3Substring completion (by default Ctrl-T).

Member Function Documentation

KCompletionBase::KCompletionBase()

Default constructor.

KCompletion *KCompletionBase::compObj() const

Returns a pointer to the completion object.

This method is only different from completionObject() in that it does not create a new KCompletion object even if the internal pointer is nullptr. Use this method to get the pointer to a completion object when inheriting so that you will not inadvertently create it.

Returns the completion object or nullptr if one does not exist.

KCompletion::CompletionMode KCompletionBase::completionMode() const

Returns the current completion mode.

See also setCompletionMode().

KCompletion *KCompletionBase::completionObject(bool handleSignals = true)

Returns a pointer to the current completion object.

If the completion object does not exist, it is automatically created and by default handles all the completion signals internally unless handleSignals is set to false. It is also automatically destroyed when the destructor is called. You can change this default behavior using the setAutoDeleteCompletionObject and setHandleSignals member functions.

See also compObj.

handleSignals if true, handles completion signals internally.

See also setCompletionObject().

[protected] KCompletionBase *KCompletionBase::delegate() const

Returns the delegation object, or nullptr if there is none

See also setDelegate().

bool KCompletionBase::emitSignals() const

Returns true if the object emits the signals.

See also setEmitSignals().

bool KCompletionBase::handleSignals() const

Returns true if the object handles the signals.

See also setHandleSignals().

bool KCompletionBase::isCompletionObjectAutoDeleted() const

Returns true if the completion object is deleted upon this widget's destruction.

See setCompletionObject() and enableCompletion() for details.

Returns true if the completion object will be deleted automatically

[since 5.0] QList<QKeySequence> KCompletionBase::keyBinding(KCompletionBase::KeyBindingType item) const

Returns the key binding used for the specified item.

This method returns the key binding used to activate the feature given by item. If the binding contains modifier key(s), the sum of the modifier key and the actual key code is returned.

item the item to check

Returns the key binding used for the feature given by item.

This function was introduced in 5.0.

See also setKeyBinding.

[protected, since 5.0] KCompletionBase::KeyBindingMap KCompletionBase::keyBindingMap() const

Returns a key binding map.

This method is the same as getKeyBinding(), except that it returns the whole keymap containing the key bindings.

Returns the key binding used for the feature given by item.

This function was introduced in 5.0.

See also setKeyBindingMap().

void KCompletionBase::setAutoDeleteCompletionObject(bool autoDelete)

Sets the completion object when this widget's destructor is called.

If the argument is set to true, the completion object is deleted when this widget's destructor is called.

autoDelete if true, delete completion object on destruction.

[pure virtual] void KCompletionBase::setCompletedItems(const QStringList &items, bool autoSuggest = true)

A pure virtual function that must be implemented by all inheriting classes.

items the list of completed items

autoSuggest if true, the first element of items is automatically completed (i.e. preselected).

[pure virtual] void KCompletionBase::setCompletedText(const QString &text)

A pure virtual function that must be implemented by all inheriting classes.

This function is intended to allow external completion implementations to set completed text appropriately. It is mostly relevant when the completion mode is set to CompletionAuto and CompletionManual modes. See KCompletionBase::setCompletedText. Does nothing in CompletionPopup mode, as all available matches will be shown in the popup.

text the completed text to be set in the widget.

[virtual] void KCompletionBase::setCompletionMode(KCompletion::CompletionMode mode)

Sets the type of completion to be used.

mode Completion type

See also completionMode() and CompletionMode.

[virtual] void KCompletionBase::setCompletionObject(KCompletion *completionObject, bool handleSignals = true)

Sets up the completion object to be used.

This method assigns the completion object and sets it up to automatically handle the completion and rotation signals internally. You should use this function if you want to share one completion object among your widgets or need to use a customized completion object.

The object assigned through this method is not deleted when this object's destructor is invoked unless you explicitly call setAutoDeleteCompletionObject after calling this method. Be sure to set the bool argument to false, if you want to handle the completion signals yourself.

completionObject a KCompletion or a derived child object.

handleCompletionSignals if true, handles completion signals internally.

See also completionObject().

[protected] void KCompletionBase::setDelegate(KCompletionBase *delegate)

Sets or removes the delegation object. If a delegation object is set, all function calls will be forwarded to the delegation object. delegate the delegation object, or nullptr to remove it

See also delegate().

void KCompletionBase::setEmitSignals(bool emitRotationSignals)

Sets whether the object emits rotation signals.

emitRotationSignals if false, disables the emission of rotation signals.

See also emitSignals().

void KCompletionBase::setEnableSignals(bool enable)

Sets the widget's ability to emit text completion and rotation signals.

Invoking this function with enable set to false will cause the completion and rotation signals not to be emitted. However, unlike setting the completion object to nullptr using setCompletionObject, disabling the emission of the signals through this method does not affect the current completion object.

There is no need to invoke this function by default. When a completion object is created through completionObject or setCompletionObject, these signals are set to emit automatically. Also note that disabling this signals will not necessarily interfere with the objects' ability to handle these events internally. See setHandleSignals.

enable if false, disables the emission of completion and rotation signals.

[virtual] void KCompletionBase::setHandleSignals(bool handle)

Enables this object to handle completion and rotation events internally.

This function simply assigns a boolean value that indicates whether it should handle rotation and completion events or not. Note that this does not stop the object from emitting signals when these events occur.

handle if true, it handles completion and rotation internally.

See also handleSignals().

bool KCompletionBase::setKeyBinding(KCompletionBase::KeyBindingType item, const QList<QKeySequence> &key)

Sets the key binding to be used for manual text completion, text rotation in a history list as well as a completion list.

When the keys set by this function are pressed, a signal defined by the inheriting widget will be activated. If the default value or 0 is specified by the second parameter, then the key binding as defined in the global setting should be used. This method returns false when key is negative or the supplied key binding conflicts with another one set for another feature.

Note: To use a modifier key (Shift, Ctrl, Alt) as part of the key binding simply sum up the values of the modifier and the actual key. For example, to use CTRL+E, supply "Qt::CtrlButton | Qt::Key_E" as the second argument to this function.

item the feature whose key binding needs to be set:

key key binding used to rotate down in a list.

Returns true if key binding is successfully set.

See also keyBinding.

[protected] void KCompletionBase::setKeyBindingMap(KCompletionBase::KeyBindingMap keyBindingMap)

Sets the keymap.

keyBindingMap

See also keyBindingMap().

void KCompletionBase::useGlobalKeyBindings()

Sets this object to use global values for key bindings.

This method changes the values of the key bindings for rotation and completion features to the default values provided in KGlobalSettings.

Note: By default, inheriting widgets should use the global key bindings so that there is no need to call this method.