1. 程式人生 > >QT tableview內建控制元件

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中進行相應修改即可