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

效果图:

相关推荐
侃侃_天下3 天前
最终的信号类
开发语言·c++·算法
echoarts3 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix3 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题3 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说3 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔3 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
我是菜鸟0713号3 天前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_3 天前
QT(4)
开发语言·汇编·c++·qt·算法
Brookty3 天前
【JavaEE】线程安全-内存可见性、指令全排序
java·开发语言·后端·java-ee·线程安全·内存可见性·指令重排序
百锦再3 天前
[特殊字符] Python在CentOS系统执行深度指南
开发语言·python·plotly·django·centos·virtualenv·pygame