Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QML:在表格单元格中设置焦点文本输入_C++_Qt_Qml - Fatal编程技术网

C++ QML:在表格单元格中设置焦点文本输入

C++ QML:在表格单元格中设置焦点文本输入,c++,qt,qml,C++,Qt,Qml,我有一个表,它可以在一列中包含一个文本输入或一个组合框,我想通过这些行“制表”并输入我的数据。如何强制表只关注TextInput或ComboBox,而不是整行? 奇怪的是,如果我在每个文本输入中单击一次,行为几乎就是我想要的,我可以通过上下键箭头在输入中导航,我猜行“记住”了最后一个关注的项目 以下是表格的外观(只有“值”列是可编辑的): 以下是文本输入的代码: import QtQuick 2.0 import ".." FocusScope { property alias te

我有一个表,它可以在一列中包含一个文本输入或一个组合框,我想通过这些行“制表”并输入我的数据。如何强制表只关注TextInput或ComboBox,而不是整行? 奇怪的是,如果我在每个文本输入中单击一次,行为几乎就是我想要的,我可以通过上下键箭头在输入中导航,我猜行“记住”了最后一个关注的项目

以下是表格的外观(只有“值”列是可编辑的):

以下是文本输入的代码:

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
        }
    }
}
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;
    }
...
}
以下是表格如何加载代理的缩写版本:

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

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

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

表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{
函数navigateUp(事件,tabToPrevious){
console.log(“导航:+currentRow”);
如果(当前行>0){
当前行--;
var focusObject=model.get(currentRow).focusObject;
if(typeof(focusObject)!=“未定义”){
focusObject.forceActiveFocus();
event.accepted=true;
}否则{
log(“无法获取行:+currentRow的焦点对象”);
}
}否则{
如果(禁忌){
KeyNavigation.backtab.forceActiveFocus();
}
}
}
函数navigateDown(事件,tabToNext){
console.log(“导航:+currentRow”);
如果(当前行<行计数-1){
currentRow++;
var focusObject=model.get(currentRow).focusObject;
if(typeof(focusObject)!=“未定义”){
focusObject.forceActiveFocus();
}否则{
log(“无法获取行:+currentRow的焦点对象”);
}
}否则{
if(tabToNext){
KeyNavigation.tab.forceActiveFocus();
}
}
}
Keys.onUpPressed:navigateUp(事件,false)
按键。onDownPressed:导航向下(事件,false)
Keys.ontab按下:导航向下(事件,true)
Keys.onBacktabPressed:navigateUp(事件,true)
onFocusChanged:{
console.log(“导航:+currentRow”);
if(activeFocus){
//在第一个表上,焦点为currentRow=-1
currentRow=currentRow>0?currentRow:0;
var focusObject=model.get(currentRow).focusObject;
if(typeof(focusObject)!=“未定义”){
focusObject.forceActiveFocus();
}否则{
log(“无法获取行:+currentRow的焦点对象”);
}
}
}
TableView中的
navigateUp
navigateDown
函数更改当前行并从模型中获取QML项,然后获取活动焦点

如果按下Tab/Backtab键且模型处于其末尾或开头,则
KeyNavigation.Tab中的下一个元素将获得焦点,因此您可以从表中进行Tab


onFocusChanged
事件处理事件,如果您“tab into”表,则会将焦点放在最后一个选择行或第一个选择行。

这可能是
Item.forceActiveFocus()
有帮助的情况之一。有些关联: