以下代码:可以在view 和model文章中进行添加
m_model = new QStandardItemModel(2, FixedColumnCount, this);
m_selection = new QItemSelectionModel(m_model, this);
ui->tableView->setModel(m_model); //设置模型
ui->tableView->setSelectionModel(m_selection); //设置选择的模型
ui->tableView->setSelectionMode(QAbstractItemView::ExtendedSelection); //设置选择的方式
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectItems); //设置选择行为
connect(m_selection, &QItemSelectionModel::currentChanged, this, &MainWindow::do_currentChanged);
intSpinDelegate = new TSpinBoxDelegate(this);
ui->tableView->setItemDelegateForColumn(0, intSpinDelegate); //给第几列设置属性
floatSpinDelegate = new TFloatSpinboxDelegate(this);
ui->tableView->setItemDelegateForColumn(1, floatSpinDelegate);
comDelegate = new TComBoxDelegate(this);
QStringList strlist;
strlist << "优"
<< "良"
<< "中"
<< "差";
comDelegate->setItems(strlist, false); // com添加item并且不可以编辑
ui->tableView->setItemDelegateForColumn(2, comDelegate);
#include "tcomboxdelegate.h"
#include
TComBoxDelegate::TComBoxDelegate(QObject* parent)
: QStyledItemDelegate { parent }
{
}
QWidget* TComBoxDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QComboBox* editor = new QComboBox(parent);
editor->setEditable(m_editable);
qDebug() << m_itemList.size();
for (auto item : m_itemList) {
editor->addItem(item);
}
return editor;
}
void TComBoxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{
QComboBox* box = dynamic_cast(editor); //把editor转为指向QSpinBox的指针 动态强转
QString value = index.model()->data(index, Qt::DisplayRole).toString();
box->setCurrentText(value);
}
void TComBoxDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
QComboBox* box = dynamic_cast(editor); //把editor转为指向QSpinBox的指针 动态强转
QString value = box->currentText();
model->setData(index, value);
}
void TComBoxDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
editor->setGeometry(option.rect);
}
void TComBoxDelegate::setItems(QStringList list, bool editabale)
{
m_itemList = list;
m_editable = editabale;
}
#ifndef TCOMBOXDELEGATE_H
#define TCOMBOXDELEGATE_H
#include
#include
class TComBoxDelegate : public QStyledItemDelegate {
Q_OBJECT
public:
explicit TComBoxDelegate(QObject* parent = nullptr);
// QAbstractItemDelegate interface
public:
virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
virtual void setEditorData(QWidget* editor, const QModelIndex& index) const override;
virtual void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override;
virtual void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
void setItems(QStringList list, bool editabale);
private:
QStringList m_itemList;
bool m_editable;
};
#endif // TCOMBOXDELEGATE_H
#include "tfloatspinboxdelegate.h"
#include
TFloatSpinboxDelegate::TFloatSpinboxDelegate(QObject* parent)
: QStyledItemDelegate { parent }
{
}
QWidget* TFloatSpinboxDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
//创建
QDoubleSpinBox* editor = new QDoubleSpinBox(parent);
editor->setFrame(true);
editor->setMinimum(0);
editor->setMaximum(150);
return editor;
}
void TFloatSpinboxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const
{ //设置值
QDoubleSpinBox* box = dynamic_cast(editor); //把editor转为指向QSpinBox的指针 动态强转
double value = index.model()->data(index, Qt::DisplayRole).toDouble();
box->setValue(value);
}
void TFloatSpinboxDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{ //将值设置给模型
QDoubleSpinBox* box = static_cast(editor); //静态强转
double value = box->value();
model->setData(index, value);
}
void TFloatSpinboxDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
editor->setGeometry(option.rect);
}
#ifndef TFLOATSPINBOXDELEGATE_H
#define TFLOATSPINBOXDELEGATE_H
#include
#include
class TFloatSpinboxDelegate : public QStyledItemDelegate {
Q_OBJECT
public:
explicit TFloatSpinboxDelegate(QObject* parent = nullptr);
// QAbstractItemDelegate interface
public:
virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
virtual void setEditorData(QWidget* editor, const QModelIndex& index) const override;
virtual void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override;
virtual void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
};
#endif // TFLOATSPINBOXDELEGATE_H