#ifndef MICHISTORICALRECORDS_H #define MICHISTORICALRECORDS_H #include #include #include #include #include #include "maxtu.h" #include #include #include #include #include #include #include "look.h" #include #include #include #include #include #include #include #include #include class CheckBoxTableModel : public QSqlTableModel { Q_OBJECT public: CheckBoxTableModel(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 CheckBoxHeader : public QHeaderView { Q_OBJECT public: CheckBoxHeader(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 CenteredItemDelegate : 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); } }; #include #include #include #include #include class ButtonDelegate5 : 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() == 9) { int buttonWidth = option.rect.width() / 2; int buttonHeight = option.rect.height(); int x = option.rect.x(); int y = option.rect.y(); // 绘制查看按钮 QStyleOptionButton editButtonOption; editButtonOption.initFrom(option.widget); // 修改为 option.widget editButtonOption.rect = QRect(x, y, buttonWidth, buttonHeight); editButtonOption.text = "查看"; editButtonOption.state = QStyle::State_Enabled; editButtonOption.features |= QStyleOptionButton::Flat; // 设置查看按钮的调色板 QPalette editButtonPalette = QApplication::style()->standardPalette(); editButtonPalette.setColor(QPalette::ButtonText, QColor(0, 170, 255)); editButtonOption.palette = editButtonPalette; // 绘制查看按钮 QApplication::style()->drawControl(QStyle::CE_PushButton, &editButtonOption, painter); // 绘制删除按钮 QStyleOptionButton deleteButtonOption; deleteButtonOption.initFrom(option.widget); // 修改为 option.widget deleteButtonOption.rect = QRect(x + buttonWidth, y, buttonWidth, buttonHeight); deleteButtonOption.text = "删除"; deleteButtonOption.state = QStyle::State_Enabled; deleteButtonOption.features |= QStyleOptionButton::Flat; // 设置删除按钮的调色板 QPalette deleteButtonPalette = QApplication::style()->standardPalette(); deleteButtonPalette.setColor(QPalette::ButtonText, Qt::red); deleteButtonOption.palette = deleteButtonPalette; // 绘制删除按钮 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) override { if (index.column() == 9 && event->type() == QEvent::MouseButtonPress) { QMouseEvent* mouseEvent = static_cast(event); int buttonWidth = option.rect.width() / 2; int x = option.rect.x(); int y = option.rect.y(); // 检查点击是否在查看按钮区域 QRect editRect = QRect(x, y, buttonWidth, option.rect.height()); if (editRect.contains(mouseEvent->pos())) { emit editButtonClicked(index); return true; } // 检查点击是否在删除按钮区域 QRect deleteRect = QRect(x + buttonWidth, y, buttonWidth, option.rect.height()); if (deleteRect.contains(mouseEvent->pos())) { emit deleteButtonClicked(index); return true; } } return QStyledItemDelegate::editorEvent(event, model, option, index); } }; class ClickToViewDelegatee : 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() == 6 || index.column() == 7) // 绗笁鍒楋紝鍒楃储寮曚粠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 michistoricalrecords; } class michistoricalrecords : public QWidget { Q_OBJECT public: explicit michistoricalrecords(QWidget *parent = nullptr); ~michistoricalrecords(); void getshui(); 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_3_clicked(); void shanchu(const QModelIndex& index); void on_comboBox_currentTextChanged(const QString &arg1); void chakan(const QModelIndex& index); void on_toolButton_3_clicked(); void on_toolButton_clicked(); void getkaishi(QString str); void getkaishii(QString str); void on_pushButton_5_clicked(); void on_pushButton_6_clicked(); void on_pushButton_10_clicked(); void on_pushButton_9_clicked(); void onPageButtonClicked(int id); void on_pushButton_11_clicked(); void onTableViewClicked(const QModelIndex &index); void onDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); void onHeaderCheckStateChanged(Qt::CheckState state); private: CheckBoxHeader *header; date *m_date; datee *m_datee; CheckBoxTableModel* model; QSqlQuery query; void shuaxin(); void updateButtons(); int currentPage=1; int totalPages=1; int recordsPerPage=18; void updatePage(); QButtonGroup *buttonGroup; QHBoxLayout *buttonLayout; QWidget mask_window; QItemSelectionModel *theSelection; //閫夋嫨妯″瀷 QDataWidgetMapper *dataMapper; //鏁版嵁鏄犲皠 maxtu *m_maxtu; ButtonDelegate5* delegate2 = new ButtonDelegate5; ButtonDelegate5* delegate3 = new ButtonDelegate5; signals: void yuan(QPixmap p); void fanhui(); void cha(QString s); void guan2(); void tuichu2(); void lujing(QString str1,QString str2); private: Ui::michistoricalrecords *ui; ClickToViewDelegatee delegate; look *m_look; }; #endif // MICHISTORICALRECORDS_H