qt5的mvc实现,model总共8列数据,代理其中第7列为icon+文本,model文本颜色可设置,ico在文本之前,请思考一下,给出c++示例代码
时间: 2024-03-20 16:43:06 浏览: 165
好的,这里是Qt5的MVC实现,其中第7列为icon+文本,model文本颜色可设置,icon在文本之前的C++示例代码:
首先,我们需要自定义一个代理来实现第7列的icon+文本的显示和设置文本颜色的功能。代码如下:
```cpp
#include <QStyledItemDelegate>
class MyDelegate : public QStyledItemDelegate
{
public:
MyDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
{
if (index.column() == 6) { // 第7列
QString text = index.data(Qt::DisplayRole).toString();
QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
QRect iconRect = QRect(opt.rect.left() + 2, opt.rect.top() + 2, opt.rect.height() - 4, opt.rect.height() - 4);
QRect textRect = QRect(iconRect.right() + 4, opt.rect.top(), opt.rect.width() - iconRect.width() - 6, opt.rect.height());
painter->save();
painter->setClipRect(opt.rect);
painter->drawPixmap(iconRect, icon.pixmap(iconRect.size()));
painter->setPen(index.data(Qt::TextColorRole).value<QColor>());
painter->drawText(textRect, Qt::AlignVCenter, text);
painter->restore();
} else {
QStyledItemDelegate::paint(painter, option, index);
}
}
};
```
然后,我们需要自定义一个model来提供数据,代码如下:
```cpp
#include <QAbstractTableModel>
class MyTableModel : public QAbstractTableModel
{
public:
MyTableModel(QObject *parent = nullptr) : QAbstractTableModel(parent) {}
// 设置表头
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch (section) {
case 0:
return tr("ID");
case 1:
return tr("Name");
case 2:
return tr("Age");
case 3:
return tr("Gender");
case 4:
return tr("Address");
case 5:
return tr("Phone");
case 6:
return tr("Info");
case 7:
return tr("Remark");
default:
break;
}
}
return QVariant();
}
// 设置数据
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
{
if (role == Qt::DisplayRole) {
switch (index.column()) {
case 0:
return QString::number(index.row() + 1);
case 1:
return QString("Name%1").arg(index.row() + 1);
case 2:
return QString::number(20 + index.row());
case 3:
return index.row() % 2 == 0 ? tr("Male") : tr("Female");
case 4:
return tr("Address%1").arg(index.row() + 1);
case 5:
return tr("Phone%1").arg(index.row() + 1);
case 6:
return QVariant::fromValue(QIcon(":/icon.png"), QString("Info%1").arg(index.row() + 1));
case 7:
return tr("Remark%1").arg(index.row() + 1);
default:
break;
}
} else if (role == Qt::TextColorRole && index.column() == 7) {
return QColor(Qt::red);
} else if (role == Qt::DecorationRole && index.column() == 6) {
return QVariant::fromValue(QIcon(":/icon.png"));
}
return QVariant();
}
// 设置行数
int rowCount(const QModelIndex &parent = QModelIndex()) const override
{
return 10;
}
// 设置列数
int columnCount(const QModelIndex &parent = QModelIndex()) const override
{
return 8;
}
};
```
最后,我们在主窗口中使用自定义的代理和model来显示数据,代码如下:
```cpp
#include <QTableView>
#include <QVBoxLayout>
#include "mydelegate.h"
#include "mytablemodel.h"
class MainWindow : public QMainWindow
{
public:
MainWindow(QWidget *parent = nullptr) : QMainWindow(parent)
{
QTableView *tableView = new QTableView(this);
tableView->setItemDelegateForColumn(6, new MyDelegate(tableView));
tableView->setModel(new MyTableModel(tableView));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(tableView);
QWidget *widget = new QWidget(this);
widget->setLayout(layout);
setCentralWidget(widget);
}
};
```
这样就完成了Qt5的MVC实现,其中第7列为icon+文本,model文本颜色可设置,icon在文本之前的功能。
阅读全文