QTableWidget设置代理 添加QCombox

一、实现功能:向QTableWidget上添加QCombox
  显示效果:双击鼠标才显示Combox组件
  如下图所示:未双击效果图
  在这里插入图片描述
双击效果图:
在这里插入图片描述
二、向第二列添加combox代码如下

ui.tableWidget_TestItems->verticalHeader()->setVisible(false);//隐藏垂直表头
ui.tableWidget_TestItems->setSelectionBehavior(QAbstractItemView::SelectRows);//选择一行
setItemIsEditable(ui.tableWidget_TestItems, 0);//禁止表格编辑
setItemIsEditable(ui.tableWidget_TestItems, 2);
setItemIsEditable(ui.tableWidget_TestItems, 3);
ui.tableWidget_TestItems->setItemDelegateForColumn(1, new Delegate(this));//添加QCombox代理

三、封装Delegate类

#ifndef DELEGATE_H
#define DELEGATE_H

#include <QStyledItemDelegate>
#include <QItemDelegate>
#include <QModelIndex>
#include <QPainter>
#include <QWidget>

#define  COMBOXCOL 1
class Delegate : public QItemDelegate
{
    
    
    Q_OBJECT

public:
    Delegate(QObject *parent = nullptr);
    ~Delegate();

    void paint(QPainter *painter, const QStyleOptionViewItem &option,
                    const QModelIndex &index) const;
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;

    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                                        const QModelIndex &index) const;
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
    void setModelData(QWidget *editor, QAbstractItemModel *model,
                                    const QModelIndex &index) const;

private:

};

#endif // DELEGATE_H
#include "Delegate.h"
#include <QComboBox>

Delegate::Delegate(QObject *parent)
    : QItemDelegate(parent)
{
    
    

}

Delegate::~Delegate()
{
    
    

}
void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    const QModelIndex &index) const
{
    
    
    QItemDelegate::paint(painter, option, index);
}

QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    
    
    return QItemDelegate::sizeHint(option, index);
}

QWidget *Delegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                                                   const QModelIndex &index) const
{
    
    
    if (index.isValid() && index.column() == COMBOXCOL)
    {
    
    
        QComboBox *editor = new QComboBox(parent);
        editor->setEditable(true);
        editor->installEventFilter(const_cast<Delegate *>(this));
        return editor;
    }
    else
    {
    
    
        return QItemDelegate::createEditor(parent, option, index);
    }
}

void Delegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    
    
    if (index.isValid() && index.column() == COMBOXCOL)
    {
    
    
        QString value = index.model()->data(index, Qt::DisplayRole).toString();
        QComboBox *combox = static_cast<QComboBox *>(editor);
        combox->addItem("+");
        combox->addItem("+2");
        combox->addItem("+3");
        combox->addItem("+4");
        combox->setCurrentText(value);
    }
    else
    {
    
    
        QItemDelegate::setEditorData(editor, index);
    }
}

void Delegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                             const QModelIndex &index) const
{
    
    
    if (index.isValid() && index.column() == COMBOXCOL)
    {
    
    
        QComboBox *combox = static_cast<QComboBox *>(editor);
        model->setData(index, combox->currentText());
    }
    else
    {
    
    
        QItemDelegate::setModelData(editor, model, index);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40170041/article/details/131612581