175 lines
5.5 KiB
C++
175 lines
5.5 KiB
C++
#ifndef SHU_H
|
||
#define SHU_H
|
||
|
||
#include <QWidget>
|
||
#include <QtSql>
|
||
#include <QDataWidgetMapper>
|
||
#include <QSqlTableModel>
|
||
#include <QTableView>
|
||
#include <QStandardItemModel>
|
||
#include <QStyledItemDelegate>
|
||
#include<QAbstractTableModel>
|
||
#include <QPushButton>
|
||
#include <QMessageBox>
|
||
#include "addshu.h"
|
||
#include <QStyledItemDelegate>
|
||
#include <QPushButton>
|
||
#include <QPainter>
|
||
#include <QApplication>
|
||
#include <QMouseEvent>
|
||
#include <QModelIndex>
|
||
#include<date.h>
|
||
#include<datee.h>
|
||
class CenteredItemDelegate6 : 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 ButtonDelegate3 : 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() == 5) // 判断是否为第五列
|
||
{
|
||
// // 绘制编辑按钮
|
||
// QStyleOptionButton editButtonOption;
|
||
// editButtonOption.rect = option.rect;
|
||
// editButtonOption.rect.setWidth(editButtonOption.rect.width() * 0.4);
|
||
// editButtonOption.text = "编辑";
|
||
// editButtonOption.state = QStyle::State_Enabled;
|
||
// editButtonOption.features |= QStyleOptionButton::Flat; // 设置为扁平样式,去除边框
|
||
// QApplication::style()->drawControl(QStyle::CE_PushButton, &editButtonOption, painter);
|
||
|
||
// 绘制删除按钮
|
||
QStyleOptionButton deleteButtonOption;
|
||
deleteButtonOption.rect = option.rect;
|
||
|
||
deleteButtonOption.text = "删除";
|
||
deleteButtonOption.state = QStyle::State_Enabled;
|
||
deleteButtonOption.features |= QStyleOptionButton::Flat; // 设置为扁平样式,去除边框
|
||
|
||
|
||
// 创建一个调色板对象
|
||
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() == 5 && event->type() == QEvent::MouseButtonPress)
|
||
{
|
||
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
|
||
// 判断是否点击编辑按钮区域
|
||
// QRect editRect = option.rect;
|
||
// editRect.setWidth(editRect.width() * 0.4);
|
||
// if (editRect.contains(mouseEvent->pos()))
|
||
// {
|
||
// emit editButtonClicked(index);
|
||
// return true;
|
||
// }
|
||
|
||
// 判断是否点击删除按钮区域
|
||
QRect cellRect = option.rect; // 使用更表意清晰的变量名表示单元格矩形区域
|
||
if (cellRect.contains(mouseEvent->pos()))
|
||
{
|
||
emit deleteButtonClicked(index);
|
||
return true;
|
||
}
|
||
// QRect deleteRect = option.rect;
|
||
// deleteRect.setX(deleteRect.x() + deleteRect.width() * 0.4);
|
||
// deleteRect.setWidth(deleteRect.width() * 0.6);
|
||
// if (deleteRect.contains(mouseEvent->pos()))
|
||
// {
|
||
// emit deleteButtonClicked(index);
|
||
// return true;
|
||
// }
|
||
}
|
||
return QStyledItemDelegate::editorEvent(event, model, option, index);
|
||
}
|
||
|
||
|
||
};
|
||
namespace Ui {
|
||
class shu;
|
||
}
|
||
|
||
class shu : public QWidget
|
||
{
|
||
Q_OBJECT
|
||
|
||
public:
|
||
explicit shu(QWidget *parent = nullptr);
|
||
~shu();
|
||
private slots:
|
||
void on_pushButton_clicked();
|
||
|
||
void on_pushButton_2_clicked();
|
||
|
||
void on_pushButton_3_clicked();
|
||
|
||
// void on_pushButton_5_clicked();
|
||
|
||
// void on_pushButton_6_clicked();
|
||
|
||
void on_pushButton_4_clicked();
|
||
void shanchu(const QModelIndex& index);
|
||
|
||
void on_pushButton_6_clicked();
|
||
|
||
void on_pushButton_5_clicked();
|
||
void getkaishi(QString str);
|
||
void getkaishii(QString str);
|
||
|
||
private:
|
||
date *m_date;
|
||
datee *m_datee;
|
||
void getshui();
|
||
ButtonDelegate3* delegate = new ButtonDelegate3;
|
||
QSqlTableModel* model;
|
||
|
||
void shuaxin();
|
||
addshu *m_addshu;
|
||
|
||
private:
|
||
Ui::shu *ui;
|
||
QWidget mask_window;
|
||
};
|
||
|
||
#endif // SHU_H
|