首先谢谢
http://www.cnblogs.com/li-peng/p/3961843.html
http://www.cnblogs.com/li-peng/p/4029885.htmlhttp://qimo601.iteye.com/blog/1539147
下面直接上代码:
E:\delcode\TowButtons\TowButtons.pro 在本电脑上面的路径
buttondelegate.h :
#ifndef BUTTONDELEGATE_H
#define BUTTONDELEGATE_H
#include <QItemDelegate>
class ButtonDelegate : public QItemDelegate
{
Q_OBJECT
public:
explicit ButtonDelegate(QObject *parent = 0);
void setBtnCnt(int btnCnt) {m_btnCnt=btnCnt;}
void createStyleOptionButton(QVector<QStyleOptionButton *> &vecBtns, QStringList & strtext) const;
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) Q_DECL_OVERRIDE;
signals:
public slots:
private:
void showMsg(QString str);
int m_btnCnt;
private:
typedef QMap<QModelIndex, QPair<QStyleOptionButton*, QStyleOptionButton*>* > collButtons;
// collButtons m_btns;
typedef QMap<QModelIndex, QVector<QStyleOptionButton*> > vecButtons;
vecButtons m_VecBtns;
};
#endif // BUTTONDELEGATE_H
mainwindow.h :
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
class MainWindow : public QWidget
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
};
#endif // MAINWINDOW_H
tablemodel.h:
#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 Q_DECL_OVERRIDE;
int columnCount(const QModelIndex &parent) const Q_DECL_OVERRIDE;
QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE;
QVariant headerData(int section, Qt::Orientation orientation, int role) const Q_DECL_OVERRIDE;
void setData(const QVector<QStringList>& data) ;
QVector<QStringList>& DataVector() {return m_data;}
void setHorizontalHeader(const QStringList& headers);
~TableModel(void);
signals:
public slots:
private:
QStringList m_HorizontalHeader;
QVector<QStringList> m_data;
};
#endif // TABLEMODEL_H
tableview.h:
#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
buttondelegate.cpp:
#include "buttondelegate.h"
#include <QApplication>
#include <QMouseEvent>
#include <QMessageBox>
#include <QPainter>
#include <QStyleOption>
#include <QDesktopWidget>
#include <QDebug>
ButtonDelegate::ButtonDelegate(QObject *parent) :
QItemDelegate(parent)
{
m_btnCnt=3;//! 默认为3个按钮
}
void ButtonDelegate::createStyleOptionButton(QVector<QStyleOptionButton*> & vecBtns, QStringList &strtext) const
{
for(int i=0;i<strtext.count();i++)
{
QStyleOptionButton* pBtn = new QStyleOptionButton();
pBtn->text = strtext[i];
pBtn->state |= QStyle::State_Enabled;
vecBtns.append(pBtn);
}
}
void ButtonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QVector<QStyleOptionButton*> vecBtns=m_VecBtns.value(index);
if (0==vecBtns.count()) {
QStringList strtext;
if(m_btnCnt==1)
{
strtext<<tr("XXX");
}
if(m_btnCnt==2)
{
strtext<<tr("XXX")<<tr("YYY");
}
if(m_btnCnt==3)
{
strtext<<tr("XXX")<<tr("YYY")<<tr("ZZZ");
}
createStyleOptionButton(vecBtns, strtext);
(const_cast<ButtonDelegate *>(this))->m_VecBtns.insert(index, vecBtns);
}
int widLen=option.rect.width();
if(m_btnCnt==3)
{
vecBtns[0]->rect = option.rect.adjusted(0, 4, -(widLen*2/3 + 4) , -4);
vecBtns[1]->rect = option.rect.adjusted(widLen/3 + 4, 4, -(widLen/3+4), -4);
vecBtns[2]->rect = option.rect.adjusted(widLen*2/3 + 4 , 4, -4, -4);
}
if(m_btnCnt==2)
{
vecBtns[0]->rect = option.rect.adjusted(4, 4, -(widLen/2 + 4) , -4);
vecBtns[1]->rect = option.rect.adjusted(widLen/2 + 4, 4, -4, -4);
}
if(m_btnCnt==1)
{
vecBtns[0]->rect = option.rect.adjusted(0, 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, vecBtns[0], painter);
if(m_btnCnt>=2) QApplication::style()->drawControl(QStyle::CE_PushButton, vecBtns[1], painter);
if(m_btnCnt==3)QApplication::style()->drawControl(QStyle::CE_PushButton, vecBtns[2], 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_VecBtns.contains(index)) {
QVector<QStyleOptionButton*> btns = m_VecBtns.value(index);
if (btns[0]->rect.contains(e->x(), e->y())) {
btns[0]->state |= QStyle::State_Sunken;
}
else if(btns[1]->rect.contains(e->x(), e->y())) {
btns[1]->state |= QStyle::State_Sunken;
}
else if(btns[2]->rect.contains(e->x(), e->y())) {
btns[2]->state |= QStyle::State_Sunken;
}
}
}
if (event->type() == QEvent::MouseButtonRelease) {
QMouseEvent* e =(QMouseEvent*)event;
if (m_VecBtns.contains(index)) {
QVector<QStyleOptionButton*> btns = m_VecBtns.value(index);
if (btns[0]->rect.contains(e->x(), e->y())) {
btns[0]->state &= (~QStyle::State_Sunken);
showMsg(tr("btn1 column %1").arg(index.column()));
} else if(btns[1]->rect.contains(e->x(), e->y())) {
btns[1]->state &= (~QStyle::State_Sunken);
showMsg(tr("btn2 row %1").arg(index.row()));
}
else if(btns[2]->rect.contains(e->x(), e->y())) {
btns[2]->state &= (~QStyle::State_Sunken);
showMsg(tr("btn2 row %1").arg(index.row()));
}
}
}
return true;
}
void ButtonDelegate::showMsg(QString str)
{
QMessageBox msg;
msg.setText(str);
msg.exec();
}
main.cpp:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp:
#include "mainwindow.h"
#include "tableview.h"
#include "tablemodel.h"
#include <QLayout>
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
this->resize(800, 600);
TableView *tv = new TableView(this);
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(tv);
this->setLayout(layout);
}
MainWindow::~MainWindow()
{
}
tablemodel.cpp:
#include "tablemodel.h"
TableModel::TableModel(QObject *parent) :
QAbstractTableModel(parent)
{
}
TableModel::~TableModel()
{
}
int TableModel::rowCount(const QModelIndex &parent) const
{
int i=m_data.size();
return i;
}
int TableModel::columnCount(const QModelIndex &parent) const
{
int j=m_HorizontalHeader.count();
return j;
}
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.cpp:
#include "tableview.h"
#include "tablemodel.h"
#include "buttondelegate.h"
#include <QHeaderView>
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"<< "Progress-test";
m_model->setHorizontalHeader(headers);
QVector<QStringList> data;
data.append(QStringList() << "1" << "01"<< "Progress-test111");
data.append(QStringList() << "2" << "02"<< "Progress-test222");
data.append(QStringList() << "3" << "03"<< "Progress-test333");
data.append(QStringList() << "4" << "04"<< "Progress-test444");
data.append(QStringList() << "5" << "05"<< "Progress-test555");
m_model->setData(data);
m_buttonDelegate = new ButtonDelegate(this);
m_buttonDelegate->setBtnCnt(3);
// this->setItemDelegateForRow(1, m_buttonDelegate);
this->setItemDelegateForColumn(1, m_buttonDelegate);
emit m_model->layoutChanged();
this->setColumnWidth(1, 500);
this->horizontalHeader()->setStretchLastSection(true);
}