qt5的mvc实现,model总共8列数据,代理其中第7列为icon+文本,请思考一下,给出c++代码示例
时间: 2024-03-20 19:41:30 浏览: 61
Qt5实现的一个mvc 的例子
好的,以下是一个简单的 Qt5 MVC 实现,其中包括一个模型(Model)、一个代理(Delegate)和一个视图(View)。模型中包含8列数据,第7列为图标和文本,代理用于呈现第7列数据。
Model.h:
```cpp
#ifndef MODEL_H
#define MODEL_H
#include <QAbstractTableModel>
#include <QList>
class Model : public QAbstractTableModel
{
public:
Model(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
private:
QList<QList<QVariant>> m_data;
};
#endif // MODEL_H
```
Model.cpp:
```cpp
#include "Model.h"
#include <QIcon>
Model::Model(QObject *parent)
: QAbstractTableModel(parent)
{
// 初始化模型数据
QList<QVariant> row;
row << 1 << "Item 1" << "Description 1" << "Value 1" << "Note 1" << "Tag 1" << QIcon(":/icons/icon1.png") << "Status 1";
m_data << row;
row.clear();
row << 2 << "Item 2" << "Description 2" << "Value 2" << "Note 2" << "Tag 2" << QIcon(":/icons/icon2.png") << "Status 2";
m_data << row;
}
int Model::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_data.size();
}
int Model::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return 8;
}
QVariant Model::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole)
return m_data[index.row()][index.column()];
else if (role == Qt::DecorationRole && index.column() == 6)
return m_data[index.row()][index.column()].value<QIcon>();
else
return QVariant();
}
QVariant Model::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
switch (section) {
case 0:
return QString("ID");
case 1:
return QString("Item");
case 2:
return QString("Description");
case 3:
return QString("Value");
case 4:
return QString("Note");
case 5:
return QString("Tag");
case 6:
return QString("Icon");
case 7:
return QString("Status");
default:
return QVariant();
}
}
return QVariant();
}
Qt::ItemFlags Model::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return QAbstractTableModel::flags(index);
}
```
Delegate.h:
```cpp
#ifndef DELEGATE_H
#define DELEGATE_H
#include <QStyledItemDelegate>
class Delegate : public QStyledItemDelegate
{
public:
Delegate(QObject *parent = nullptr);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
};
#endif // DELEGATE_H
```
Delegate.cpp:
```cpp
#include "Delegate.h"
#include <QComboBox>
#include <QLineEdit>
#include <QPainter>
Delegate::Delegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
QWidget *Delegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (index.column() == 6) {
QComboBox *editor = new QComboBox(parent);
editor->addItem(QIcon(":/icons/icon1.png"), "Icon 1");
editor->addItem(QIcon(":/icons/icon2.png"), "Icon 2");
editor->addItem(QIcon(":/icons/icon3.png"), "Icon 3");
return editor;
} else {
return QStyledItemDelegate::createEditor(parent, option, index);
}
}
void Delegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if (index.column() == 6) {
QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
comboBox->setCurrentIndex(comboBox->findText(index.data().toString()));
} else {
QStyledItemDelegate::setEditorData(editor, index);
}
}
void Delegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if (index.column() == 6) {
QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
model->setData(index, comboBox->currentText());
} else {
QStyledItemDelegate::setModelData(editor, model, index);
}
}
void Delegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (index.column() == 6) {
editor->setGeometry(option.rect);
} else {
QStyledItemDelegate::updateEditorGeometry(editor, option, index);
}
}
void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (index.column() == 6) {
QIcon icon = index.data(Qt::DecorationRole).value<QIcon>();
QString text = index.data(Qt::DisplayRole).toString();
QRect rect = option.rect;
QRect iconRect(rect.left() + 2, rect.top() + 2, 16, 16);
QRect textRect(rect.left() + 20, rect.top(), rect.width() - 20, rect.height());
icon.paint(painter, iconRect, Qt::AlignLeft | Qt::AlignVCenter);
painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, text);
} else {
QStyledItemDelegate::paint(painter, option, index);
}
}
```
View.h:
```cpp
#ifndef VIEW_H
#define VIEW_H
#include <QTableView>
class View : public QTableView
{
public:
View(QWidget *parent = nullptr);
private:
void setupModel();
void setupDelegate();
};
#endif // VIEW_H
```
View.cpp:
```cpp
#include "View.h"
#include "Model.h"
#include "Delegate.h"
View::View(QWidget *parent)
: QTableView(parent)
{
setSelectionMode(QAbstractItemView::SingleSelection);
setSelectionBehavior(QAbstractItemView::SelectRows);
setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked);
setAlternatingRowColors(true);
setupModel();
setupDelegate();
}
void View::setupModel()
{
Model *model = new Model(this);
setModel(model);
}
void View::setupDelegate()
{
Delegate *delegate = new Delegate(this);
setItemDelegate(delegate);
}
```
在这个示例中,我们为第7列数据创建了一个自定义代理。代理中创建了一个`QComboBox`,其中包含三个图标和文本,用于选择图标。在`paint()`方法中,代理将图标和文本呈现在单元格中。在`createEditor()`、`setEditorData()`和`setModelData()`方法中,代理将`QComboBox`与模型中的数据进行交互。
请注意,此示例中使用的图标应该存储在项目资源中,可以使用Qt的资源系统进行管理。
阅读全文