Reputation: 70030
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?
Upvotes: 4
Views: 1453
Reputation: 13691
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.
Upvotes: 5
Reputation: 3030
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
}
}
Upvotes: 4