Qt QListView详解

Qt QListView详解一 常用接口 1 添加数据 QStringListM model model newQStringLi this model gt setStringLis QStringList lt lt 语文 lt lt 数学 lt lt 英语 ui gt listView gt setModel model 2 使用自定义的 widget 这里使用 QLineEdit 来举例 可以使用自定义的 w

一.常用接口

1.添加数据

 QStringListModel *model; model = new QStringListModel(this); model->setStringList(QStringList()<<"语文"<<"数学"<<"英语"); ui->listView->setModel(model);

2.使用自定义的widget

这里使用QLineEdit来举例,可以使用自定义的widget,图如下,可以看到第二行变成了行编辑框。

 QLineEdit *lineEdit = new QLineEdit(ui->listView); lineEdit->setText("数学"); ui->listView->setIndexWidget(index,lineEdit);

Qt QListView详解

3.设置默认哪一行选中

QModelIndex index2 = model->index(2); ui->listView->setCurrentIndex(index2);

二.信号

以下信号经常使用

 void activated(const QModelIndex &index) void clicked(const QModelIndex &index) void doubleClicked(const QModelIndex &index) void entered(const QModelIndex &index) void pressed(const QModelIndex &index) 

简单相应点击的信号,点击后打印text

void Widget::on_listView_clicked(const QModelIndex &index) { qDebug()< 
  
    data(index).toString(); } 
  

三.右键弹出菜单

 //打开右键菜单属性 ui->listView->setContextMenuPolicy(Qt::CustomContextMenu); //右键菜单 menu = new QMenu(ui->listView); menu->addAction("添加"); menu->addAction("删除"); //响应右键菜单信号槽 connect(ui->listView,&QListView::customContextMenuRequested, this,&Widget::slotCustomContextMenuRequested); void Widget::slotCustomContextMenuRequested(const QPoint &pos) { menu->exec(ui->listView->mapToGlobal(pos)); } 

四.设置QSS

 QListView { show-decoration-selected: 1; /* make the selection span the entire width of the view */ } QListView::item:alternate { background: #EEEEEE; } QListView::item:selected { border: 1px solid #6a6ea9; } QListView::item:selected:!active { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #ABAFE5, stop: 1 #8588B2); } QListView::item:selected:active { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #6a6ea9, stop: 1 #888dd9); } QListView::item:hover { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #FAFBFE, stop: 1 #DCDEF1); } //适用于开启交替颜色 QListWidget#listWidget { alternate-background-color:blue;background:yellow } 

调用setStyleSheet即可

五.源码

QListView详解-C++文档类资源-CSDN下载

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/217137.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 上午10:15
下一篇 2026年3月18日 上午10:16


相关推荐

  • C语言 u16_c语言自定义数据类型

    C语言 u16_c语言自定义数据类型stdint.h这里放着C语言的标准表达方式typedefsignedcharint8_t;typedefsignedshortintint16_t;typedefsignedintint32_t;typedefsigned__int64int64_t;typedefun…

    2022年10月15日
    5
  • java模板引擎哪个好_模板引擎比较

    java模板引擎哪个好_模板引擎比较18 9 3 总结 1 FreeMarker 是一个用 Java 语言编写的模板引擎 它基于模板来生成文本输出 FreeMarker 与 Web 容器无关 即在 Web 运行时 它并不知道 Servlet 或 HTTP 它不仅可以用作表现层的实现技术 而且还可以用于生成 XML JSP 或 Java 等 在 spring4 0 中推荐使用 thymeleaf 来做前端模版引擎 2 JSP 技术 springboot 官方是不推荐的 原因

    2026年3月17日
    2
  • Debussy简介

    Debussy简介Debussy 是 NOVASSoftwar Inc 思源科技 发展的 HDLDebug amp Analysistool 这套软体主要不是用来跑模拟或看波形 它最强大的功能是 能够在 HDLsourcecod schematicdia waveform statebubbled 之间 即时做 trace 协助工程师 debug Debussy 软件在 5 4 版本以后不再有 Win

    2026年3月18日
    2
  • 告别“重绘地狱”:基于法线重构与 2D 光照系统的全动态环境光影工业级工作流

    告别“重绘地狱”:基于法线重构与 2D 光照系统的全动态环境光影工业级工作流

    2026年3月16日
    2
  • c++ offsetof_函数offset的用法

    c++ offsetof_函数offset的用法宏offsetof标准库stddef.h定义size_toffsetof(type,member);分析C库宏offsetof(type,member)会生成一个类型为size_t的整型常量,它是一个结构成员(member)相对于结构(type)开头的字节偏移量。成员是由member给定的,结构的名称是在type中给定的。type–这是一个参数class类型,是结…

    2022年8月22日
    11
  • 什么是SSL数字证书

    什么是SSL数字证书什么是 SSL 数字证书 SSL 数字证书是 CA 机构颁发给域名或者 IP 地址的数字证书 为客户端与服务器端建立 SSL 加密通道 二 SSL 证书如何工作 基本原则是 当您在服务器上安装 SSL 证书 并且浏览器连接到该证书时 SSL 证书的存在会触发 SSL 或 TLS 协议 该协议将加密服务器和浏览器 或服务器之间 之间发送的信息 详细信息显然要复杂一些 SSL 直接在传输控制协议 TCP 之上运行 有效地起到了一个安全保护层的作用 它允许更高的协议层保持不变 同时仍然提供安全连接 因此 在 SSL 层下面 其他协议层能

    2026年3月19日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号