QListView自定义item(结合QSqlQueryModel)

QListView:绘制自定义List(一)------设置ItemDelegate_qt_繁星执着-开放原子开发者工作坊 (csdn.net)

QListView自定义Item_qlistview 自定义item-CSDN博客

结合我写的上一篇文章:

QTableView与QSqlQueryModel的简单使用-CSDN博客

这次尝试让QListView与QSqlQueryModel结合使用。

但经过尝试,我觉得它们不适合放在一起使用,

因为感觉不是很好用。

QSqlQueryModel的基类是QAbstractTableModel

所以其实QSqlQueryModel更适合与QTableView结合起来使用。

QListView实际上是只有一列的QTableView。

下面是实践的源码:

cpp 复制代码
#pragma once

#include <QStyledItemDelegate>
#include <qmetatype.h>

class QListView;
class WItemDelegate  : public QStyledItemDelegate
{
	Q_OBJECT

public:
	WItemDelegate(QListView *parent = nullptr);
	~WItemDelegate();
    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
    QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override;
};
cpp 复制代码
#include "WItemDelegate.h"
#include "qlistview.h"
#include "qpainter.h"
#include "qsqlquerymodel.h"
#include <qdebug.h>
WItemDelegate::WItemDelegate(QListView*parent)
	: QStyledItemDelegate(parent)
{}

WItemDelegate::~WItemDelegate()
{}
QSize WItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
	return QSize(option.rect.width(), 60);
}
void WItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    if (index.isValid()) {
        int row=index.row();
        QSqlQueryModel* Model = (QSqlQueryModel*)index.model();
        QVariant rowid = index.data(Qt::DisplayRole);
        QVariant col1 = Model->index(row, 1).data(Qt::DisplayRole);
        QVariant col2 = Model->index(row, 2).data(Qt::DisplayRole);
        QRect rect = option.rect;
        qDebug() << rect;
        int wx = 5;
        int wy = 5;
        rect.setX(rect.x() + wx);
        rect.setY(rect.y() + wy);
        rect.setWidth(rect.width() - wx * 2);
        rect.setHeight(rect.height() - wy * 2);
        qDebug() << rect;
        QString text = QString("rowid:%1,col1:%2,col2:%3").arg(rowid.toInt()).arg(col1.toInt()).arg(col2.toInt());
        painter->setBrush(QColor(0, 0, 0));
        painter->drawRoundedRect(rect, 5, 5);
        painter->setPen(QColor(255, 255, 255));
        painter->drawText(rect, text,QTextOption(Qt::AlignCenter));
    }
}
cpp 复制代码
#include "SqlQueryModel.h"
#include "qfiledialog.h"
#include "qsqldatabase.h"
#include "qsqlerror.h"
#include "qsqlrecord.h"
#include "qmessagebox.h"
#include "qapplication.h"
#include "qboxlayout.h"
#include "WItemDelegate.h"
#include "Config/config.h"
SqlQueryModel::SqlQueryModel(QWidget *parent)
	: QMainWindow(parent)
{
    QString aFile = QString::fromLocal8Bit("E:/桌面/3.db");
    if (aFile.isEmpty())
        return;

    //打开数据库
    DB = QSqlDatabase::addDatabase("QSQLITE"); //添加 SQL LITE数据库驱动
    DB.setDatabaseName(aFile); //设置数据库名称
    if (!DB.open())   //打开数据库
    {
        QMessageBox::warning(this, "错误", "打开数据库失败",
            QMessageBox::Ok, QMessageBox::NoButton);
        return;
    }
    qryModel = new QSqlQueryModel(this);
    qryModel->setQuery("select rowid,col1,col2 from test;");
    if (qryModel->lastError().isValid())
    {
        QMessageBox::critical(this, "错误", "数据表查询错误,错误信息\n" + qryModel->lastError().text(),
            QMessageBox::Ok, QMessageBox::NoButton);
        return;
    }

    listView = new QListView;
    listView->setModel(qryModel);
    WItemDelegate* wItemDelegate = new WItemDelegate(listView);
    listView->setItemDelegate(wItemDelegate);
    listView->show();
}

在获取数据的时候,有点别扭:

index是第row行第0列的索引

根据index得到row和model

然后得到第row行第1列和第2列的索引

cpp 复制代码
        int row=index.row();
        QSqlQueryModel* Model = (QSqlQueryModel*)index.model();
        QVariant rowid = index.data(Qt::DisplayRole);
        QVariant col1 = Model->index(row, 1).data(Qt::DisplayRole);
        QVariant col2 = Model->index(row, 2).data(Qt::DisplayRole);

效果图:

相关推荐
灯澜忆梦8 小时前
GO_并发编程---定时器
开发语言·后端·golang
-银雾鸢尾-8 小时前
C#中的StringBuilder相关方法
开发语言·c#
-银雾鸢尾-9 小时前
C#中结构体与类的区别;抽象类与接口的区别
开发语言·c#
大模型码小白10 小时前
【Python零基础教程】继承、多态与魔法函数:面向对象编程三大核心特性详解
java·大数据·开发语言·人工智能·python·ai编程
段一凡-华北理工大学12 小时前
向量数据库实战:选型、调优与落地~系列文章12:文本分块策略实战:chunk_size 怎么选?重叠多少?
开发语言·数据库·后端·oracle·rust·工业智能体·高炉智能化
Ljwuhe12 小时前
C++——多态
开发语言·c++
心平气和量大福大12 小时前
C#-WPF-Window主窗体
开发语言·c#·wpf
从零开始的代码生活_14 小时前
C++ 继承详解:访问控制、对象模型、菱形继承与设计取舍
开发语言·c++·后端·学习·算法
云小逸14 小时前
【C++ 第七阶段:模板、泛型编程与工程综合详解】
开发语言·c++
Quz14 小时前
QML 信号与槽:直接绑定与跨文件通信
qt·编程语言·设计