QTableView的一行里添加两个按钮

devtools/2024/9/23 10:16:48/

我是光明正大地抄,作者说的欢迎转载
作者:李鹏
出处:http://www.cnblogs.com/li-peng/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

链接:https://pan.xunlei.com/s/VO540QHTSoXJtFfL5J3xxtT6A1?pwd=krff#
复制这段内容后打开手机迅雷App,查看更方便

看一下列的效果
在这里插入图片描述
看一下添加两个按钮的效果:
点击第一个按钮弹出 but1 +当前列
在这里插入图片描述

点击第二个按钮弹出but2 + 当前行
在这里插入图片描述
下面是主要实现

继承自 QItemDelegate

主要是实现 了它的painter方法,把两个自定义的按钮绘制到视图并保存

还有editorEvent事件,用来处理点击事件,在点击时我们算一下鼠标的坐标在哪个按钮下,

再处理相应的点击事件


#ifndef BUTTONDELEGATE_H
#define BUTTONDELEGATE_H#include <QItemDelegate>class ButtonDelegate : public QItemDelegate
{Q_OBJECT
public:explicit ButtonDelegate(QObject *parent = 0);void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);signals:public slots:private:void showMsg(QString str);private:typedef QMap<QModelIndex, QPair<QStyleOptionButton*, QStyleOptionButton*>* >  collButtons;collButtons m_btns;};#endif // BUTTONDELEGATE_H

按钮的具体实现

#include "buttondelegate.h"#include <QApplication>
#include <QMouseEvent>
#include <QMessageBox>
#include <QPainter>
#include <QStyleOption>
#include <QDesktopWidget>ButtonDelegate::ButtonDelegate(QObject *parent) :QItemDelegate(parent)
{
}void ButtonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{QPair<QStyleOptionButton*, QStyleOptionButton*>* buttons = m_btns.value(index);if (!buttons) {QStyleOptionButton* button1 = new QStyleOptionButton();//button1->rect = option.rect.adjusted(4, 4, -(option.rect.width() / 2 + 4) , -4); //button1->text = "X";button1->state |= QStyle::State_Enabled;QStyleOptionButton* button2 = new QStyleOptionButton();//button2->rect = option.rect.adjusted(button1->rect.width() + 4, 4, -4, -4);button2->text = "Y";button2->state |= QStyle::State_Enabled;buttons =new  QPair<QStyleOptionButton*, QStyleOptionButton*>(button1, button2);(const_cast<ButtonDelegate *>(this))->m_btns.insert(index, buttons);}buttons->first->rect = option.rect.adjusted(4, 4, -(option.rect.width() / 2 + 4) , -4); //buttons->second->rect = option.rect.adjusted(buttons->first->rect.width() + 4, 4, -4, -4);painter->save();if (option.state & QStyle::State_Selected) {painter->fillRect(option.rect, option.palette.highlight());}painter->restore();QApplication::style()->drawControl(QStyle::CE_PushButton, buttons->first, painter);QApplication::style()->drawControl(QStyle::CE_PushButton, buttons->second, painter);
}bool ButtonDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{if (event->type() == QEvent::MouseButtonPress) {QMouseEvent* e =(QMouseEvent*)event;if (m_btns.contains(index)) {QPair<QStyleOptionButton*, QStyleOptionButton*>* btns = m_btns.value(index);if (btns->first->rect.contains(e->x(), e->y())) {btns->first->state |= QStyle::State_Sunken;}else if(btns->second->rect.contains(e->x(), e->y())) {btns->second->state |= QStyle::State_Sunken;}}}if (event->type() == QEvent::MouseButtonRelease) {QMouseEvent* e =(QMouseEvent*)event;if (m_btns.contains(index)) {QPair<QStyleOptionButton*, QStyleOptionButton*>* btns = m_btns.value(index);if (btns->first->rect.contains(e->x(), e->y())) {btns->first->state &= (~QStyle::State_Sunken);showMsg(tr("btn1 column %1").arg(index.column()));} else if(btns->second->rect.contains(e->x(), e->y())) {btns->second->state &= (~QStyle::State_Sunken);showMsg(tr("btn2 row %1").arg(index.row()));}}}
}void ButtonDelegate::showMsg(QString str)
{QMessageBox msg;msg.setText(str);msg.exec();
}

好了自定义按钮处理完了

我们建一个Table添加一些数据

#ifndef TABLEMODEL_H
#define TABLEMODEL_H#include <QAbstractTableModel>class TableModel : public QAbstractTableModel
{Q_OBJECT
public:explicit TableModel(QObject *parent = 0);int rowCount(const QModelIndex &parent) const;int columnCount(const QModelIndex &parent) const;QVariant data(const QModelIndex &index, int role) const;Qt::ItemFlags flags(const QModelIndex &index) const;void setHorizontalHeader(const QStringList& headers);QVariant headerData(int section, Qt::Orientation orientation, int role) const;void setData(const QVector<QStringList>& data);QVector<QStringList>& DataVector() {return m_data;}~TableModel(void);signals:public slots:private:QStringList m_HorizontalHeader;QVector<QStringList> m_data;
};#endif // TABLEMODEL_H

model的实现 并添加一些数据

#include "tablemodel.h"TableModel::TableModel(QObject *parent) :QAbstractTableModel(parent)
{
}TableModel::~TableModel()
{}int TableModel::rowCount(const QModelIndex &parent) const
{return m_data.size();
}int TableModel::columnCount(const QModelIndex &parent) const
{return m_HorizontalHeader.count();
}QVariant TableModel::data(const QModelIndex &index, int role) const
{if (!index.isValid())return QVariant();if (role == Qt::DisplayRole) {int ncol = index.column();int nrow =  index.row();QStringList values = m_data.at(nrow);if (values.size() > ncol)return values.at(ncol);elsereturn QVariant();}return QVariant();
}Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
{if (!index.isValid())return Qt::NoItemFlags;Qt::ItemFlags flag = QAbstractItemModel::flags(index);// flag|=Qt::ItemIsEditable // 设置单元格可编辑,此处注释,单元格无法被编辑return flag;
}void TableModel::setHorizontalHeader(const QStringList &headers)
{m_HorizontalHeader =  headers;
}QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
{if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {return m_HorizontalHeader.at(section);}return QAbstractTableModel::headerData(section, orientation, role);
}void TableModel::setData(const QVector<QStringList> &data)
{m_data = data;
}

TableView的实现,和model关联

#ifndef TABLEVIEW_H
#define TABLEVIEW_H#include <QTableView>
#include "tablemodel.h"
#include "buttondelegate.h"class TableView : public QTableView
{Q_OBJECT
public:explicit TableView(QWidget *parent = 0);TableModel* tableModel() {return m_model;}~TableView();signals:public slots:private:void iniData();private:TableModel *m_model;ButtonDelegate *m_buttonDelegate;};#endif // TABLEVIEW_H
#include "tableview.h"#include "tablemodel.h"
#include "buttondelegate.h"TableView::TableView(QWidget *parent) :QTableView(parent)
{iniData();
}TableView::~TableView()
{delete m_model;
}void TableView::iniData()
{m_model = new TableModel();this->setModel(m_model);QStringList headers;headers << "Id" << "Progress";m_model->setHorizontalHeader(headers);QVector<QStringList> data;data.append(QStringList() << "1" << "22");data.append(QStringList() << "2" << "32");data.append(QStringList() << "3" << "2");data.append(QStringList() << "4" << "80");data.append(QStringList() << "5" << "40");m_model->setData(data);m_buttonDelegate = new ButtonDelegate(this);this->setItemDelegateForColumn(1, m_buttonDelegate);emit m_model->layoutChanged();this->setColumnWidth(1, 500);
}

这就完成了

我们看一下调用

this->resize(800, 600);TableView *tv = new TableView(this);QVBoxLayout* layout = new QVBoxLayout();layout->addWidget(tv);this->setLayout(layout);

http://www.ppmy.cn/devtools/99883.html

相关文章

天猫商品评论API:获取商品热门评价与最新评价

天猫&#xff08;Tmall&#xff09;作为中国最大的B2C电商平台之一&#xff0c;提供了丰富的商品和服务。然而&#xff0c;天猫并没有直接公开一个通用的API来允许第三方开发者直接获取商品的热门评价或最新评价。这主要是出于保护用户隐私、防止恶意抓取和滥用数据等考虑。 不…

使用Python恢复Windows、Linux、MacOS回收站中的文件和目录

一、使用Python恢复Windows回收站中的文件和目录 import os import platformdef put_back_trash():# 获取操作系统类型os_type platform.system()if os_type "Windows": # Windowsfrom winshell import recycle_binfor item in recycle_bin():winshell.undelete(…

SQL 优化慢的update语句

原语句 UPDATE tbl_mix_traceSET mix_plan_id ( SELECT p.id FROM tbl_mix_plan p WHERE p.lot_id_out lot_id_in ),pro_date_in (SELECT p.pro_date_out FROM tbl_mix_plan p WHERE p.lot_id_out lot_id_in),shift_id_in (SELECT p.shift_id_out FROM tbl_mix_plan p WHER…

Paxos算法概述:从Basic Paxos到Fast Paxos及在Zookeeper中的应用

目录 Basic Paxos算法概述 Fast Paxos算法概述 Fast Paxos相对于Basic Paxos的改进 在Zookeeper中的应用 Basic Paxos算法概述 Basic Paxos算法是一种经典的分布式一致性算法&#xff0c;用于解决分布式系统中的一致性问题。它主要用于确保在一个分布式系统中&#xff0c;…

Web前端性能优化合集

简介 自互联网兴起以来&#xff0c;从最初的静态网页到如今的动态交互、单页应用&#xff08;SPA&#xff09;、PWA&#xff08;Progressive Web Apps&#xff09;等&#xff0c;互联网技术正在飞速发展&#xff0c;随着用户体验成为核心竞争力之一&#xff0c;前端性能直接影…

8.21-部署eleme项目

1.设置主从从mysql57服务器 &#xff08;1&#xff09;配置主数据库 [rootmsater_5 ~]# systemctl stop firewalld[rootmsater_5 ~]# setenforce 0[rootmsater_5 ~]# systemctl disable firewalldRemoved symlink /etc/systemd/system/multi-user.target.wants/firewalld.serv…

【功能实现】Vue3中导航栏效果实现

功能需求&#xff1a; 点击导航后会跳转到相应的页面当页面在顶部时&#xff0c;导航栏模块背景是透明的当页面向下滚动超过150px时&#xff0c;导航栏背景为白色 效果实现&#xff1a; 1.准备列表 将导航栏区域的类名设置为动态&#xff0c;即通过判断isTransparent是否存在…

树莓派结合ZigBee通讯实现宿舍安全管理系统:Flask、React Native

一、项目概述 随着智能技术的发展和物联网&#xff08;IoT&#xff09;技术的广泛应用&#xff0c;高校宿舍的安全和生活智能化需求日益迫切。我们设计的智能校园宿舍系统&#xff0c;旨在通过实时监测学生宿舍的用电安全、实施火灾应急措施和实现日常物件的智能化管理&#x…