QML QtQuick.Controls 2.2 Combobox doesn't have selectByMouse; What is the alternative?

1.3k views Asked by At

We are using QtQuick.Controls 2.2 and can't downgrade due to various reasons. When we use Combobox util from QML, it doesn't appear with selectByMouse field which was introduced in 1.4 version.

Our requirement is -- to be able to select the text in the combobox for copying purpose as well as have a dropdown menu.

How to fix this issue; Is there any alternative?

2

There are 2 answers

1
Mark Ch On

As of Qt 5.9 / Quick Controls 2.2, the ComboBox now contains a TextField to show the current text, if the ComboBox is set as editible. TextField does have the selectByMouse property you require, only it is not exposed as a property of the ComboBox so it is not accessible by QML. However, it can be accessed in the javascript, e.g. from the Component.onCompleted attached signal handler.

For example:

ComboBox {
    editable: true

    model: ListModel {
        id: model
        ListElement { text: "Banana" }
        ListElement { text: "Apple" }
        ListElement { text: "Coconut" }
    }

    Component.onCompleted: {
        contentItem.selectByMouse = true
    }
}
4
derM - not here for BOT dreams On

You can just alter the contentItem to be a TextField of with the properties of your choice. This might look like this:

ComboBox {
    id: control
    model: ['Hallo', 'Hello', 'Sallut', 'Godan Dagin']
    editable: true

    contentItem: TextField {
        text: control.editText
        selectByMouse: true
    }
}

Note that, if you edit the text, and editText is not an element of your model, it will not be accepted to be the displayText.

This works for QtQuick.Controls 2.2 onwards, as the properties editable and editText need to exist. Then it will automatically copy the edited text back to displayText once it is a valid input.
For earlier versions, this is much harder to achieve.