ComboBox QML Type

  • Import Statement: import QtQuick.Controls 1.4

  • Since: Qt 5.1

  • Inherits: FocusScope

  • Group: ComboBox is part of Buttons and Controls

Detailed Description

Image non disponible

Add items to the ComboBox by assigning it a ListModel, or a list of strings to the model property.

 
Sélectionnez
ComboBox {
    width: 200
    model: [ "Banana", "Apple", "Coconut" ]
}

In this example we are demonstrating how to use a ListModel with a combo box.

 
Sélectionnez
ComboBox {
    currentIndex: 2
    model: ListModel {
        id: cbItems
        ListElement { text: "Banana"; color: "Yellow" }
        ListElement { text: "Apple"; color: "Green" }
        ListElement { text: "Coconut"; color: "Brown" }
    }
    width: 200
    onCurrentIndexChanged: console.debug(cbItems.get(currentIndex).text + ", " + cbItems.get(currentIndex).color)
}

You can make a combo box editable by setting the editable property. An editable combo box will autocomplete its text based on what is available in the model.

In the next example we demonstrate how you can append content to an editable combo box by reacting to the accepted signal. Note that you have to explicitly prevent duplicates.

 
Sélectionnez
ComboBox {
    editable: true
    model: ListModel {
        id: model
        ListElement { text: "Banana"; color: "Yellow" }
        ListElement { text: "Apple"; color: "Green" }
        ListElement { text: "Coconut"; color: "Brown" }
    }
    onAccepted: {
        if (find(currentText) === -1) {
            model.append({text: editText})
            currentIndex = find(editText)
        }
    }
}

You can create a custom appearance for a ComboBox by assigning a ComboBoxStyle.

Property Documentation

 

[read-only, since QtQuick.Controls 1.1] acceptableInput : bool

Returns true if the combo box contains acceptable text in the editable text field.

If a validator was set, this property will return true if the current text satisfies the validator or mask as a final string (not as an intermediate string).

This property was introduced in QtQuick.Controls 1.1.

See Also

See also validator, accepted

activeFocusOnPress : bool

This property specifies whether the combobox should gain active focus when pressed. The default value is false.

[read-only, since QtQuick.Controls 1.1] count : int

This property holds the number of items in the combo box.

This property was introduced in QtQuick.Controls 1.1.

currentIndex : int

The index of the currently selected item in the ComboBox.

Setting currentIndex to -1 will reset the selection and clear the text label. If editable is true, you may also need to manually clear editText.

See Also

See also model

[read-only] currentText : string

The text of the currently selected item in the ComboBox.

Since currentText depends on currentIndex, there's no way to ensure currentText will be up to date whenever a onCurrentIndexChanged handler is called.

[since QtQuick.Controls 1.1] editText : string

This property specifies text being manipulated by the user for an editable combo box.

This property was introduced in QtQuick.Controls 1.1.

[since QtQuick.Controls 1.1] editable : bool

This property holds whether the combo box can be edited by the user. The default value is false.

This property was introduced in QtQuick.Controls 1.1.

[read-only] hovered : bool

This property indicates whether the control is being hovered.

[read-only, since QtQuick.Controls 1.3] inputMethodComposing : bool

This property holds whether an editable ComboBox has partial text input from an input method.

While it is composing an input method may rely on mouse or key events from the ComboBox to edit or commit the partial text. This property can be used to determine when to disable events handlers that may interfere with the correct operation of an input method.

This property was introduced in QtQuick.Controls 1.3.

[since QtQuick.Controls 1.5] inputMethodHints : enumeration

Provides hints to the input method about the expected content of the combo box and how it should operate.

The value is a bit-wise combination of flags or Qt.ImhNone if no hints are set.

Flags that alter behavior are:

  • Qt.ImhHiddenText - Characters should be hidden, as is typically used when entering passwords.

  • Qt.ImhSensitiveData - Typed text should not be stored by the active input method in any persistent storage like predictive user dictionary.