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);
效果图: