QTableView 自定义代理(一):基于QComboBox 的下拉框代理类

QTableView 自定义item delegate,单元格下拉框代理。

  1. 头文件
#include <QStyledItemDelegate>

class QMyComboBoxDelegate: public QStyledItemDelegate
{
    Q_OBJECT
public:
    QMyComboBoxDelegate(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 "qmycomboboxdelegate.h"
#include <QComboBox>

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

}

QWidget *QMyComboBoxDelegate::createEditor(QWidget *parent,
       const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QComboBox *editor = new QComboBox(parent);

    editor->addItem("计算机");
    editor->addItem("英语");
    editor->addItem("会计");
    editor->addItem("通信");

    return editor;
}

void QMyComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QString str = index.model()->data(index, Qt::EditRole).toString();

    QComboBox *comboBox = static_cast<QComboBox*>(editor);
    comboBox->setCurrentText(str);
}

void QMyComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QComboBox *comboBox = static_cast<QComboBox*>(editor);

    QString str = comboBox->currentText();

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

void QMyComboBoxDelegate::updateEditorGeometry(QWidget *editor,
                const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}

  1. 使用方法
QMyComboBoxDelegate *comboBoxDelegate = new QMyComboBoxDelegate(this);
ui->tableView->setItemDelegateForColumn(0, comboBoxDelegate); // 给需要的列设置代理
  • 7
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
QTableView是一个非常灵活的控件,可以很容易地通过自定义来添加图片、复选框、下拉框、按钮、滑条、微调框和日历等控件。 首先,我们可以通过自定义QStandardItemModel来向QTableView添加图片。可以在数据模型使用Qt::DecorationRole添加图片数据,并且可以通过自定义代理来实现不同型的图片展示。 其次,添加复选框可以使用自定义代理来实现。可以通过重载QItemDelegate的paint和editorEvent方法来实现在表格显示复选框,并且可以通过信号和槽来处理复选框的状态变化。 要在QTableView使用下拉框,可以通过自定义QComboBoxDelegate来实现下拉框的展示,可以通过paint方法在表格显示下拉框,同时需要重载editorEvent方法来处理下拉框的交互事件。 添加按钮可以通过自定义QButtonDelegate来实现,通过paint方法在表格显示按钮,同时需要重载editorEvent方法来处理按钮的交互事件。 要添加滑条,可以通过自定义QSliderDelegate来实现。在表格通过paint方法显示滑条,并通过editorEvent处理滑条的交互事件。 为了在QTableView添加微调框,可以通过自定义QSpinBoxDelegate来实现。通过paint方法在表格显示微调框,并需要通过editorEvent处理微调框的交互事件。 最后,要添加日历控件,可以通过自定义QCalendarDelegate来实现。通过paint方法在表格显示日历控件,并且通过editorEvent来处理日历的交互事件。 总的来说,通过自定义代理和数据模型,我们可以方便地向QTableView添加图片、复选框、下拉框、按钮、滑条、微调框和日历等控件,实现丰富多彩的表格展示效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

VectorAL

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

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

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

打赏作者

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

抵扣说明:

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

余额充值