QTableView自定义代理(三):基于QDoubleSpinBox的自定义代理类,输入双精度double类型的值

QTableView自定义代理(三):基于QDoubleSpinBox的自定义代理类

  1. 头文件
#include <QStyledItemDelegate>

class QMyDoubleSpinDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    QMyDoubleSpinDelegate (QObject *parent=0);

//自定义代理组件必须继承以下4个函数
    //创建编辑组件
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                          const QModelIndex &index) const Q_DECL_OVERRIDE;

    void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;
    void setModelData(QWidget *editor, QAbstractItemModel *model,
                      const QModelIndex &index) const Q_DECL_OVERRIDE;
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
                              const QModelIndex &index) const Q_DECL_OVERRIDE;
};
  1. 源文件
#include "qmydoublespindelegate.h"

#include <QDoubleSpinBox>

QMyDoubleSpinDelegate ::QMyDoubleSpinDelegate (QObject *parent):QStyledItemDelegate(parent)
{

}

QWidget *QMyDoubleSpinDelegate ::createEditor(QWidget *parent,
   const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    Q_UNUSED(option);
    Q_UNUSED(index);

    QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
    editor->setFrame(false);
    editor->setMinimum(0);
    editor->setDecimals(2);
    editor->setMaximum(10000);

    return editor;
}

void QMyDoubleSpinDelegate ::setEditorData(QWidget *editor,
                      const QModelIndex &index) const
{
    float value = index.model()->data(index, Qt::EditRole).toFloat();
    QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox*>(editor);
    spinBox->setValue(value);
}

void QMyDoubleSpinDelegate ::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox*>(editor);
    spinBox->interpretText();
    float value = spinBox->value();
    QString str=QString::asprintf("%.2f",value);

    model->setData(index, str, Qt::EditRole);
}

void QMyDoubleSpinDelegate ::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}
  1. 使用方法
QMyDoubleSpinDelegate *doubleSpinDelegate = new QMyDoubleSpinDelegate(this) ;
ui->tableView->setItemDelegateForColumn(2, doubleSpinDelegate); // 给需要的列设置代理
要在`QTableView`中自定义代理按钮并支持两种不同状态,你需要在Qt中结合使用`QStyledItemDelegate`和`QPushButton`。这里是一个简单的步骤概述: 1. **创建自定义代理**: - 创建一个继承自`QStyledItemDelegate`的新,例如`CustomButtonDelegate`。 ```cpp class CustomButtonDelegate : public QStyledItemDelegate { public: CustomButtonDelegate(QObject* parent = nullptr) : QStyledItemDelegate(parent) {} // 添加绘制按钮的槽函数 void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) override { // ... 在这里画按钮,并设置位置 } // 其他必要的槽函数,如sizeHint() 和 createEditor() }; ``` 2. **设置代理**: - 在`QTableView`上设置你的自定义代理。 ```cpp QTableView* tableView = new QTableView(parent); tableView->setItemDelegate(new CustomButtonDelegate(tableView)); ``` 3. **处理按钮点击事件**: - 在`paint()`函数里,你可以画一个`QPushButton`,并在`mousePressEvent()`或似事件中响应按钮点击。 ```cpp bool CustomButtonDelegate::hitTest(const QPoint& point, const QStyleOptionViewItem& option, const QModelIndex& index) override { // 检查鼠标是否落在了按钮区域内 // 如果是,则返回true以便处理按钮点击 if (buttonRect.contains(point)) { return true; } return QStyledItemDelegate::hitTest(point, option, index); } void CustomButtonDelegate::mousePressEvent(QMouseEvent* event) { if (event->button() == Qt::LeftButton) { QPushButton* button = // 获取绘制的按钮实例 // 实现按钮的状态切换逻辑,比如槽函数switchState(button) } } void CustomButtonDelegate::switchState(QPushButton* button) { // 根据按钮当前状态改变样式或执行相应操作 button->setStyleSheet(...); // 改变按钮外观 // 或者执行其他业务逻辑 } ``` 4. **管理两种状态**: - 在`switchState()`中,你可以根据需要检查按钮的状态(例如,通过检查其文本、图标或属性),然后更新相应的样式,或者触发不同的操作。 记得在适当的位置添加信号槽连接,以便在按钮状态改变时执行相应的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

VectorAL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值