Qt-可编辑的ListView
新建一个QML项目, main.cpp不动如下:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
        &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
    engine.load(url);
    return app.exec();
}
主界面main.qml如下
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    DemoList {
        anchors.centerIn: parent
        width: parent.width
        height: parent.height
    }
}
用到的Model文件 MyModel.qml 如下:
import QtQuick 2.0
ListModel {
    ListElement {
        description: "Wash the car"
        done: true
    }
    ListElement {
        description: "Read a book"
        done: false
    }
    ListElement {
        description: "Buy a cup"
        done: false
    }
}
主要界面 DemoList.qml :
import QtQuick 2.12
import QtQuick.Controls 2.5
import QtQuick.Layouts 1.3
import QtQml.Models 2.1
/**
  方法2: 通过设置currentIndex, 属性自动变化. 支持键盘
*/
ColumnLayout {
    RowLayout {
        Layout.fillHeight: true
        Layout.leftMargin: 10
        Layout.rightMargin: 20
        Layout.alignment: Qt.AlignHCenter | Qt.AlignTop
        Component {
            id: delegateItem
            Rectangle {
                id: dragRect
                height: 50
                width: parent.width
                onFocusChanged: {
                    if(focus){
                        console.debug("got focus dragRect" );
                        textinput.focus = true;
                    }
                }
                CheckBox {
                    id: chkbox
                    width: 50
                    checked: model.done
                    onClicked: model.done = checked
                }
                Rectangle {
                    id: textSection
                    height: parent.height
                    width: parent.width - chkbox.width
                    anchors.left: chkbox.right
                    Text {
                        id: textShow
                        text: model.description
                        font.underline: true
                        font.bold: true
                        anchors.verticalCenter: parent.verticalCenter
                        visible: !dragRect.ListView.isCurrentItem
                        color: "black"
                    } //end textShow
                    TextInput {
                        id: textinput
                        anchors.verticalCenter: parent.verticalCenter
                        color: "blue"
                        text:  model.description
                        visible: dragRect.ListView.isCurrentItem
                        enabled: dragRect.ListView.isCurrentItem
                        focus: true
                        onFocusChanged: {
                            if(focus){
                                console.debug("got focus" + "textInput");
                            }
                        }
                        onEditingFinished: {
                            model.description = textinput.text
                            //方法1: 设置index
                            if(listView.currentIndex == index){
                                listView.currentIndex = -1;
                            }
                            console.log(("TextInput listview currentIndex: " + listView.currentIndex +  " index: " + index ))
                            console.log(("TextInput ListView isCurrentItem: " + dragRect.ListView.isCurrentItem))
                        }
                    } //end TextInput
                } //end textSection Rectangle
                MouseArea {
                    id: mouseArea
                    width: parent.width
                    height: parent.height
                    hoverEnabled: true
                    z: -1 //避免遮住checkbox
                    onClicked: {
                        //方法1: 设置当前
                        listView.currentIndex = index
                        console.log(("MouseArea listview currentIndex: " + listView.currentIndex +  " index: " + index ))
                        console.log(("MouseArea ListView isCurrentItem: " + dragRect.ListView.isCurrentItem))
                        // 在dragRect的 onFocusChanged 中设置了, 此处不需要了
                        //textinput.focus = true;
                    }
                }
            }
        } //end Row Rectangle
        ListView {
            id: listView
            Layout.fillWidth: true
            Layout.fillHeight: true
            keyNavigationEnabled: true
            //clip: true
            model: MyModel {}
            delegate: delegateItem
            //默认不要是第一个, 否则第一个是可编辑状态(针对方法1)
            Component.onCompleted : {
                currentIndex = -1;
            }
            //默认焦点
            focus: true
        }
    }
    RowLayout {
        Button {
            text: qsTr("Add New Item")
            Layout.fillWidth: true
            onClicked: {
                var c = listView.model.rowCount();
                listView.model.append({
                                          "description": "Buy a new book " + (c + 1),
                                          "done": false
                                      })
                //设置焦点, 否则listView就没焦点了
                listView.focus = true;
                listView.currentIndex = c;
            }
        }
    }
}
几个关键点, 详见代码:
- 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的更多相关文章
- OSG嵌入QT(QT界面使用Qt Designer编辑)
		
本文主要内容:使用Qt Designer编辑好QT界面后,将OSG中的ViewerWidget嵌入到QT的Widget中. 在VS中嵌入QT工具,建立QT GUIApplication后,打开自动生成 ...
 - QT creator 编辑多个UI 文件 出现 无法解析的外部符号的错误
		
创建一般的Qt Gui 程序一般会默认一个UI 文件 ,但是随着应用程序窗口的增多,同时编辑多个UI 界面是必须的. 假设我们已经创建好了一个QTUI的工程,里面已经默认了一个UI文件,但是想在添几个 ...
 - ubuntu系统中Qt creator 编辑和应用使用中文输入法
		
在ubuntu系统的GUI开发过程中遇到在编辑器里面不能使用中文输入法,前提我已经安装了搜狗输入法,但是还是不能使用,原因是QT的库里没有最新fcix的库,. 没有安装搜狗的输入法的 https:// ...
 - Qt - QLineEdit编辑框
		
QLineEdit输入内容获取及合理性检查? 控件自带触发信息: void textChanged(const QString &);void textEdited(const QString ...
 - QT 下把编辑框内的中文字符转换为 char*
		
第一种方法 QString str(tr("你好")); wchar_t wc[100] = {0}; pEditShortDes->text().toWCharArray( ...
 - QT-可拖拽可编辑的多控件ListView
		
目标 结合前面的2篇文章, 继续升级QML版本的ListView: 又要拖拽, 又要可编辑, 还得支持多个控件. 循序渐进 本文基于前一篇的基础: Qt-可编辑的ListView 要循序渐进的学习. ...
 - Qt, 我回来了。。。
		
说起qt,大学时就有接触,但一直没有深入,这个周六周天利用两于时间重新温习了一下,跟之前用过的vs上的MFC.C++ builder比起来,Qt封装很人性化,库也比较全,写个 一般的小工具很轻松. 参 ...
 - 简单QT应用了可实现手动布局QT应用
		
 新建QT项目 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcXVhbg==/font/5a6L5L2T/fontsize/4 ...
 - Qt入门之基础篇 ( 二 ) :Qt项目建立、编译、运行和发布过程解析
		
转载请注明出处:CN_Simo. 题解: 本篇内容主讲Qt应用从创建到发布的整个过程,旨在帮助读者能够快速走进Qt的世界. 本来计划是讲解Qt源码静态编译,如此的话读者可能并不能清楚地知道为何要静态编 ...
 
随机推荐
- Telnet查看端口是否通
			
1. 查看端口是否通畅 telnet IP 端口号 如:telnet 10.150.159.71 5516 2.查看本机是否开启某个端口:netstat -an |findstr "端口 ...
 - Jenkins 流水线远程部署 .NET Core/Framework 到 IIS
			
目录 Windows 安装 Git WebDeploy Windows 从节点 .NET Core 处理 IIS 处理项目 Jenkinsfile .NET Framework 安装环境 .NET F ...
 - 文氏电桥振荡电路原理详解及Multisim实例仿真
			
文氏电桥振荡电路(Wien bridge oscillator circuit),简称"文氏电桥",是一种适于产生正弦波信号的振荡电路之一,此电路振荡稳定且输出波形良好,在较宽的频 ...
 - ffmpeg入门到实战-ffmpeg是怎么转码的?
			
阅读目录 视频是怎么被播放的? ffmpeg命令的格式 ffmpeg转码输出的过程 视频是怎么被播放的? 我们知道,当下大多数播放器都是基于ffmpeg二次开发的.你有没有想过,你用播放器打开一个视频 ...
 - Linux中ls的用法
			
在linux系统中,可以说一切皆文件.文件类型包含:普通文件,目录,字符设备文件,块设备文件,符号链接文件等 我们可以用file这个命令来查看文件的属性: 这里可以看到1.sh是个脚本文件 下面开始介 ...
 - hive学习笔记之十一:UDTF
			
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
 - MPP大规模并行处理架构详解
			
面试官:说下你知道的MPP架构的计算引擎? 这个问题不少小伙伴在面试时都遇到过,因为对MPP这个概念了解较少,不少人都卡壳了,但是我们常用的大数据计算引擎有很多都是MPP架构的,像我们熟悉的Impal ...
 - Raspberry Pi:树莓派开发板配置USB启动系统
			
准备材料 树莓派4B U盘 TF卡 树莓派基础镜像2020-08-20稳定版(这个系统是必须的并拷录在TF卡) Kali树莓派系统(这个是我想要学习的系统,大家可以准备自己的系统,拷录在U盘的) SD ...
 - Android java层常见加密算法的hook自吐以及栈信息的打印
			
杂谈:其实原理并没有很难,本质就是hook Android的框架层中的api将我们想要的key和iv(也可以没有,就打个比方),但是目前的话,很多厂家已经不在直接调用java层的这些加密算法的api了 ...
 - Java实验项目三——平面图形和立体图形抽象类
			
Program:按照下面要求完成类的设计 (1)设计一个平面图形抽象类和一个立体图形抽象类,提供该类对象公共的方法和属性. (2)修改项目三中第2题中所设计的球类.圆柱类,圆锥类.矩形类.三角形类.圆 ...