qt - QML ListView + TextInput 焦点

标签 qt qml qtquickcontrols2

我有一个相当简单的 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。

最佳答案

我太不耐烦了。 This answer提供了何时调用哪个与焦点相关的函数的详细信息。简而言之,我不得不调用 forceActiveFocus() 而不是仅仅设置 focus = true

关于qt - QML ListView + TextInput 焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49594336/

相关文章:

c++ - 在一个系统中关联另一个 make 实用程序时出错

c++ - 如何将 "warnings as error"规则添加到 Qt .pro 文件?

qt - 由于 QML 中的主线程阻塞错误,无法启动视频表面

c++ - 如何在 QtQuick 2 中对 QML TableView 进行排序?

qt - model.rowCount() 不会绑定(bind)到 Item 的属性

c++ - 在 QtQuick 中自定义样式

python - 如何重新实现Qt生成的Ui_MainWindow

qt - 在 Qt 中使用像素图或位图快速绘图

qt - 如何找到所有 QML 模块的最新版本

qt - 三态复选框 : deny user from setting it to PartiallyChecked