在QTableView的一列里添加两个按钮,之前添加一个按钮的思路是一样的,只是计算了一下按钮的宽,放两个按钮而已。

添加一个按钮的例子:QTableView 添加按钮

本例源代码:QtTowButtons.rar

看一下列的效果

看一下添加两个按钮的效果点击第一个按钮弹出 but1 +当前列 点击第二个按钮弹出but2 + 当前行

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAf0AAAE+CAIAAAAI0l7FAAAgAElEQVR4nO3dfXAb52Hn8SczncnV7SXtNXXrztmdNo7L2HXrjgU1ZOS7xL2LoySuY7uKk0gEXU3AS9WMR07cOdfTKJEIvdkWhdi1HVt27agWSVGWE5ctm+ioxJ2RHFKm7wSuCEDEUCRBYBfvfJFIACJI3B8LggvsLghi98HuPvv7zDNjACShx1jgi+UDEEtmr17jOC4BAAD6GTAH+cQ4jiOzV6+i+wAA+jI6+EXyiXEcR2bm0X0AAJ0ZHfwi+cQ4jiMzc/PoPgCAvowOfpF8YhzHkfTcHLoPAKAvo4NfJJ8Yx3HkibcT6D4AgL6MDn6RfGIcx5H/8lQE3QcAUNNUldpPGR38IvnEOI4jv/f0Ovv7e92Hahz//tMzsVhMh5sZAMA0Gtz9QqFQKBSqX6K1+7f8IFW9+78cel9xDF0YvjD8wdD7HwxeGBYv2es+NDU1pcPNDABgGlXiTml/Xxp6LdFX775nne4v5pZqHHvdhyYmJjZ0g4KdDRxW3oE6rHBf3Shv1079rxTsqfHdL+VeY/RVu3/z0WT17ufz+YGz3m17Tj2y7+TXDvZsP3LC+fxxgedz2UxpbNn1Ri6bQfehPt6unU1NO7u81K5SfIapJf3erp3l3zhwWO+pgdUY0v0BzXv6Vbt/ZJ3uL+fzA2e9j73wr4+/0ved13/yd2++/eTJU+g+6Ih692v/F9B9kGFwf3/d13Xnry0OnPV+78TPHjh07AtPP3e/5+iDLz4jdj+bzWQzi7ls5sGXnkH3oW4qVS5fq1mN8cBh6a67+D2ls6VKK3W/+F2VaS+dr1gb2tnVVb4QtfozyvMq/esDa1+W/j95lS8GGj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5j524+xdd6U+ebvwiY/H/+Tuz/yoUGX89zeUx3/7UeEelUEI2UHIW4SkCfkJIScJSRLyL4T0EBIn5N8I6SJEIKSbkGcJ8RHyDULuIuQ9Qv6NkNcJmSLkLCGvETJByC8IeYWQcUL+gxBO9ovLq2+eam9vN/DYx6CLu+++W8uPDw8Pq33pa54PvvXj3K63rj3248yOH6Vf/tn4cvZqMj1T+suXdDp97dq1N7tPx2Lxubm5mZkZ+V/HpNPpXC5XKBRWVla0zBOgMf4/qocWiayADIkAAAAASUVORK5CYII=" alt="" width="316" height="199" />

下面是主要实现

继承自 QItemDelegate

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

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

再处理相应的点击事件

#ifndef BUTTONDELEGATE_H
#define BUTTONDELEGATE_H #include <QItemDelegate> class ButtonDelegate : public QItemDelegate
{
Q_OBJECT
public:
explicit ButtonDelegate(QObject *parent = );
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(, , -(option.rect.width() / + ) , -); //
buttons->second->rect = option.rect.adjusted(buttons->first->rect.width() + , , -, -);
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 = );
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);
else
return 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 = );
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() << "" << "");
data.append(QStringList() << "" << "");
data.append(QStringList() << "" << "");
data.append(QStringList() << "" << "");
data.append(QStringList() << "" << "");
m_model->setData(data); m_buttonDelegate = new ButtonDelegate(this);
this->setItemDelegateForColumn(, m_buttonDelegate);
emit m_model->layoutChanged();
this->setColumnWidth(, );
}

这就完成了

我们看一下调用

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

QTableView 一列添加两个按钮的更多相关文章

  1. Revit二次开发之添加选项卡和按钮

      我们日常在revit开发中经常会用到按钮,可以通过revitAPI提供的接口创建按钮,今天我简单介绍一下两种按钮,一种是单命令按钮,另一种是含下拉菜单的按钮,包括创建他们的方法. 实现方法 1.实 ...

  2. swift项目实战--微博的未登录界面的实现,和监听未登录界面两个按钮的两种实现方法

    1.未登录界面的实现 微博项目中,用户不登录的话,显示的是未登录的界面.项目中TabBarVC的子控制器都是tableViewVC,所以抽取了父类,让父类判断用户是否登录,决定显示什么样的界面.loa ...

  3. 黄聪:TinyMCE 4 增强 添加样式、按钮、字体、下拉菜单和弹出式窗口

    我最喜欢 WordPress 3.9 的更新是使用了 TinyMCE 4.0 编辑器.新的 TinyMCE 看起来看起来更整洁(真正匹配WP仪表板),它有一些非常不错的附加功能.我的很多老主题和插件必 ...

  4. Dynamics CRM2011 隐藏sub-grid 新建项和添加现有项按钮

    在CRM2011中ribbon区的自定义按钮可以通过工具例如RibbonEditor或者RibbonWorkbench进行配置包括action.display等等,但是系统级别的按钮是不能进行编辑的, ...

  5. 三种方法为QLineEdit添加清除内容按钮

    很多时候我们会发现输入的一长串内容不得不全部删除重新输入,这时比起一直按着退格键不放一个清除内容按钮更受欢迎. 今天我将介绍三种为QLineEdit添加清除内容按钮的方法,其中两种方法有较强的功能针对 ...

  6. OAF TABLE中第一列添加事件不生效

    我遇到一个比较诡异的现象 在TABLE中,我在TABLE的第一列添加了一个MessageCheckBox,并为其设置全局刷新的FireAction事件selection, 但是点击勾选框按钮之后,事件 ...

  7. 为GRIDVIEW“删除”列添加确认对话框

    如何为gridview控件里的“删除”列添加一个确认对话框?网上众说纷纭,大致见到了三种解决方案,现归纳如下:1.继承Web.IO里的button控件,为其实现一个IPostback的接口用于回调,具 ...

  8. Qt QTableView 表格内添加控件

    目录 Qt QTableView 表格内添加控件 1. QItemDelegate 2. setIndexWidget 3. setIndexWidget + setLayout Qt QTableV ...

  9. 判断ecshop未付款添加去付款按钮

    判断ecshop未付款添加去付款按钮,打开ecshop模板目录下user_transaction.dwt找到{$item.handler} 这个变量 给它下面添加以下代码 {if $item.pay_ ...

随机推荐

  1. python urllib2 发起http请求post

    使用urllib2发起post请求 def GetCsspToken(): data = json.dumps({"userName":"wenbin", &q ...

  2. Apache虚拟主机配置

    在一个Apache服务器上可以配置多个虚拟主机,实现一个服务器提供多站点服务,其实就是访问同一个服务器上的不同目录.Apache虚拟主机配置有3中方法:基于IP配置.基于域名配置和基于端口配置,这里介 ...

  3. 首先,编写一个类ChongZai,该类中有3个重载的方法void print();其次, 再编写一个主类来测试ChongZai类的功能

    //计算器 jisuanqi jsq=new jisuanqi(); System., )); System., , )); System.out.println("2.3和4.5 中最大的 ...

  4. spring定时器(一)

    使用的是spring的quartz功能,需导入com.springsource.org.quartz.jar包. 此定时器无法重置定时时间,需要的话可查看:spring定时器(二) spring的be ...

  5. ThreadPoolExecutor-线程池开发的使用

    好久没有写过笔记了,最近做的一个项目涉及打线程池和队列的开发,觉得在这个项目中学习到的还是挺多的,对线程安全,并发的知识有加深认知:当然,现在用过的东西并不是代表以后还能娴熟的使用,做好笔记非常重要: ...

  6. 九、Android学习第八天——广播机制与WIFI网络操作(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 九.Android学习第八天——广播机制与WIFI网络操作 今天熟悉了An ...

  7. plain framework 1 pak插件说明(资源压缩加密)

    在互联网的发展中,资源的整理一般成了发布软件应用的迫在眉睫的一件事情,不经打包的资源往往容易暴露而且众多的文件使得拷贝等待时间变长.在这种情况下,一种应用便诞生了,其起源是源自压缩软件,这便是我们今天 ...

  8. Codeforces 461B. Appleman and Tree[树形DP 方案数]

    B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. POJ1062昂贵的聘礼[最短路建模]

    昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 45892   Accepted: 13614 Descripti ...

  10. avalon.js路由

    之前自己写了一个AJAX加载页面的方法:有时候一个页面里面会分区域加载不同的东西(div,html),但是IE的回退按钮,就失去任何意义了: 这两天研究了一下avalon.js的路由: 需要准备: 1 ...