添加自定义代理:attributeTableModel,完成数据编辑和删除时的特效展示
This commit is contained in:
parent
e9a3fd0b45
commit
a3dcbf4233
|
|
@ -33,6 +33,7 @@ set(H_HEADER_FILES
|
|||
include/modelInfoEditDialog.h
|
||||
include/sqlQueryExecutor.h
|
||||
include/attributeTableModel.h
|
||||
include/attributeTableDelegate.h
|
||||
include/attributeView.h
|
||||
)
|
||||
|
||||
|
|
@ -54,6 +55,7 @@ set(CPP_SOURCE_FILES
|
|||
source/modelInfoEditDialog.cpp
|
||||
source/sqlQueryExecutor.cpp
|
||||
source/attributeTableModel.cpp
|
||||
source/attributeTableDelegate.cpp
|
||||
source/attributeView.cpp
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
#ifndef ATTRIBUTETABLEDELEGATE_H
|
||||
#define ATTRIBUTETABLEDELEGATE_H
|
||||
|
||||
#include <QStyledItemDelegate>
|
||||
#include <QTableView>
|
||||
|
||||
class AttributeTableDelegate : public QStyledItemDelegate
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit AttributeTableDelegate(QTableView* view, QObject* parent = nullptr);
|
||||
~AttributeTableDelegate();
|
||||
|
||||
/*QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const override;
|
||||
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
|
||||
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
|
||||
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;*/
|
||||
|
||||
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
|
||||
|
||||
private:
|
||||
QTableView* m_tableView;
|
||||
};
|
||||
|
||||
#endif //ATTRIBUTETABLEDELEGATE_H
|
||||
|
|
@ -38,6 +38,7 @@ public:
|
|||
, const QString& tableName = "basic.attribute");
|
||||
~AttributeTableModel();
|
||||
|
||||
QModelIndex index(int row, int column, const QModelIndex& parent) const override;
|
||||
QVariant data(const QModelIndex& index, int role) const override;
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
||||
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
|
||||
|
|
@ -59,7 +60,8 @@ public:
|
|||
//数据操作
|
||||
void setTable(const QString&);
|
||||
void refresh();
|
||||
void addNewRecord();
|
||||
void insertRecord(int);
|
||||
void removeRecord(int);
|
||||
|
||||
//展示列控制
|
||||
void setVisibleColumns(const QStringList& columns);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
#include "attributeTableDelegate.h"
|
||||
#include <QPainter>
|
||||
|
||||
AttributeTableDelegate::AttributeTableDelegate(QTableView* view, QObject *parent)
|
||||
: QStyledItemDelegate(parent)
|
||||
, m_tableView(view)
|
||||
{
|
||||
}
|
||||
|
||||
AttributeTableDelegate::~AttributeTableDelegate()
|
||||
{}
|
||||
|
||||
void AttributeTableDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
||||
{
|
||||
if(index.column() == 0)
|
||||
{
|
||||
QString text = index.data(Qt::DisplayRole).toString();
|
||||
if(text.contains("-") && m_tableView && m_tableView->model()) //删除项,整行画一条红线
|
||||
{
|
||||
int row = index.row();
|
||||
for(int col = 0; col < m_tableView->model()->columnCount(); col++)
|
||||
{
|
||||
QModelIndex index = m_tableView->model()->index(row, col);
|
||||
QRect rect = m_tableView->visualRect(index);
|
||||
painter->save();
|
||||
painter->setPen(Qt::red);
|
||||
painter->drawLine(rect.x(), rect.y() + rect.height()*0.5, rect.x() + rect.width(), rect.y() + rect.height()*0.5); //painter属于view,所以要以view的坐标系为标准
|
||||
painter->restore();
|
||||
QStyledItemDelegate::paint(painter, option, index);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (index.data(Qt::UserRole + 1).toBool())
|
||||
{
|
||||
QStyleOptionViewItem opt = option;
|
||||
initStyleOption(&opt, index);
|
||||
opt.font.setBold(true);
|
||||
QStyledItemDelegate::paint(painter, opt, index);
|
||||
}
|
||||
}
|
||||
|
|
@ -18,6 +18,18 @@ AttributeTableModel::AttributeTableModel(const ModelAttributeGroup& modelAttribu
|
|||
AttributeTableModel::~AttributeTableModel()
|
||||
{}
|
||||
|
||||
QModelIndex AttributeTableModel::index(int row, int column, const QModelIndex& parent) const
|
||||
{
|
||||
if(!hasIndex(row, column, parent))
|
||||
return QModelIndex();
|
||||
|
||||
int dataCol = column - 1;
|
||||
if(dataCol >= m_visibleColumns.count())
|
||||
return QModelIndex();
|
||||
|
||||
return createIndex(row, column);
|
||||
}
|
||||
|
||||
QVariant AttributeTableModel::data(const QModelIndex& index, int role) const
|
||||
{
|
||||
if(!index.isValid())
|
||||
|
|
@ -33,25 +45,25 @@ QVariant AttributeTableModel::data(const QModelIndex& index, int role) const
|
|||
{
|
||||
case Qt::DisplayRole:
|
||||
{
|
||||
QString prefix;
|
||||
QString mark;
|
||||
if(m_modifiedRows.contains(globalRow))
|
||||
{
|
||||
switch(m_modifiedRows[globalRow].state)
|
||||
{
|
||||
case Modified:
|
||||
prefix = "*";
|
||||
mark = "*";
|
||||
break;
|
||||
case New:
|
||||
prefix = "+";
|
||||
mark = "+";
|
||||
break;
|
||||
case Deleted:
|
||||
prefix = "-";
|
||||
mark = "-";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return prefix + QString::number(globalRow + 1);
|
||||
return QString::number(globalRow + 1) + mark;
|
||||
}
|
||||
case Qt::DecorationRole:
|
||||
return QVariant();
|
||||
|
|
@ -104,7 +116,7 @@ QVariant AttributeTableModel::headerData(int section, Qt::Orientation orientatio
|
|||
return role == Qt::DisplayRole ? "No." : QVariant();
|
||||
else
|
||||
{
|
||||
return m_visibleColumns.value(section);
|
||||
return m_visibleColumns.value(section - 1);
|
||||
}
|
||||
}
|
||||
return QAbstractTableModel::headerData(section, orientation, role);
|
||||
|
|
@ -268,9 +280,49 @@ void AttributeTableModel::refresh()
|
|||
emit syncDataStatus(m_modifiedRows.isEmpty(), m_paginationInfo);
|
||||
}
|
||||
|
||||
void AttributeTableModel::addNewRecord()
|
||||
void AttributeTableModel::insertRecord(int row)
|
||||
{
|
||||
if(row < 0 || row > rowCount())
|
||||
return;
|
||||
|
||||
RowData newRow;
|
||||
newRow.state = New;
|
||||
newRow.values.resize(m_visibleColumns.count());
|
||||
|
||||
int globalRow = (m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage + row;
|
||||
m_modifiedRows[globalRow] = newRow;
|
||||
|
||||
beginInsertRows(QModelIndex(), row, row);
|
||||
m_currentPageData.insert(row, newRow);
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
void AttributeTableModel::removeRecord(int row)
|
||||
{
|
||||
if(row < 0 || row > rowCount())
|
||||
return;
|
||||
|
||||
emit showMessage(type_question, QString::fromWCharArray(L"提示"),
|
||||
QString::fromWCharArray(L"确认要删除该记录吗?"));
|
||||
if(g_msgDlgBtn == btn_No)
|
||||
return;
|
||||
|
||||
beginRemoveRows(QModelIndex(), row, row);
|
||||
|
||||
int globalRow = (m_paginationInfo.currentPage - 1) * m_paginationInfo.entriesPerPage + row;
|
||||
m_modifiedRows[globalRow].state = Deleted;
|
||||
if(m_currentPageData.at(row).state == New) //新添加未提交的记录
|
||||
{
|
||||
m_modifiedRows.remove(globalRow);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_currentPageData[row].state = Deleted;
|
||||
m_modifiedRows[globalRow] = m_currentPageData[row];
|
||||
}
|
||||
m_currentPageData.removeAt(row);
|
||||
|
||||
endRemoveRows();
|
||||
}
|
||||
|
||||
void AttributeTableModel::triggerSyncSignal()
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#include "tableWidgetHoverDelegate.h"
|
||||
#include <QPainter>
|
||||
|
||||
QTableWidgetHoverDelegate::QTableWidgetHoverDelegate(QTableWidget *parent)
|
||||
: QStyledItemDelegate{parent}
|
||||
|
|
@ -31,5 +32,16 @@ void QTableWidgetHoverDelegate::paint(QPainter* painter, const QStyleOptionViewI
|
|||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/*int row = index.row();
|
||||
int col = index.column();
|
||||
QTableWidgetItem* item = m_tableWiget->item(row, col);
|
||||
QRect rect = m_tableWiget->visualItemRect(item);
|
||||
qDebug() << row << ", " << col << " optionRect:" << option.rect << " visualItemRect:" << rect;
|
||||
painter->save();
|
||||
painter->setPen(Qt::red);
|
||||
painter->drawLine(rect.x(), rect.y() + rect.height()*0.5, rect.x() + rect.width(), rect.y() + rect.height()*0.5); //painter属于view,所以要以view的坐标系为标准
|
||||
painter->restore();*/
|
||||
QStyledItemDelegate::paint(painter, option, index);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue