Reputation: 2886
I have a fairly trivial QML example:
import QtQuick 2.10
import QtQuick.Controls 2.1
import QtQuick.Window 2.10
Window {
id: window
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Button {
id: but
text: "press"
onPressed: {
profileModel.insertRow(list.count)
list.currentIndex = list.count-1
list.currentItem.focus = true
list.currentItem.text = "focused " + list.currentIndex
//list.currentItem.cursorVisible = true
}
}
ListView {
id: list
anchors.top: but.bottom
anchors.bottom: parent.bottom
anchors.left: parent.left
anchors.right: parent.right
model: profileModel
delegate: TextInput {
text: display
//activeFocusOnPress: true
onFocusChanged: {
console.log("onFocusChanged " + index + ", " + focus)
}
onEditingFinished: {
console.log("onEditingFinished " + index + ", " + focus)
}
}
}
}
profileModel
is defined as:
ProfileModel::ProfileModel(QObject *parent)
: QAbstractListModel(parent)
, m_count(3)
{
}
QVariant ProfileModel::data(const QModelIndex &index, int role) const
{
return index.row();
}
Qt::ItemFlags ProfileModel::flags(const QModelIndex &index) const
{
return QAbstractListModel::flags(index) | Qt::ItemIsEditable;
}
bool ProfileModel::insertRows(int position, int rows, const QModelIndex &index)
{
beginInsertRows(QModelIndex(), position, position + rows - 1);
m_count++;
endInsertRows();
return true;
}
bool ProfileModel::removeRows(int position, int rows, const QModelIndex &index)
{
return QAbstractListModel::removeRows(position, rows, index);
}
int ProfileModel::rowCount(const QModelIndex &parent) const
{
return m_count;
}
bool ProfileModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
return true;
}
No matter what I do I can't get focus on TextInput
after I push the button. From logs everything seems to be correct, i.e. after pushing the button I see I lost focus from the previous item and got focus on the newly created one but that's pretty much it:
D/libuntitled.so( 6871): qrc:/main.qml:38 (onFocusChanged): qml: onFocusChanged 3, true
D/libuntitled.so( 6871): qrc:/main.qml:38 (onFocusChanged): qml: onFocusChanged 0, false
If I want to edit the line (or even move the cursor) I have to click on it.
I can force showing the keyboard using Qt.inputMethod.show()
or showing the cursor using cursorVisible = true
but the input field just wouldn't activate.
I'm using Qt 5.10.0.
Upvotes: 3
Views: 2646
Reputation: 2886
I was too impatient. This answer gives great details when to call which focus-related function. In short, I had to call forceActiveFocus()
instead of just setting focus = true
.
Upvotes: 2