From a3dcbf423306608588a4d02fc5e55f871a1084d9 Mon Sep 17 00:00:00 2001 From: duanshengchao <519970194@qq.com> Date: Thu, 27 Mar 2025 14:47:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E4=BB=A3=E7=90=86=EF=BC=9AattributeTableModel=EF=BC=8C?= =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=95=B0=E6=8D=AE=E7=BC=96=E8=BE=91=E5=92=8C?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=B6=E7=9A=84=E7=89=B9=E6=95=88=E5=B1=95?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 + include/attributeTableDelegate.h | 26 ++++++++++++ include/attributeTableModel.h | 4 +- source/attributeTableDelegate.cpp | 40 +++++++++++++++++ source/attributeTableModel.cpp | 66 ++++++++++++++++++++++++++--- source/tableWidgetHoverDelegate.cpp | 12 ++++++ 6 files changed, 142 insertions(+), 8 deletions(-) create mode 100644 include/attributeTableDelegate.h create mode 100644 source/attributeTableDelegate.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 46642cc..a3b8df8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 ) diff --git a/include/attributeTableDelegate.h b/include/attributeTableDelegate.h new file mode 100644 index 0000000..9a19a99 --- /dev/null +++ b/include/attributeTableDelegate.h @@ -0,0 +1,26 @@ +#ifndef ATTRIBUTETABLEDELEGATE_H +#define ATTRIBUTETABLEDELEGATE_H + +#include +#include + +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 diff --git a/include/attributeTableModel.h b/include/attributeTableModel.h index bfba683..4d94b91 100644 --- a/include/attributeTableModel.h +++ b/include/attributeTableModel.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); diff --git a/source/attributeTableDelegate.cpp b/source/attributeTableDelegate.cpp new file mode 100644 index 0000000..157bc43 --- /dev/null +++ b/source/attributeTableDelegate.cpp @@ -0,0 +1,40 @@ +#include "attributeTableDelegate.h" +#include + +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); + } +} diff --git a/source/attributeTableModel.cpp b/source/attributeTableModel.cpp index 95cec97..5667578 100644 --- a/source/attributeTableModel.cpp +++ b/source/attributeTableModel.cpp @@ -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() diff --git a/source/tableWidgetHoverDelegate.cpp b/source/tableWidgetHoverDelegate.cpp index 0dfab31..5f2e26f 100644 --- a/source/tableWidgetHoverDelegate.cpp +++ b/source/tableWidgetHoverDelegate.cpp @@ -1,4 +1,5 @@ #include "tableWidgetHoverDelegate.h" +#include 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); + } }