QML ListView + TextInput фокус

У меня есть довольно тривиальный пример QML:

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 определяется как:

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;
}

Независимо от того, что я делаю, я не могу сосредоточиться на TextInput после того, как нажму кнопку. Судя по журналам, все кажется правильным, т.е. после нажатия кнопки я вижу, что потерял фокус от предыдущего элемента и сосредоточился на вновь созданном, но это почти все:

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

Если я хочу отредактировать строку (или даже переместить курсор), я должен щелкнуть по ней.

Я могу принудительно показать клавиатуру с помощью Qt.inputMethod.show() или показать курсор с помощью cursorVisible = true, но поле ввода просто не активируется.

Я использую Qt 5.10.0.


person Miro Kropacek    schedule 01.04.2018    source источник


Ответы (1)


Я был слишком нетерпелив. В этом ответе подробно описано, когда вызывать какую функцию, связанную с фокусом. Короче, мне пришлось вызвать forceActiveFocus() вместо того, чтобы просто установить focus = true.

person Miro Kropacek    schedule 01.04.2018