Qt工作笔记-对QItemDelegate自定义委托的理解

关键是重写这四个函数:

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;


程序运行截图如下:



代码如下:

combodelegate.h

#ifndef COMBODELEGATE_H
#define COMBODELEGATE_H

#include <QItemDelegate>

class ComboDelegate:public QItemDelegate
{
    Q_OBJECT
public:
    ComboDelegate(QObject *parent=0);
    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;
};

#endif // COMBODELEGATE_H


datedelegate.h

#ifndef DATEDELEGATE_H
#define DATEDELEGATE_H

#include <QItemDelegate>

class DateDelegate:public QItemDelegate
{
    Q_OBJECT
public:
    DateDelegate(QObject *parent=0);
    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;
};

#endif // DATEDELEGATE_H


passportdelegate.h

#ifndef PASSPORTDELEGATE_H
#define PASSPORTDELEGATE_H

#include <QItemDelegate>
#include <QLabel>

class PassportDelegate:public QItemDelegate
{
    Q_OBJECT
public:
    PassportDelegate(QObject *parent=0);
    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;
};

#endif // PASSPORTDELEGATE_H


spindelegate.h

#ifndef SPINDELEGATE_H
#define SPINDELEGATE_H

#include <QItemDelegate>

class SpinDelegate:public QItemDelegate
{
    Q_OBJECT
public:
    SpinDelegate(QObject *parent=0);
    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;
};

#endif // SPINDELEGATE_H


widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H


combodelegate.cpp

#include "combodelegate.h"
#include <QComboBox>
#include <QDebug>

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

}

QWidget *ComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QComboBox *editor=new QComboBox(parent);
    editor->addItem("程序员");
    editor->addItem("网管");
    editor->addItem("修电脑的");
    editor->addItem("送水的");
    editor->installEventFilter(const_cast<ComboDelegate*>(this));
    return editor;
}

void ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QString str=index.model()->data(index).toString();
    QComboBox *box=static_cast<QComboBox*>(editor);
    int i=box->findText(str);
    box->setCurrentIndex(i);
}

void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QComboBox *box=static_cast<QComboBox*>(editor);
    QString str=box->currentText();
    model->setData(index,str);
}

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


datedelegate.cpp

#include "datedelegate.h"
#include <QDateTimeEdit>
#include <QDebug>

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

}

QWidget *DateDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QDateTimeEdit *editor=new QDateTimeEdit(parent);
    editor->setDisplayFormat("yyyy-MM-dd");
    editor->setCalendarPopup(true);
    editor->installEventFilter(const_cast<DateDelegate*>(this));
    return editor;
}

void DateDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QString dateStr=index.model()->data(index).toString();
    QDate date=QDate::fromString(dateStr,Qt::ISODate);
    QDateTimeEdit *edit=static_cast<QDateTimeEdit*>(editor);
    edit->setDate(date);
}

void DateDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QDateTimeEdit *edit=static_cast<QDateTimeEdit*>(editor);
    QDate date=edit->date();
    model->setData(index,QVariant(date.toString(Qt::ISODate)));
}

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


main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}


passportdelegate.cpp

#include "passportdelegate.h"
#include <QCheckBox>
#include <QDebug>
#include <QFont>

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

}

QWidget *PassportDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QCheckBox *checkBox=new QCheckBox(parent);
    checkBox->setText("是否通过");
    QFont font;
    font.setBold(true);
    font.setPixelSize(18);
    checkBox->setFont(font);
    return checkBox;
}

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

    QCheckBox *checkBox=static_cast<QCheckBox*>(editor);
    if(str=="通过"){
        checkBox->setChecked(Qt::Checked);
    }
    else{
        checkBox->setChecked(Qt::Unchecked);
    }
}

void PassportDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QCheckBox *checkBox=static_cast<QCheckBox*>(editor);
    if(checkBox->isChecked()){
        model->setData(index,QString("通过"));
    }
    else{
        model->setData(index,QString("不通过"));
    }
}

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


spindelegate.cpp

#include "spindelegate.h"
#include <QSpinBox>

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

}

QWidget *SpinDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QSpinBox *editor=new QSpinBox(parent);
    editor->setRange(0,100000);
    editor->installEventFilter(const_cast<SpinDelegate*>(this));
    return editor;

}

void SpinDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    int value=index.model()->data(index).toInt();
    QSpinBox *box=static_cast<QSpinBox*>(editor);
    box->setValue(value);
}

void SpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QSpinBox *box=static_cast<QSpinBox*>(editor);
    int value=box->value();
    model->setData(index,value);
}

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


widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include "combodelegate.h"
#include "datedelegate.h"
#include "spindelegate.h"
#include "passportdelegate.h"
#include <QStandardItem>
#include <QStandardItemModel>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    QStandardItemModel *model=new QStandardItemModel;
    model->setColumnCount(5);
    ComboDelegate *comboDelegate=new ComboDelegate;
    DateDelegate *dateDelegate=new DateDelegate;
    SpinDelegate *spinDelegate=new SpinDelegate;
    PassportDelegate *passportDelegate=new PassportDelegate;
    model->setHeaderData(0,Qt::Horizontal,"姓名");
    model->setHeaderData(1,Qt::Horizontal,"职业");
    model->setHeaderData(2,Qt::Horizontal,"出生日期");
    model->setHeaderData(3,Qt::Horizontal,"薪资");
    model->setHeaderData(4,Qt::Horizontal,"审核状态");
    model->insertRow(0,new QStandardItem("猪小明"));
    ui->tableView->setModel(model);
    ui->tableView->setItemDelegateForColumn(1,comboDelegate);
    ui->tableView->setItemDelegateForColumn(2,dateDelegate);
    ui->tableView->setItemDelegateForColumn(3,spinDelegate);
    ui->tableView->setItemDelegateForColumn(4,passportDelegate);
}

Widget::~Widget()
{
    delete ui;
}

猜你喜欢

转载自blog.csdn.net/qq78442761/article/details/80817741