之前在项目上需要在表格中加入Button是按照以下两个文章的做法:

http://www.cnblogs.com/li-peng/p/3961843.html

http://www.cnblogs.com/li-peng/p/4029885.html

文章的做法是传统通过子类化QItemDelegate类来做的,通过paint函数在某列中画出QPushButton的Style。

但是这么做有一个问题,就是按钮画出来以后,但是拖动QTableView的滚动条的时候,会导致按钮所在的列的部分按钮又消失的问题。

没查到原因,但是最终还是在找到解决方案了。还是子类化QItemDelegate类,但是与之前的两个文章之前的做法有些不同,以下是正确的

代码:

Delegate头文件:

 #pragma once

 #include <QStyledItemDelegate>
#include <QString>
#include <QPersistentModelIndex> class QStyleOptionButton;
class CTableWidget;
class QPushButton; class AppRepoButtonDelegate : public QStyledItemDelegate
{
Q_OBJECT public: explicit AppRepoButtonDelegate(QObject *parent = Q_NULLPTR);
~AppRepoButtonDelegate();
public:
void setText(const QString& text);
void setStyleSheet(const QString& qss); signals:
void buttonClicked(const QModelIndex& index);
public: QWidget* createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
private slots:
void cellEntered(const QModelIndex& index);
void slotBtnClicked();
private:
CTableWidget * m_table_view;
bool isOneCellInEditMode;
QPushButton* m_btn;
QPersistentModelIndex m_currentEditedCellIndex;
QString m_btnText;
QString m_btnQss;
};

Delegate类的CPP实现文件:

 #include "AppRepoButtonDelegate.h"

 #include <QStyleOptionButton>
#include <QPainter>
#include <QApplication>
#include <QMouseEvent>
#include <QStandardItemModel>
#include <QPushButton>
#include <QTableView> #include "CTableWidget.h" AppRepoButtonDelegate::AppRepoButtonDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
CTableWidget *tabView = qobject_cast<CTableWidget*>(parent);
if (tabView)
{
m_table_view = tabView;
m_btn = new QPushButton(QStringLiteral(""), m_table_view);
m_btn->hide();
m_table_view->setMouseTracking(true);
connect(m_table_view, SIGNAL(entered(QModelIndex)),
this, SLOT(cellEntered(QModelIndex)));
isOneCellInEditMode = false;
}
} AppRepoButtonDelegate::~AppRepoButtonDelegate()
{
} void AppRepoButtonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
int x, y, width, height;
x = option.rect.left() + option.rect.width() / - ;
y = option.rect.top() + ;
width = ;
height = ; m_btn->setGeometry(QRect(x,y,width,height));
m_btn->setText(m_btnText);
m_btn->setStyleSheet(m_btnQss);
if (option.state == QStyle::State_Selected)
painter->fillRect(option.rect, option.palette.highlight());
QPixmap map = QPixmap::grabWidget(m_btn);
painter->drawPixmap(x, y, map);
} QWidget* AppRepoButtonDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QPushButton * btn = new QPushButton(parent);
connect(btn, &QPushButton::clicked, this, &AppRepoButtonDelegate::slotBtnClicked);
btn->setText(m_btnText);
btn->setStyleSheet(m_btnQss);
return btn;
} void AppRepoButtonDelegate::cellEntered(const QModelIndex& index)
{
if (index.column() == || index.column() == )
{
if (isOneCellInEditMode)
{
m_table_view->closePersistentEditor(m_currentEditedCellIndex);
}
m_table_view->openPersistentEditor(index);
isOneCellInEditMode = true;
m_currentEditedCellIndex = index;
}
else {
if (isOneCellInEditMode)
{
isOneCellInEditMode = false;
m_table_view->closePersistentEditor(m_currentEditedCellIndex);
}
}
} void AppRepoButtonDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
int x, y, width, height;
x = option.rect.left() + option.rect.width() / - ;
y = option.rect.top() + ;
width = ;
height = ;
editor->setGeometry(QRect(x, y, width, height));
} void AppRepoButtonDelegate::setText(const QString& text)
{
m_btnText = text;
} void AppRepoButtonDelegate::setStyleSheet(const QString& qss)
{
m_btnQss = qss;
} void AppRepoButtonDelegate::slotBtnClicked()
{
emit buttonClicked(m_currentEditedCellIndex);
}

用法:

  auto delegateBtn = new AppRepoButtonDelegate(ui->appTable);
delegateBtn->setText(QStringLiteral("下载更新"));
m_downloadUpdateDelegate = delegateBtn;
connect(m_downloadUpdateDelegate, &AppRepoButtonDelegate::buttonClicked,
this, &AppRepoPage::slotDownloadUpdateBtnClicked);
ui->appTable->setItemDelegateForColumn(
, m_downloadUpdateDelegate); //给第6列添加下载更新按钮的委托

references:

https://stackoverflow.com/questions/12360111/qpushbutton-in-qtableview
https://stackoverflow.com/questions/25337740/how-to-add-qpushbutton-in-qtableview-when-loading-database-by-c
http://www.qtcentre.org/threads/32394-Add-QPushButton-to-cell-in-QTableView
https://forum.qt.io/topic/53467/how-to-add-qpushbutton-in-qtableview

https://stackoverflow.com/questions/43082419/create-pushbuttons-in-qtableview-with-qstyleditemdelegate-subclass/43109044#43109044

https://qtadventures.wordpress.com/2012/02/04/adding-button-to-qviewtable/

在QTableView中某列中添加Button的导致滚动条滚动的时候消失的问题的更多相关文章

  1. Pandas中查看列中数据的种类及个数

    Pandas中查看列中数据的种类及个数 读取数据 import pandas as pd import numpy as np filepath = 'your_file_path.csv' data ...

  2. excel中统计列中的值在其他列出现的次数多个条件

    excel中统计列中的值在其他列出现的次数多个条件 =COUNTIFS(E2:E373,"=VIP经销商",J2:J373,K2) 解释 E列的第二行到第373行中值 等于 VIP ...

  3. python – 基于pandas中的列中的值从DataFrame中选择行

    如何从基于pandas中某些列的值的DataFrame中选择行?在SQL中我将使用: select * from table where colume_name = some_value. 我试图看看 ...

  4. c# richTextBox1添加内容并将滚动条滚动到当前焦点处

    1.   StringBuilder sb = new StringBuilder(); StringBuilder的改变比string快多了 2. sb.Append("\r\n" ...

  5. 在Where中对列使用函数,将导致其不可索引

    在Sql语句的Select部分对字段编写标量函数是完全可以的,但是下面代码: select EmpNo,LastName from Emp 应当写为 select EmpNo,LastName fro ...

  6. Jquery Ajax 异步设置Table中某列的值

    可根据table中某列中的ID去改变某列的值! 只是参考,实际应用中不能这样做的,如果有很多行,频繁访问服务器,服务器是顶不住的! JS: $(document).ready(function () ...

  7. Qt中的QTableView 中的列放入Widget

    QTableView是Qt中Model View理念的框架,View只展现数据,所以通过互交修改编辑数据,需要用到委托这个概念Delegate. 所以基本思路是继承QItemDelegate这个类,然 ...

  8. 在datagridview中添加button按钮

    .Net的DataGridView控件中,提供了一种列的类型,叫 DataGridViewButtonColumn ,这种列类型是展示为一个 按钮,可以给button赋予相应的text,并且,此but ...

  9. javafx这些学会后,开发就不难了,往tablecloumn列中添加按钮,修改javafx中tableview中tablecell中的值,修改完回车表示保存到内存中

    javafx开发过程中遇见难题,往tablecloumn列中添加按钮 想了很久的方法,也配有办法判断每行中有数据的地方添加按钮set bank_caozuo.setCellFactory((col)- ...

随机推荐

  1. 会动的文字Marquee应用(转)

    想要做个滚动公告,看了网上的教程,无一不是很恐怖的场频啊java语言编制的JS,或者就是各种复杂,无意中发现了Marquee这东西,用了一下,很简单,只需两行代码,即可以实现很好的效果,特此分享一下. ...

  2. [算法导论]quicksort algorithm @ Python

    算法导论上面快速排序的实现. 代码: def partition(array, left, right): i = left-1 for j in range(left, right): if arr ...

  3. 使用LeakCanary检测内存泄露 翻译 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  4. 怎样修改SQL Server 2005/2008的系统存储过程(转)

    我们知道,SQL Server 2005/2008的系统存储过程在正常情况下是无法直接修改的. 尽管本文是介绍怎样修改它的,但在这里,我还是建议大家尽量不要去修改它.(好像有点绕哈...) OK,闲话 ...

  5. 使用checkstyle来规范你的项目

    Checkstyle是什么 自从做了程序员,关于格式化的讨论就不曾中断过,到底什么才是正确的,什么才是错误的,到现在也没有完整的定论.但随着时间发展,渐渐衍生出一套规范出来.没有什么绝对的正确和错误, ...

  6. iOS - 切换图片/clip subview/iCarousel

    源代码:点击打开链接 这个图片展示的时候,我只想让它在蓝色的view上展示..就需要去设置view的一个属性clip subview..把这个属性打勾...view有个clip subview属性,选 ...

  7. 史上最全面的Buffalo WHR-G300N CH v2 刷OpenWrt教程

    Buffalo WHR-G300N CH v2 刷OpenWrt.有两种办法.一种是Windows下刷.一种是在linux下使用tftp刷.Buffalo WHR-G300N-CH v2的openwr ...

  8. Google Java Style 中文版

      Google Java Style 中文版     基于官方文档2013.12.19最后一次改动. 翻译人:Weir Zhang (zh.weir) 旁白:水平有限,很多地方只是意译.不准确的地方 ...

  9. 轻量级分布式文件系统FastDFS使用安装说明手册(新手入门级)

    轻量级分布式文件系统FastDFS使用安装说明手册(新手入门级) 实验室所在的课题组以研究云计算为主,但所有的研究都是在基于理论的凭空想像,缺少分布式环境的平台的实践,云计算神马的都是浮云了.因此,我 ...

  10. LintCode: Single Number II

    一篇解析比较详细的文章:http://www.acmerblog.com/leetcode-single-number-ii-5394.html C++ 解法(1) 求出每个比特位的数目,然后%3,如 ...