242 lines
8.2 KiB
C++
242 lines
8.2 KiB
C++
#ifndef YAOMIN_H
|
||
#define YAOMIN_H
|
||
|
||
#include <QWidget>
|
||
#include <QtSql/QSql>
|
||
#include <QtSql/QSqlDatabase>
|
||
#include <QtSql/QSqlQueryModel>
|
||
#include <QtSql/QSqlError>
|
||
#include <QSqlQuery>
|
||
#include <QSqlTableModel>
|
||
#include <QItemSelection>
|
||
#include <QDataWidgetMapper>
|
||
#include<QSqlRecord>
|
||
#include <QDebug>
|
||
#include "addyaomin.h"
|
||
#include "maxtu.h"
|
||
#include <QStyledItemDelegate>
|
||
#include <QPainter>
|
||
#include <QStyleOptionButton>
|
||
#include <QApplication>
|
||
#include <QStyledItemDelegate>
|
||
#include <QPushButton>
|
||
#include <QPainter>
|
||
#include <QApplication>
|
||
#include <QMouseEvent>
|
||
#include <QModelIndex>
|
||
#include <QFontMetrics>
|
||
#include<date.h>
|
||
#include<datee.h>
|
||
class CenteredItemDelegate3 : 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 ButtonDelegate2 : 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() == 7) // 判断是否为第五列
|
||
{
|
||
// 绘制编辑按钮
|
||
// 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 = 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; // 设置为扁平样式,去除边框
|
||
// QFontMetrics metrics(painter->font());
|
||
// int textWidth = metrics.horizontalAdvance(deleteButtonOption.text );
|
||
|
||
// // 增加一定的间距值(这里设为5像素,可根据实际效果调整)
|
||
// const int padding = 5;
|
||
// textWidth += 2 * padding;
|
||
|
||
// // 调整按钮矩形的位置,使其居左显示,同时考虑间距来设置宽度
|
||
// deleteButtonOption.rect.setX(option.rect.x() + padding);
|
||
// deleteButtonOption.rect.setWidth(textWidth);
|
||
// 创建一个调色板对象
|
||
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() == 7 && 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 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;
|
||
// }
|
||
QRect cellRect = option.rect; // 使用更表意清晰的变量名表示单元格矩形区域
|
||
if (cellRect.contains(mouseEvent->pos()))
|
||
{
|
||
emit deleteButtonClicked(index);
|
||
return true;
|
||
}
|
||
}
|
||
return QStyledItemDelegate::editorEvent(event, model, option, index);
|
||
}
|
||
|
||
|
||
};
|
||
class ClickToViewDelegateee : 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() ==4) // 第三列,列索引从0开始计数
|
||
{
|
||
QString text = "点击查看";
|
||
QStyleOptionButton buttonOption;
|
||
buttonOption.rect = option.rect;
|
||
buttonOption.text = text;
|
||
buttonOption.state = QStyle::State_Enabled;
|
||
buttonOption.features |= QStyleOptionButton::Flat; // 设置为扁平样式,去除边框
|
||
// 使用horizontalAdvance函数获取文本宽度
|
||
|
||
// 创建一个调色板对象
|
||
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 yaomin;
|
||
}
|
||
|
||
class yaomin : public QWidget
|
||
{
|
||
Q_OBJECT
|
||
|
||
public:
|
||
explicit yaomin(QWidget *parent = nullptr);
|
||
~yaomin();
|
||
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 on_currentChanged(const QModelIndex ¤t, const QModelIndex &previous);
|
||
void shanchu(const QModelIndex& index);
|
||
void onCellClicked(const QModelIndex &index);
|
||
|
||
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;
|
||
void getshui();
|
||
QWidget mask_window;
|
||
|
||
ButtonDelegate2* delegate2 = new ButtonDelegate2;
|
||
QSqlTableModel* model;
|
||
QItemSelectionModel *theSelection; //选择模型
|
||
QDataWidgetMapper *dataMapper; //数据映射
|
||
|
||
void shuaxin();
|
||
addyaomin *m_addyaomin;
|
||
maxtu *m_maxtu;
|
||
ClickToViewDelegateee delegate;
|
||
|
||
//QWidget mask_window;
|
||
private:
|
||
Ui::yaomin *ui;
|
||
signals:
|
||
void yuan(QPixmap p);
|
||
};
|
||
|
||
#endif // YAOMIN_H
|