QT代理Delegates使用实例

效果如下,在表格的单元格中插入控件,用Delegates方式实现








源代码如下:

main.cpp文件

#include
#include
#include
#include
#include
#include "datedelegate.h"
#include "combodelegate.h"
#include "spindelegate.h"


int main(int argc,char *argv[])
{
    QApplication app(argc,argv);


    QStandardItemModel model(4,4);
    QTableView tableView;
    tableView.setModel(&model);


    DateDelegate dateDelegate;
    tableView.setItemDelegateForColumn(1,&dateDelegate);


    ComboDelegate comboDelegate;
    tableView.setItemDelegateForColumn(2,&comboDelegate);


    SpinDelegate spinDelegate;
    tableView.setItemDelegateForColumn(3,&spinDelegate);


    model.setHeaderData(0,Qt::Horizontal,QObject::tr("Name"));
    model.setHeaderData(1,Qt::Horizontal,QObject::tr("Birthday"));
    model.setHeaderData(2,Qt::Horizontal,QObject::tr("Job"));
    model.setHeaderData(3,Qt::Horizontal,QObject::tr("Income"));


    QFile file("test.tab");
    if(file.open(QFile::ReadOnly|QFile::Text))
    {
        QTextStream stream(&file);
        QString line;


        model.removeRows(0,model.rowCount(QModelIndex()),QModelIndex());
        int row =0;
        do{
               line = stream.readLine();
               if(!line.isEmpty())
               {
                   model.insertRows(row,1,QModelIndex());


                   QStringList pieces = line.split(",",QString::SkipEmptyParts);
                   model.setData(model.index(row,0,QModelIndex()),pieces.value(0));
                   model.setData(model.index(row,1,QModelIndex()),pieces.value(1));
                   model.setData(model.index(row,2,QModelIndex()),pieces.value(2));
                   model.setData(model.index(row,3,QModelIndex()),pieces.value(3));
                   row++;
               }
        }while(!line.isEmpty());


        file.close();
    }
    tableView.setWindowTitle(QObject::tr("Delegate"));
    tableView.show();
    return app.exec();
}



datedelegate.h文件

#ifndef DATEDELEGATE_H
#define DATEDELEGATE_H


#include


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



datedelegate.cpp文件

#include "datedelegate.h"
#include


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(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(editor);
   edit->setDate(date);
}


void DateDelegate::setModelData(QWidget *editor,QAbstractItemModel *model,
                                const QModelIndex &index) const
{
    QDateTimeEdit *edit=static_cast(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);
}


combodelegate.h文件

#ifndef COMBODELEGATE_H
#define COMBODELEGATE_H


#include


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



combodelegate.cpp文件

#include "combodelegate.h"
#include


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(QString::fromLocal8Bit("工人"));
    editor->addItem(QString::fromLocal8Bit("农民"));
    editor->addItem(QString::fromLocal8Bit("医生"));
    editor->addItem(QString::fromLocal8Bit("律师"));
    editor->addItem(QString::fromLocal8Bit("军人"));
    editor->installEventFilter(const_cast(this));


    return editor;
}


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


    QComboBox *box = static_cast(editor);
    int i=box->findText(str);
    box->setCurrentIndex(i);
}


void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                 const QModelIndex &index) const
{
    QComboBox *box = static_cast(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);
}



spindelegate.h文件

#ifndef SPINDELEGATE_H
#define SPINDELEGATE_H


#include


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




spindelegate.cpp文件

#include "spindelegate.h"
#include


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,10000);
    editor->installEventFilter(const_cast(this));


    return editor;
}


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


    QSpinBox *box = static_cast(editor);
    box->setValue(value);
}


void SpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                 const QModelIndex &index) const
{
    QSpinBox *box = static_cast(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);
}





你可能感兴趣的:(QT,delegates,qt,date,stream,file)