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);

效果图:

相关推荐
lly2024067 分钟前
ASP Folder:深入解析其功能与使用技巧
开发语言
雪域迷影38 分钟前
Go语言中通过get请求获取api.open-meteo.com网站的天气数据
开发语言·后端·http·golang·get
ysdysyn3 小时前
C# 进程管理实战:检查与启动EXE程序的完整指南
开发语言·c#
IDOlaoluo3 小时前
PHP-5.2.1.tar.gz 离线安装教程:从源码编译到配置的详细步骤(附安装包)
开发语言·php
wangjialelele3 小时前
Qt中的常用组件:QWidget篇
开发语言·前端·c++·qt
爱上妖精的尾巴4 小时前
5-26 WPS JS宏数组元素添加删除应用
开发语言·前端·javascript·wps·js宏
_OP_CHEN4 小时前
C++进阶:(三)深度解析二叉搜索树原理及实现
开发语言·数据结构·c++·二叉树·二叉搜索树·键值对
wxxka4 小时前
git使用
开发语言·git
花北城5 小时前
【C#】List快速检查重复数据
开发语言·c#
练习时长一年5 小时前
Jdk反射优化
java·开发语言