新建一个Delegate类继承自QStyledItemDelegate:
#ifndef QWINTSPINDELEGATE_H
#define QWINTSPINDELEGATE_H
//#include
//#include
#include
class QWIntSpinDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
QWIntSpinDelegate(QObject *parent=0);
//自定义代理组件必须继承以下4个函数
//创建编辑组件
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const Q_DECL_OVERRIDE;
//从数据模型获取数据,显示到代理组件中
void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;
//将代理组件的数据,保存到数据模型中
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const Q_DECL_OVERRIDE;
//更新代理编辑组件的大小
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
const QModelIndex &index) const Q_DECL_OVERRIDE;
};
#endif // QWINTSPINDELEGATE_H
创建代理编辑组件:
QWidget *QWIntSpinDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const
{
//创建代理编辑器
QSpinBox* editot = new QSpinBox(parent);
editot->setFrame(false);//设置为无边框
editot->setMinimum(0);
editot->setMaximum(10000);
return editot;
}
从数据模型获取数值:
void QWIntSpinDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const
{
//从数据模型获取数据显示到代理组件中
//获取模型索引指向的单元格数据
int value = index.model()->data(index, Qt::EditRole).toInt();
QSpinBox* spinBox = static_cast(editor); //强制类型转换
spinBox->setValue(value);//设置编辑器的数值
}
将代理组件的数据保存到数据模型中:
void QWIntSpinDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
//将代理组件的数据保存到数据模型中
QSpinBox* spinBox = static_cast(editor);
spinBox->interpretText();//数据被修改后触发信号
int value = spinBox->value();
model->setData(index, value, Qt::EditRole);//将从组件获取到的值更新到数据模型
}
设置组件大小:
void QWIntSpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{ //设置组件大小
editor->setGeometry(option.rect);
}
头文件:
#ifndef QWFLOATSPINDELEGATE_H
#define QWFLOATSPINDELEGATE_H
#include
#include
#include
class QWFloatSpinDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
QWFloatSpinDelegate(QObject *parent=0);
//自定义代理组件必须继承以下4个函数
//创建编辑组件
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const Q_DECL_OVERRIDE;
void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const Q_DECL_OVERRIDE;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
const QModelIndex &index) const Q_DECL_OVERRIDE;
};
#endif // QWFLOATSPINDELEGATE_H
#ifndef QWCOMBOBOXDELEGATE_H
#define QWCOMBOBOXDELEGATE_H
#include
class QWComboBoxDelegate : public QItemDelegate
{
Q_OBJECT
public:
QWComboBoxDelegate(QObject *parent=0);
//自定义代理组件必须继承以下4个函数
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const Q_DECL_OVERRIDE;
void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const Q_DECL_OVERRIDE;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
const QModelIndex &index) const Q_DECL_OVERRIDE;
};
#endif // QWCOMBOBOXDELEGATE_H
cpp文件:
#include "qwfloatspindelegate.h"
#include
QWFloatSpinDelegate::QWFloatSpinDelegate(QObject *parent):QStyledItemDelegate(parent)
{
}
QWidget *QWFloatSpinDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(option);
Q_UNUSED(index);
QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
editor->setFrame(false);
editor->setMinimum(0);
editor->setDecimals(2);
editor->setMaximum(10000);
return editor;
}
void QWFloatSpinDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
float value = index.model()->data(index, Qt::EditRole).toFloat();
QDoubleSpinBox *spinBox = static_cast(editor);
spinBox->setValue(value);
}
void QWFloatSpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QDoubleSpinBox *spinBox = static_cast(editor);
spinBox->interpretText();
float value = spinBox->value();
QString str=QString::asprintf("%.2f",value);
model->setData(index, str, Qt::EditRole);
}
void QWFloatSpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
#include "qwcomboboxdelegate.h"
#include
QWComboBoxDelegate::QWComboBoxDelegate(QObject *parent):QItemDelegate(parent)
{
}
QWidget *QWComboBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QComboBox *editor = new QComboBox(parent);
editor->addItem("优");
editor->addItem("良");
editor->addItem("一般");
editor->addItem("不合格");
return editor;
}
void QWComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString str = index.model()->data(index, Qt::EditRole).toString();
QComboBox *comboBox = static_cast(editor);
comboBox->setCurrentText(str);
}
void QWComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QComboBox *comboBox = static_cast(editor);
QString str = comboBox->currentText();
model->setData(index, str, Qt::EditRole);
}
void QWComboBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
在主窗口中:
创建数据模型和Item项选择模型:
QStandardItemModel* theModel;//数据模型
QItemSelectionModel* theSelection;//Item项选择模型
theModel = new QStandardItemModel(2,6,this);
theSelection = new QItemSelectionModel(theModel);
定义当前项变化的槽函数并进行连接:
QLabel* LabCellText;//当前单元格内容
void MainWindow::on_currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
{
Q_UNUSED(previous);
if (current.isValid())
{
LabCellPos->setText(QString::asprintf("当前单元格:%d行,%d列",
current.row(),current.column()));
QStandardItem *aItem;
aItem=theModel->itemFromIndex(current); //从模型索引获得Item
this->LabCellText->setText("单元格内容:"+aItem->text());
QFont font=aItem->font();
ui->actFontBold->setChecked(font.bold());
}
}
connect(theSelection, SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(on_currentChanged(QModelIndex, QModelIndex)));
为各列设置自定义代理组件:
QWIntSpinDelegate intSpinDelegate;//整型数
QWFloatSpinDelegate floatSpinDelegate;//浮点数
QWComboBoxDelegate comboBoxDelegate;//列表选择
ui.tableView->setItemDelegateForColumn(0, &intSpinDelegate);
ui.tableView->setItemDelegateForColumn(1,&floatSpinDelegate);
ui.tableView->setItemDelegateForColumn(1, &floatSpinDelegate);
ui.tableView->setItemDelegateForColumn(2, &floatSpinDelegate);
ui.tableView->setItemDelegateForColumn(3, &floatSpinDelegate);
ui.tableView->setItemDelegateForColumn(4,&comboBoxDelegate);