如何将焦点设置到ListView委托内的TextField?

时间:2019-11-24 00:41:40

标签: qt qml qt5

我有一个ListView,里面有一些文本输入字段:

Window {
    visible: true

    ListModel {
        id: textModel
        ListElement {
            text: "Bill Smith"
        }
        ListElement {
            text: "John Brown"
        }
        ListElement {
            text: "Sam Wise"
        }
    }

    ListView {
        width: 180; height: 200
        focus: true

        model: textModel
        delegate: RowLayout{
            id: layout
            Label {
                text: model.text
            }
            TextField {
                text: model.text
            }
        }
    }
}

我想将输入焦点设置为列表中的第一个TextField。我怎样才能做到这一点?如果我在ListView中添加focus: true则无济于事。

1 个答案:

答案 0 :(得分:2)

您必须使用ListView.isCurrentItem属性来激活TextField的焦点:

#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <ostream>
#include <istream>

int main ()
{
  // Declare your queue of type std::string
  std::queue<std::string> q;

  // Push 1 to 3
  q.push ("1");
  q.push ("2");
  q.push ("3");

  // Declare a string variable
  std::string input;

  // Prompt
  std::cout << "- Please input a string: " << std::endl;

  // Catch user input and store
  std::cin >> input;

  // Push value inputted by the user
  q.push(input);

  // Loop while the queue is not empty, while popping each value
  while (not q.empty ())
    {
      // Output front of the queue
      std::cout << q.front () << std::endl;
      // Pop the queue, delete item
      q.pop ();
    }
  // New line, formatting purposes
  std::cout << std::endl;

  return 0;
}