#ifndef BACHISTORICALRECORDS_H #define BACHISTORICALRECORDS_H #include #include #include #include #include #include #include #include "maxtu.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include class CheckBoxTableModel2 : public QSqlTableModel { Q_OBJECT public: CheckBoxTableModel2(QObject *parent = nullptr, QSqlDatabase db = QSqlDatabase()) : QSqlTableModel(parent, db) {} QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override { if (index.column() == 0 && role == Qt::CheckStateRole) { // 返回复选框的状态 return m_checkStates.value(index.row(), Qt::Unchecked); } return QSqlTableModel::data(index, role); } bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override { if (index.column() == 0 && role == Qt::CheckStateRole) { // 设置复选框的状态 m_checkStates[index.row()] = value.toBool() ? Qt::Checked : Qt::Unchecked; emit dataChanged(index, index, {role}); return true; } return QSqlTableModel::setData(index, value, role); } Qt::ItemFlags flags(const QModelIndex &index) const override { if (index.column() == 0) { // 使第一列可选中 return QSqlTableModel::flags(index) | Qt::ItemIsUserCheckable; } return QSqlTableModel::flags(index); } void setAllCheckState(Qt::CheckState state) { for (int i = 0; i < rowCount(); ++i) { QModelIndex index = this->index(i, 0); setData(index, state, Qt::CheckStateRole); } } private: QHash m_checkStates; // 存储复选框的状态 }; class CheckBoxHeader2 : public QHeaderView { Q_OBJECT public: CheckBoxHeader2(Qt::Orientation orientation, QWidget *parent = nullptr) : QHeaderView(orientation, parent), m_checkState(Qt::Unchecked) {} protected: void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override { painter->save(); QHeaderView::paintSection(painter, rect, logicalIndex); painter->restore(); if (logicalIndex == 0) { QStyleOptionButton option; option.initFrom(this); option.rect = QRect(rect.left() + 3, rect.top() + 13, 16, 16); option.state = QStyle::State_Enabled | QStyle::State_Active; if (m_checkState == Qt::Checked) { option.state |= QStyle::State_On; } else if (m_checkState == Qt::Unchecked) { option.state |= QStyle::State_Off; } else { option.state |= QStyle::State_NoChange; } style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter, this); } } void mousePressEvent(QMouseEvent *event) override { int logicalIndex = logicalIndexAt(event->pos()); if (logicalIndex == 0) { if (m_checkState == Qt::Checked) { m_checkState = Qt::Unchecked; } else { m_checkState = Qt::Checked; } updateSection(0); emit checkStateChanged(m_checkState); } QHeaderView::mousePressEvent(event); } signals: void checkStateChanged(Qt::CheckState state); private: Qt::CheckState m_checkState; }; class CenteredItemDelegate2 : public QStyledItemDelegate { public: QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override { QWidget* editor = QStyledItemDelegate::createEditor(parent, option, index); if (editor) { editor->setStyleSheet("text-align: center;"); } return editor; } void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override { QStyleOptionViewItem opt = option; opt.displayAlignment = Qt::AlignHCenter | Qt::AlignVCenter; QStyledItemDelegate::paint(painter, opt, index); } }; class ButtonDelegate4 : public QStyledItemDelegate { Q_OBJECT signals: void editButtonClicked(const QModelIndex& index) const; void deleteButtonClicked(const QModelIndex& index) const; public: using QStyledItemDelegate::QStyledItemDelegate; // 创建编辑器,这里我们不需要编辑器,返回nullptr QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override { return nullptr; } // 重绘函数,用于绘制按��? void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override { if (index.column() == 6) { // 绘制删除按钮 QStyleOptionButton deleteButtonOption; // deleteButtonOption.rect = option.rect; deleteButtonOption.rect = option.rect; deleteButtonOption.text = "删除"; deleteButtonOption.state = QStyle::State_Enabled; deleteButtonOption.features |= QStyleOptionButton::Flat; // 设置为扁平样式,去除边框 QFontMetrics metrics(painter->font()); // 创建一个调色板对象 QPalette buttonPalette = QApplication::style()->standardPalette(); // 设置文本颜色为红色 buttonPalette.setColor(QPalette::ButtonText, Qt::red); // 将调色板应用到按钮样式选项中 deleteButtonOption.palette = buttonPalette; QApplication::style()->drawControl(QStyle::CE_PushButton, &deleteButtonOption, painter); } else { QStyledItemDelegate::paint(painter, option, index); } } // 处理鼠标事件,判断按钮点��? bool editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index) { if (index.column() == 6 && event->type() == QEvent::MouseButtonPress) { QMouseEvent* mouseEvent = static_cast(event); QRect cellRect = option.rect; // 使用更表意清晰的变量名表示单元格矩形区域 if (cellRect.contains(mouseEvent->pos())) { emit deleteButtonClicked(index); return true; } } return QStyledItemDelegate::editorEvent(event, model, option, index); } }; class ClickToViewDelegate : public QStyledItemDelegate { public: using QStyledItemDelegate::QStyledItemDelegate; QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override { // 不允许编辑,返回nullptr return nullptr; } void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override { if (index.column() == 2 || index.column() == 3) // 第三列,列索引从0开始计��? { QString text = "点击查看"; QStyleOptionButton buttonOption; buttonOption.rect = option.rect; buttonOption.text = text; buttonOption.state = QStyle::State_Enabled; buttonOption.features |= QStyleOptionButton::Flat; // 璁剧疆涓烘墎骞虫牱寮忥紝鍘婚櫎杈规 QFontMetrics metrics(painter->font()); // 创建一个调色板对象 QPalette buttonPalette2 = QApplication::style()->standardPalette(); buttonPalette2.setColor(QPalette::ButtonText, QColor(0, 170, 255)); // 将调色板应用到按钮样式选项中 buttonOption.palette = buttonPalette2; QApplication::style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter); } else { QStyledItemDelegate::paint(painter, option, index); } } }; namespace Ui { class bachistoricalrecords; } class bachistoricalrecords : public QWidget { Q_OBJECT public: explicit bachistoricalrecords(QWidget *parent = nullptr); ~bachistoricalrecords(); void getname(); public slots: void getshuaxin(); private slots: void onCellClicked(const QModelIndex &index); void on_pushButton_clicked(); void on_pushButton_2_clicked(); void on_pushButton_4_clicked(); void on_pushButton_6_clicked(); void shanchu(const QModelIndex& index); void on_toolButton_clicked(); void on_pushButton_3_clicked(); void on_pushButton_5_clicked(); void getkaishi(QString str); void getkaishii(QString str); void onPageButtonClicked(int id); void on_pushButton_10_clicked(); void on_pushButton_9_clicked(); void on_pushButton_11_clicked(); void onTableViewClicked(const QModelIndex &index); void onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); void onHeaderCheckStateChanged(Qt::CheckState state); private: CheckBoxHeader2 *header; CheckBoxTableModel2* model; QSqlQueryModel *currentModel; // 新增成员变量 void updateButtons(); int currentPage=0; int totalPages; int recordsPerPage=18; void updatePage(); QButtonGroup *buttonGroup; QHBoxLayout *buttonLayout; QWidget mask_window; void handleErrorAndRelease(QAxObject *workbook, QAxObject *workbooks, QAxWidget *excel); Ui::bachistoricalrecords *ui; QSqlQuery query; void shuaxin(); QItemSelectionModel *theSelection; //选择模型 QDataWidgetMapper *dataMapper; //数据映射 maxtu *m_maxtu; ClickToViewDelegate delegate; ButtonDelegate4* delegate2; date *m_date; datee *m_datee; signals: void yuan(QPixmap p); void jie(QPixmap p); void fanhui(); void lujing(QString str1,QString str2); void guan(); void tuichu2();//历史记录的退出 }; #endif // BACHISTORICALRECORDS_H