QListView自定义item(结合QSqlQueryModel)

news/2024/7/7 18:38:28 标签: qt, 开发语言

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

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

结合我写的上一篇文章:

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

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

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

因为感觉不是很好用。

QSqlQueryModel的基类是QAbstractTableModel

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

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

下面是实践的源码:

#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;
};
#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));
    }
}
#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列的索引

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

 效果图:


http://www.niftyadmin.cn/n/5534855.html

相关文章

OpenCV——实现裁剪YOLO格式的图片目标并按图片名保存

import os import cv2def crop_image(image_path, label_path, output_folder):# 读取图片img cv2.imread(image_path)height, width, _ img.shape# 读取标签文件with open(label_path, r) as file:labels file.readlines()img_id 1# 遍历每个标签for label in labels:part…

Matplotlib 线条的样式

标记型 特点 描述 . 点标记 , 像素标记 o 圆圈标记 v triangle_down 标记 ^ triangle_up 标记 < triangle_left 标记 > triangle_right 标记 1 tri_down 标记 2 tri_up 标记 3 三左标记 4 tri_right 标记 8 八角形标记 s 方形标记 p 五边形…

word 转pdf 中图片不被压缩的方法

word 转pdf 中图片不被压缩的方法 法1&#xff1a; 调节word 选项中的图片格式为不压缩、高保真 法2&#xff1a; 1: word 中的图片尽可能使用高的分辨率&#xff0c;图片存为pnd或者 tif 格式&#xff08;最高清&#xff09; 2: 转化为pdf使用打印机器&#xff0c;参数如下…

【ubuntu】切换shell并显示git分支名字

y9kp 显示当前shell echo $SHELLwhich bash根据输出&#xff0c;例如 /bin/bash 改变shell&#xff1a; chsh -s /bin/bash退出重新登录 加入函数及覆盖PS1 # Function to return the current Git branch name git_branch() {# Check if the current directory is in a Git …

EtherCAT通讯介绍

一、EtherCAT简介 EtherCAT&#xff08;Ethernet for Control Automation Technology&#xff09;是一种实时以太网技术&#xff0c;是由德国公司Beckhoff Automation在2003年首次推出的。它是一种开放的工业以太网标准&#xff0c;被设计用于满足工业自动化应用中的高性能和低…

WSL——忘记root密码(Ubuntu)

1、问题描述 Windows下的WSL&#xff08;Ubuntu&#xff09;忘记了root密码&#xff0c;无法使用管理员权限。 2、解决方法 关闭 Ubuntu 窗口。打开 Windows 的 Powershell 或 cmd&#xff0c; 以 root 默认登陆 WSL。 wsl -u root 修改对应用户密码。 # xxx为要修改密码的用…

【Django】网上蛋糕项目商城-首页

概念 本文在上一文章搭建完数据库&#xff0c;以及创建好项目之后&#xff0c;以及前端静态文件后&#xff0c;对项目的首页功能开发。 后端代码编写 在views.py文件中创建方法&#xff0c;连接数据库&#xff0c;并获取首页需要的数据 def getGoodsList(type):# 获取所有横…

【linux/shell】shell中使用for循环读取数据

目录 一.for循环从列表中读取数据的几种形式 二.for循环从配置文件读取数据 三.for循环用通配符读取目录 四.for循环带有数字变量 一.for循环从列表中读取数据的几种形式 #!/bin/bash listl"aa bb cc" list2aa bb ccfor i in $list3 doecho $i done 使用这种形…