QT tableview内置控件

为什么需要内置控件

tableview 默认的内置控件是QLineEdit,但是实际使用时,我们常常会有特殊需求,例如对QLineEdit有字数限制,性别有固定的选项等等,因此我们需要自定义tableview的内置控件

代码

下面的例子中,我使用了两个内置控件,你们可以根据需要自行修改,依旧先上运行图,再上代码
此处对性别栏内置了combo,对爱好栏内置了QTextEdit
在这里插入图片描述
tabviewDelegate.h

#include <QItemDelegate>

#include <QComboBox>
#include <QTextEdit>
class tabviewDelegate: public QItemDelegate
{
    Q_OBJECT
public:
    tabviewDelegate(QObject *parent = 0);

public:
    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;

    void updateEditorGeometry(QWidget *editor,

                              const QStyleOptionViewItem &option, const QModelIndex &index) const;

public:
    QString type;//根据type来控制选中时激活的是那种控件

};

tabviewDelegate.cpp

#include "tabviewDelegate.h"

#include <QDebug>
tabviewDelegate::tabviewDelegate(QObject *parent)
{

}


QWidget *tabviewDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    qDebug()<<"createEditor";
    if(type == "sex")
    {
        QComboBox *editor = new QComboBox(parent);
        editor->addItem(tr("男"));
        editor->addItem(tr("女"));
        return editor;
    }
    else
    {
        QTextEdit *editor = new QTextEdit(parent);
        return editor;
    }

}

//设置editor数据
void tabviewDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    qDebug()<<"setEditorData";

    QString text =index.model()->data(index,Qt::DisplayRole).toString();
    if(type == "sex")
    {
        QComboBox *cmb = static_cast<QComboBox*>(editor);
        cmb->setCurrentText(text);
    }
    else
    {
        QTextEdit *textedit = static_cast<QTextEdit*>(editor);
        textedit->setText(text);
    }
}

//设置model数据
void tabviewDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    qDebug()<<"setModelData";
    QString text;
    if(type == "sex")
    {
        QComboBox *cmb = static_cast<QComboBox*>(editor);
        text= cmb->currentText();
    }
    else
    {
        QTextEdit *edit = static_cast<QTextEdit*>(editor);
        text= edit->toPlainText();
    }
    model->setData(index,text);
}

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

MainWindow.cpp

#include <tabviewDelegate.h>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
	 tabviewDelegate *textedit = new tabviewDelegate(this);
    textedit->type = "hobby";
    ui->tableView->setItemDelegateForColumn(5,textedit);//第五列,0开始计数

    tabviewDelegate *cmb = new tabviewDelegate(this);
    cmb->type = "sex";
    ui->tableView->setItemDelegateForColumn(2,cmb);
}

还有一些话要说

如果想要实现tableview的某列不可修改,也可以使用内置控件来实现,在createEditor函数中,改成

if(type == "sex")
    {
        QComboBox *editor = new QComboBox(parent);
        editor->addItem(tr("男"));
        editor->addItem(tr("女"));
        editor->setEnabled(false);//不可修改
        return editor;
    }

在这里插入图片描述
其他的类似设置输入长度等操作,只需要在createEditor中进行相应修改即可

  • 6
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值