#ifndef TABLEDELEGATE_H
#define TABLEDELEGATE_H
#include <QStyledItemDelegate>
class TableDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit TableDelegate(QObject *parent = nullptr);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
};
#endif // TABLEDELEGATE_H
#include "tabledelegate.h"
#include <QPainter>
#include <QColorDialog>
#include "tablemodel.h"
#include <QDebug>
#include <QCheckBox>
#include <QApplication>
#include <QMouseEvent>
TableDelegate::TableDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
}
QWidget *TableDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.column() == TableModel::Color)
{
QColorDialog *editor = new QColorDialog(parent);
return editor;
}
// else if (index.column() == TableModel::Visible)
// {
// QCheckBox *editor = new QCheckBox(parent);
// return editor;
// }
return QStyledItemDelegate::createEditor(parent, option, index);
}
void TableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if (index.column() == TableModel::Color)
{
QColorDialog *dlg = qobject_cast<QColorDialog *>(editor);
if (dlg)
{
const QVariant color = index.model()->data(index);
dlg->setCurrentColor(color.value<QColor>());
}
}
// else if (index.column() == TableModel::Visible)
// {
// QCheckBox *checkbox = qobject_cast<QCheckBox *>(editor);
// if(checkbox)
// {
// const bool visible = index.model()->data(index).toBool();
// checkbox->setChecked(visible);
// }
// }
else
{
QStyledItemDelegate::setEditorData(editor, index);
}
}
void TableDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if (index.column() == TableModel::Color)
{
QColorDialog *dlg = qobject_cast<QColorDialog *>(editor);
if (dlg)
{
model->setData(index,dlg->currentColor());
}
}
// else if (index.column() == TableModel::Visible)
// {
// QCheckBox *checkbox = qobject_cast<QCheckBox *>(editor);
// if(checkbox)
// {
// model->setData(index,checkbox->checkState());
// }
// }
else
{
QStyledItemDelegate::setModelData(editor, model, index);
}
}
void TableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem viewOption(option);
initStyleOption(&viewOption, index);
if (option.state.testFlag(QStyle::State_HasFocus))
viewOption.state = viewOption.state ^ QStyle::State_HasFocus;
if (index.column() == TableModel::Color)
{
painter->save();
const QVariant value = index.model()->data(index);
QColor color = value.value<QColor>();
QRect rect(option.rect);
painter->fillRect(rect, color);
// QPen pen;
// pen.setColor(color);
// pen.setWidth(3);
// pen.setStyle(Qt::SolidLine);
// painter->setPen(pen);
// painter->drawLine(QPoint(rect.x(), rect.y() + rect.height()/2), QPoint(rect.x() + rect.width(),rect.y()+ rect.height()/2));
painter->restore();
}
else if (index.column() == TableModel::Visible)
{
bool data = index.model()->data(index, Qt::EditRole).toBool();
QStyleOptionButton checkBoxStyle;
checkBoxStyle.state = data ? QStyle::State_On : QStyle::State_Off;
checkBoxStyle.state |= QStyle::State_Enabled;
checkBoxStyle.iconSize = QSize(20, 20);
checkBoxStyle.rect = option.rect;
QCheckBox checkBox;
checkBoxStyle.iconSize = QSize(20, 20);
checkBoxStyle.rect = option.rect;
QApplication::style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &checkBoxStyle, painter, &checkBox);
}
else
{
QStyledItemDelegate::paint(painter, option, index);
}
}
bool TableDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
QRect decorationRect = option.rect;
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
if (event->type() == QEvent::MouseButtonPress && decorationRect.contains(mouseEvent->pos()))
{
if (index.column() == TableModel::Visible)
{
bool data = model->data(index, Qt::EditRole).toBool();
model->setData(index, !data, Qt::EditRole);
return true;
}
}
return QStyledItemDelegate::editorEvent(event, model, option, index);
}
void TableDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
Q_UNUSED(index)
editor->setGeometry(option.rect);
}
model
#include "tablemodel.h"
#include <QDebug>
#include <QColor>
#define CHANNEL_NUM 1024
const std::vector<QColor> g_vetPenColor =
{
QColor(0, 0, 0), QColor(202,235,216), QColor(128,118,105),
QColor(255,235,205), QColor(255,245,238), QColor(255,0,0),
QColor(227,23,13), QColor(255,127,80), QColor(255,192,203),
QColor(116,0,0), QColor(255,255,0), QColor(227,207,87),
QColor(255,125,64), QColor(255,227,132), QColor(85,102,0),
QColor(0,255,0), QColor(128,42,42), QColor(0,255,255),
QColor(199,97,20), QColor(127,255,0), QColor(210,180,140),
QColor(8,46,84), QColor(188,143,143), QColor(34,139,34),
QColor(107,142,35), QColor(199,97,20), QColor(0,0,255),
QColor(160,32,240), QColor(218,112,214), QColor(25,25,112),
QColor(138,43,226), QColor(153,51,250)
};
TableModel::TableModel(QObject *parent) : QAbstractTableModel(parent)
{
m_vetIsVisible.resize(CHANNEL_NUM);
m_vetCurvesColor.resize(CHANNEL_NUM);
m_vetChannelNme.resize(CHANNEL_NUM);
for(int ch = 0; ch < CHANNEL_NUM; ++ch)
{
m_vetIsVisible[ch] = true;
m_vetCurvesColor[ch] = g_vetPenColor[ch%32];
m_vetChannelNme[ch] =QString("CH %1").arg(ch+1);
}
}
Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
{
if (index.column() == Channel)
{
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
else if(index.column() == Color)
{
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
}
else if(index.column() == Visible)
{
return Qt::NoItemFlags;
}
return QAbstractTableModel::flags(index);
}
int TableModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : m_vetChannelNme.size();
}
int TableModel::columnCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : Count;
}
QVariant TableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() < 0 || index.row() >= m_vetChannelNme.size())
{
return QVariant();
}
switch (role)
{
case Qt::DisplayRole:
case Qt::EditRole:
if (index.column() == Channel)
{
return m_vetChannelNme[index.row()];
}
else if (index.column() == Visible)
{
return m_vetIsVisible[index.row()];
}
else if (index.column() == Color)
{
return m_vetCurvesColor[index.row()];
}
break;
}
return QVariant();
}
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
switch (section)
{
case Channel:
return "Channel";
case Visible:
return "Visible";
case Color:
return "Color";
}
}
return QAbstractTableModel::headerData(section, orientation, role);
}
bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid() || index.row() < 0 || index.row() >= m_vetChannelNme.size())
{
return false;
}
switch (role)
{
case Qt::DisplayRole:
case Qt::EditRole:
if (index.column() == Channel)
{
m_vetChannelNme[index.row()] = value.toString();
}
else if (index.column() == Visible)
{
m_vetIsVisible[index.row()] = value.toBool();
}
else if (index.column() == Color)
{
m_vetCurvesColor[index.row()] = value.value<QColor>();
}
emit dataChanged(index, index);
return true;
}
return false;
}
#ifndef TABLEMODEL_H
#define TABLEMODEL_H
#include <QAbstractTableModel>
class TableModel : public QAbstractTableModel
{
Q_OBJECT
public:
enum Columns { Channel = 0, Visible, Color, Count = Color + 1 };
explicit TableModel(QObject *parent = nullptr);
Qt::ItemFlags flags(const QModelIndex &index) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole);
private:
std::vector<bool> m_vetIsVisible;
std::vector<QColor> m_vetCurvesColor;
std::vector<QString> m_vetChannelNme;
};
#endif // TABLEMODEL_H
widget
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "tablemodel.h"
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
private:
TableModel *tableModel;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include "tabledelegate.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
tableModel = new TableModel(); // Model
ui->tableView->setModel(tableModel); // View
ui->tableView->setItemDelegate(new TableDelegate); // Delegate
// QString checkQss = "QCheckBox::indicator { width: 20px;height: 20px;}"
// "QCheckBox::indicator::unchecked {image: url(:/img/res/checkun.png);}"
// "QCheckBox::indicator::checked {image: url(:/img/res/check.png);}";
ui->tableView->horizontalHeader()->setStretchLastSection(true);
}
Widget::~Widget()
{
delete ui;
}
mian
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
使用MVD模式,目的是自定义颜色选择框
源码下载