Qt ListView使用

本文介绍了Qt中ListView加载数据的两种方法,包括直接在qml文件中定义model和通过C++自定义Model。重点讲解了自定义QAbstractListModel的子类,实现rowCount、data和roleNames方法,并通过setContextProperty将Model暴露给qml领域。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

Qt中ListView加载数据一般有两种方式,一种是直接qml文件中model直接定义并且放置数据,一种是C++代码中定义Model,再setContextProperty的方式暴露给qml域。
官方介绍

步骤

(1)qml界面

import QtQuick 2.0
import QtQuick.Controls 1.5
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.3
SubPageForm {
    title:"切换线路"
    property int font_mid_size:24
    bodyArea:  Rectangle {
        id: subroot
        color: "#00ffffff"
        Image {
            id: tablebg
            source: "../Resources/tablebg.jpg"
            anchors.left: parent.left
            anchors.leftMargin: 50
            anchors.right: parent.right
            anchors.rightMargin: 50
            anchors.top: parent.top
            anchors.topMargin: 30
            ListView {
                id: datalist
                anchors.fill: parent
                interactive:false
                header:Rectangle{
                    width: datalist.width
                    height: 53
                    color:Qt.rgba(0,0,0,0)
                    RowLayout{
                        anchors.left: parent.left
                        anchors.right: parent.right
                        height: parent.height

                        Text{
                            text:"序号"
                            color:"white"
                            font.pixelSize:font_mid_size
                            horizontalAlignment: Text.AlignHCenter
                            Layout.fillWidth:true
                        }

                        Text{
                            text:"线路"
                            color:"white"
                            font.pixelSize:font_mid_size
                            horizontalAlignment: Text.AlignHCenter
                            Layout.fillWidth:true
                        }

                        Text{
                            text:"方向"
                            color:"white"
                            font.pixelSize:font_mid_size
                            horizontalAlignment: Text.AlignHCenter
                            Layout.fillWidth:true
                        }
                    }
                }
                delegate:Rectangle{
                    width: datalist.width
             
### 如何自定义和美化 Qt ListView 组件 为了实现更美观的 `Qt ListView` 外观,可以采用多种方法来定制其样式。以下是几种常见的技术: #### 使用 QSS (Qt Style Sheets) QSS 提供了一种类似于 CSS 的方式来自定义控件外观。通过设置不同的属性,能够改变列表项的颜色、字体大小以及其他视觉效果。 ```css /* 设置整个ListView背景颜色 */ QListView { background-color: white; } /* 当鼠标悬停时更改项目前景色 */ QListView::item:hover { color: blue; } /* 被选中的项目的高亮显示 */ QListView::item:selected { background-color: lightblue; } ``` 这些样式可以直接应用到应用程序中[^1]。 #### 自定义委托绘制 对于更加复杂的界面需求,则可以通过创建子类化 `QStyledItemDelegate` 来控制每一行的具体渲染逻辑。这允许开发者完全掌控每一个单元格的内容呈现形式,比如嵌入图片或图标等多媒体元素。 ```cpp class CustomDelegate : public QStyledItemDelegate { protected: void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override { // 定制绘画代码... } }; // 应用代理对象至视图实例 listView->setItemDelegate(new CustomDelegate(listView)); ``` 这种方法提供了极大的灵活性,适用于需要高度个性化展示的应用场景。 #### 利用模型/视图架构的优势 利用好 Qt 的 MVC 架构模式也可以间接影响 `ListView` 的表现力。例如,在数据源端调整返回给前端的数据结构(如加入额外的状态字段),再配合上述提到的委托机制一起工作,便能轻松实现出动态变化的效果。 ```cpp QStringListModel* model = new QStringListModel(); model->setStringList(QStringList() << "Item 1" << "Item 2"); listView->setModel(model); ``` 以上就是一些基本思路和技术手段用于提升 `Qt ListView` 控件的表现质量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值