176 lines
5.6 KiB
C++
176 lines
5.6 KiB
C++
#ifndef ZHONG_H
|
||
#define ZHONG_H
|
||
|
||
#include <QWidget>
|
||
#include <QtSql/QSql>
|
||
#include <QtSql/QSqlDatabase>
|
||
#include <QtSql/QSqlQueryModel>
|
||
#include <QtSql/QSqlError>
|
||
#include <QSqlQuery>
|
||
#include <QSqlTableModel>
|
||
#include <QDebug>
|
||
#include "addzhong.h"
|
||
#include <QStyledItemDelegate>
|
||
#include <QPushButton>
|
||
#include <QPainter>
|
||
#include <QApplication>
|
||
#include <QMouseEvent>
|
||
#include <QModelIndex>
|
||
#include<QTableView>
|
||
#include<date.h>
|
||
#include<datee.h>
|
||
class CenteredItemDelegate4 : 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 ColoredSqlTableModel : QSqlTableModel {
|
||
public:
|
||
ColoredSqlTableModel(QObject *parent = nullptr) : QSqlTableModel(parent) {}
|
||
|
||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override {
|
||
if (role == Qt::BackgroundRole) {
|
||
if (index.row() % 2 == 0) {
|
||
return QVariant(QBrush(Qt::white));
|
||
} else {
|
||
return QVariant(QBrush(QColor(229, 247, 255)));
|
||
}
|
||
}
|
||
return QSqlTableModel::data(index, role);
|
||
}
|
||
};
|
||
class ButtonDelegate1 : 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 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.rect.setX(deleteButtonOption.rect.x() + deleteButtonOption.rect.width() * 0.4);
|
||
//deleteButtonOption.rect.setWidth(deleteButtonOption.rect.width() * 0.6);
|
||
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() == 6 && event->type() == QEvent::MouseButtonPress)
|
||
{
|
||
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
|
||
|
||
QRect cellRect = option.rect; // 使用更表意清晰的变量名表示单元格矩形区域
|
||
if (cellRect.contains(mouseEvent->pos()))
|
||
{
|
||
emit deleteButtonClicked(index);
|
||
return true;
|
||
}
|
||
|
||
}
|
||
|
||
return QStyledItemDelegate::editorEvent(event, model, option, index);
|
||
}
|
||
|
||
|
||
};
|
||
namespace Ui {
|
||
class zhong;
|
||
}
|
||
|
||
class zhong : public QWidget
|
||
{
|
||
Q_OBJECT
|
||
|
||
public:
|
||
explicit zhong(QWidget *parent = nullptr);
|
||
~zhong();
|
||
void getshui();
|
||
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 bianji(const QModelIndex& index);
|
||
void shanchu(const QModelIndex& index);
|
||
|
||
void on_comboBox_currentTextChanged(const QString &arg1);
|
||
|
||
void on_pushButton_5_clicked();
|
||
|
||
void on_pushButton_6_clicked();
|
||
void getkaishi(QString str);
|
||
void getkaishii(QString str);
|
||
|
||
private:
|
||
date *m_date;
|
||
datee *m_datee;
|
||
QSqlTableModel* model;
|
||
QWidget mask_window;
|
||
void shuaxin();
|
||
addzhong *m_addzhong;
|
||
ButtonDelegate1* delegate2 = new ButtonDelegate1;
|
||
private:
|
||
Ui::zhong *ui;
|
||
|
||
};
|
||
|
||
#endif // ZHONG_H
|