新建一个QML项目, main.cpp不动如下:

  1. #include <QGuiApplication>
  2. #include <QQmlApplicationEngine>
  3. int main(int argc, char *argv[])
  4. {
  5. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  6. QGuiApplication app(argc, argv);
  7. QQmlApplicationEngine engine;
  8. const QUrl url(QStringLiteral("qrc:/main.qml"));
  9. QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
  10. &app, [url](QObject *obj, const QUrl &objUrl) {
  11. if (!obj && url == objUrl)
  12. QCoreApplication::exit(-1);
  13. }, Qt::QueuedConnection);
  14. engine.load(url);
  15. return app.exec();
  16. }

主界面main.qml如下

  1. import QtQuick 2.12
  2. import QtQuick.Window 2.12
  3. Window {
  4. visible: true
  5. width: 640
  6. height: 480
  7. title: qsTr("Hello World")
  8. DemoList {
  9. anchors.centerIn: parent
  10. width: parent.width
  11. height: parent.height
  12. }
  13. }

用到的Model文件 MyModel.qml 如下:

  1. import QtQuick 2.0
  2. ListModel {
  3. ListElement {
  4. description: "Wash the car"
  5. done: true
  6. }
  7. ListElement {
  8. description: "Read a book"
  9. done: false
  10. }
  11. ListElement {
  12. description: "Buy a cup"
  13. done: false
  14. }
  15. }

主要界面 DemoList.qml :

  1. import QtQuick 2.12
  2. import QtQuick.Controls 2.5
  3. import QtQuick.Layouts 1.3
  4. import QtQml.Models 2.1
  5. /**
  6. 方法2: 通过设置currentIndex, 属性自动变化. 支持键盘
  7. */
  8. ColumnLayout {
  9. RowLayout {
  10. Layout.fillHeight: true
  11. Layout.leftMargin: 10
  12. Layout.rightMargin: 20
  13. Layout.alignment: Qt.AlignHCenter | Qt.AlignTop
  14. Component {
  15. id: delegateItem
  16. Rectangle {
  17. id: dragRect
  18. height: 50
  19. width: parent.width
  20. onFocusChanged: {
  21. if(focus){
  22. console.debug("got focus dragRect" );
  23. textinput.focus = true;
  24. }
  25. }
  26. CheckBox {
  27. id: chkbox
  28. width: 50
  29. checked: model.done
  30. onClicked: model.done = checked
  31. }
  32. Rectangle {
  33. id: textSection
  34. height: parent.height
  35. width: parent.width - chkbox.width
  36. anchors.left: chkbox.right
  37. Text {
  38. id: textShow
  39. text: model.description
  40. font.underline: true
  41. font.bold: true
  42. anchors.verticalCenter: parent.verticalCenter
  43. visible: !dragRect.ListView.isCurrentItem
  44. color: "black"
  45. } //end textShow
  46. TextInput {
  47. id: textinput
  48. anchors.verticalCenter: parent.verticalCenter
  49. color: "blue"
  50. text: model.description
  51. visible: dragRect.ListView.isCurrentItem
  52. enabled: dragRect.ListView.isCurrentItem
  53. focus: true
  54. onFocusChanged: {
  55. if(focus){
  56. console.debug("got focus" + "textInput");
  57. }
  58. }
  59. onEditingFinished: {
  60. model.description = textinput.text
  61. //方法1: 设置index
  62. if(listView.currentIndex == index){
  63. listView.currentIndex = -1;
  64. }
  65. console.log(("TextInput listview currentIndex: " + listView.currentIndex + " index: " + index ))
  66. console.log(("TextInput ListView isCurrentItem: " + dragRect.ListView.isCurrentItem))
  67. }
  68. } //end TextInput
  69. } //end textSection Rectangle
  70. MouseArea {
  71. id: mouseArea
  72. width: parent.width
  73. height: parent.height
  74. hoverEnabled: true
  75. z: -1 //避免遮住checkbox
  76. onClicked: {
  77. //方法1: 设置当前
  78. listView.currentIndex = index
  79. console.log(("MouseArea listview currentIndex: " + listView.currentIndex + " index: " + index ))
  80. console.log(("MouseArea ListView isCurrentItem: " + dragRect.ListView.isCurrentItem))
  81. // 在dragRect的 onFocusChanged 中设置了, 此处不需要了
  82. //textinput.focus = true;
  83. }
  84. }
  85. }
  86. } //end Row Rectangle
  87. ListView {
  88. id: listView
  89. Layout.fillWidth: true
  90. Layout.fillHeight: true
  91. keyNavigationEnabled: true
  92. //clip: true
  93. model: MyModel {}
  94. delegate: delegateItem
  95. //默认不要是第一个, 否则第一个是可编辑状态(针对方法1)
  96. Component.onCompleted : {
  97. currentIndex = -1;
  98. }
  99. //默认焦点
  100. focus: true
  101. }
  102. }
  103. RowLayout {
  104. Button {
  105. text: qsTr("Add New Item")
  106. Layout.fillWidth: true
  107. onClicked: {
  108. var c = listView.model.rowCount();
  109. listView.model.append({
  110. "description": "Buy a new book " + (c + 1),
  111. "done": false
  112. })
  113. //设置焦点, 否则listView就没焦点了
  114. listView.focus = true;
  115. listView.currentIndex = c;
  116. }
  117. }
  118. }
  119. }

几个关键点, 详见代码:

  • Text/TextInput的visible属性
  • MouseArea的点击事件: 设置 listView.currentIndex = index
  • MouseArea避免遮挡: z: -1 //避免遮住checkbox
  • ListView的 Component.onCompleted, 设置默认不选中
  • ListView默认设置: focus: true
  • TextInput的onEditingFinished, 处理编辑完成事件
  • 组件dragRect的onFocusChanged, 使TextInput获得焦点

项目代码

https://github.com/cnscud/learn/tree/master/qt/editListView

Qt-可编辑的ListView的更多相关文章

  1. OSG嵌入QT(QT界面使用Qt Designer编辑)

    本文主要内容:使用Qt Designer编辑好QT界面后,将OSG中的ViewerWidget嵌入到QT的Widget中. 在VS中嵌入QT工具,建立QT GUIApplication后,打开自动生成 ...

  2. QT creator 编辑多个UI 文件 出现 无法解析的外部符号的错误

    创建一般的Qt Gui 程序一般会默认一个UI 文件 ,但是随着应用程序窗口的增多,同时编辑多个UI 界面是必须的. 假设我们已经创建好了一个QTUI的工程,里面已经默认了一个UI文件,但是想在添几个 ...

  3. ubuntu系统中Qt creator 编辑和应用使用中文输入法

    在ubuntu系统的GUI开发过程中遇到在编辑器里面不能使用中文输入法,前提我已经安装了搜狗输入法,但是还是不能使用,原因是QT的库里没有最新fcix的库,. 没有安装搜狗的输入法的 https:// ...

  4. Qt - QLineEdit编辑框

    QLineEdit输入内容获取及合理性检查? 控件自带触发信息: void textChanged(const QString &);void textEdited(const QString ...

  5. QT 下把编辑框内的中文字符转换为 char*

    第一种方法 QString str(tr("你好")); wchar_t wc[100] = {0}; pEditShortDes->text().toWCharArray( ...

  6. QT-可拖拽可编辑的多控件ListView

    目标 结合前面的2篇文章, 继续升级QML版本的ListView: 又要拖拽, 又要可编辑, 还得支持多个控件. 循序渐进 本文基于前一篇的基础: Qt-可编辑的ListView 要循序渐进的学习. ...

  7. Qt, 我回来了。。。

    说起qt,大学时就有接触,但一直没有深入,这个周六周天利用两于时间重新温习了一下,跟之前用过的vs上的MFC.C++ builder比起来,Qt封装很人性化,库也比较全,写个 一般的小工具很轻松. 参 ...

  8. 简单QT应用了可实现手动布局QT应用

     新建QT项目 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/4 ...

  9. Qt入门之基础篇 ( 二 ) :Qt项目建立、编译、运行和发布过程解析

    转载请注明出处:CN_Simo. 题解: 本篇内容主讲Qt应用从创建到发布的整个过程,旨在帮助读者能够快速走进Qt的世界. 本来计划是讲解Qt源码静态编译,如此的话读者可能并不能清楚地知道为何要静态编 ...

随机推荐

  1. 备份schema并排除大表到ASM磁盘上

    1.查出占用空间比较大的表 select owner,segment_name,segment_type,bytes/1024/1024 mb from dab_segment where owner ...

  2. ES6 学习笔记之对象的新增方法

    1. Object.is() ES5 比较两个值是否相等,只有两个运算符:相等运算符(==)和严格相等运算符(===).它们都有缺点,前者会自动转换数据类型,后者的 NaN 不等于自身,以及 +0 等 ...

  3. Linux中cut,sort,uniq和wc的用法

    一.cut是一个选取命令,就是将一段数据经过分析,取出我们想要的.一般来说,选取信息通常是针对"行"来进行分析的,并不是整篇信息分析的.1.语法格式为:cut [-bn] [fil ...

  4. Zabbix5.0钉钉报警(centos7)

    2.1.到钉钉官网下载pc版钉钉,安装.注册.登陆: 钉钉下载地址:https://www.dingtalk.com/ 2.2.创建群聊和钉钉机器人: 1.创建群聊,把需要收到报警的人员都拉到这个群: ...

  5. 02 jumpserver系统设置

    2.系统设置: (1)基本设置: (2)邮件设置: 1)163邮箱设置: 2)在jumpserver上填写邮箱信息: 3)邮件测试信息如下: (3)邮件内容设置: (4)终端设置: (5)安全设置:

  6. 9.11、mysql增量备份和增量恢复介绍

    1.增量备份: 增量数据是从上次全量备份之后,更新的新数据,对于mysql来说,binlog日志就是mysql的增量数据: (1)按天进行备份: 周一00点全量备份 周二00点全量备份 ...... ...

  7. flex mx:TabNavigator进行选项卡切换,需要进行交互时。发生Error #1009错误

    当需要进行 mx:TabNavigator选项卡进行切换时,需要进行交互,然后却报了"TypeError: Error #1009: 无法访问空对象引用的属性或方法."错误,产生这 ...

  8. Redis 底层数据结构之String

    文章参考:<Redis设计与实现>黄建宏 Redis 的 string 类型底层使用的是 SDS(动态字符串) 实现的, 具体数据结构如下: struct sdshdr { int len ...

  9. 资源:Git快速下载路径

    Git快速下载地址: 地址:https://npm.taobao.org/mirrors/git-for-windows/

  10. 关于easyswoole实现websocket聊天室的步骤解析

    在去年,我们公司内部实现了一个聊天室系统,实现了一个即时在线聊天室功能,可以进行群组,私聊,发图片,文字,语音等功能,那么,这个聊天室是怎么实现的呢?后端又是怎么实现的呢? 后端框架 在后端框架上,我 ...