QML:将焦点TextInput设置在表格单元格中

时间:2018-01-06 12:09:40

标签: c++ qt qml

我有一个表,可以在列中包含TextInput或ComboBox,我想要“标记”这些行并输入我的数据。我怎么能强制表只给焦点TextInput或Comboboxes而不是整行? 奇怪的是,如果我点击每个TextInput一次,行为几乎是我想要的,我可以使用键向上和向下导航输入,我猜测行“记住”最后一个焦点项目。

以下是表格的外观(只有'Value'列可编辑): Table

以下是TextInput的代码:

import QtQuick 2.0
import ".."

FocusScope {
    property alias text: textInput.text
    property alias font: textInput.font
    x: rectangle.x; y: rectangle.y
    width: rectangle.width; height: rectangle.height

    Rectangle {
        id: rectangle
        anchors.fill: parent
        border.color: Style.table.itemBorderColor
        color: textInput.activeFocus ? "lightgray" : "white"

        TextInput {
            id: textInput
            focus: true
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.verticalCenter: parent.verticalCenter
            anchors.leftMargin: 5
            anchors.rightMargin: 5
            color: parent.enabled ? "grey" : ""
            selectByMouse: true
            enabled: parent.enabled
            clip: true

            font.pixelSize: Style.fontSize
        }
    }
}

这是关于表如何加载委托的缩写版本:

StepTableView {
    ...

    TableViewColumn {
        id: tcValue
        role: "value"
        title: qsTr("Value")

        delegate:   Component {
            Loader {
                sourceComponent: {
                    // can happen if the model has not loaded yet
                    if (model === undefined || model === null || parent === null ) {
                        return;
                    }

                    return Qt.createQmlObject("import QtQuick 2.0;" +
                                              "Component {" +
                                              "Item { Component.onCompleted: loadComponent(this, model, styleData)}}", parent);
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

好的,我明白了。我不知道是否有更简单的解决方案,但这是我的:

由于我不知道如何从currentRow获取实际的Item,我在我的模型中添加了一个QHash,它存储了行索引和包含的Object(例如TextInput):

bool BacktraceTableModel::setData(const QModelIndex &index, const QVariant &value, int role /* = Qt::EditRole */)
{
    if (!index.isValid())
    {
        return false;
    }

    if (role == FocusObjectRole)
    {
        focusObjectList_.insert(index.row(), value);
        return true;
    }
...
}

表QML:

TableView {
    function navigateUp(event, tabToPrevious) {
        console.log("navigation: " + currentRow);
        if (currentRow > 0) {
            currentRow--;
            var focusObject = model.get(currentRow).focusObject;
            if (typeof(focusObject) != 'undefined') {
                focusObject.forceActiveFocus();
                event.accepted = true;
            } else {
                console.log("Cannot get focus object for row: " + currentRow);
            }
        } else {
            if (tabToPrevious) {
                KeyNavigation.backtab.forceActiveFocus();
            }
        }
    }

    function navigateDown(event, tabToNext) {
        console.log("navigation: " + currentRow);
        if (currentRow < rowCount - 1) {
            currentRow++;
            var focusObject = model.get(currentRow).focusObject;
            if (typeof(focusObject) != 'undefined') {
                focusObject.forceActiveFocus();
            } else {
                console.log("Cannot get focus object for row: " + currentRow);
            }
        } else {
            if (tabToNext) {
                KeyNavigation.tab.forceActiveFocus();
            }
        }
    }

    Keys.onUpPressed: navigateUp(event, false)
    Keys.onDownPressed:navigateDown(event, false)
    Keys.onTabPressed: navigateDown(event, true)
    Keys.onBacktabPressed: navigateUp(event, true)

    onFocusChanged: {
        console.log("navigation: " + currentRow);
        if (activeFocus) {
            // on first table focus the currentRow = -1
            currentRow = currentRow > 0 ? currentRow : 0;
            var focusObject = model.get(currentRow).focusObject;
            if (typeof(focusObject) != 'undefined') {
                focusObject.forceActiveFocus();
            } else {
                console.log("Cannot get focus object for row: " + currentRow);
            }
        }
    }

TableView中的navigateUpnavigateDown函数更改currentRow并从模型中获取QML项,然后获得活动焦点。

如果按下Tab / Backtab键并且模型位于其末尾或开始,则KeyNavigation.tab中的下一个元素将获得焦点,因此您可以跳出表格。

onFocusChanged事件处理该事件,如果您选择&#34;标签为&#34;该表,然后将焦点放在最后一个选择行或第一个行。