qt5的mvc模型视图代理实现,9列数据,第7列包含ico图片,其中视图只展示当前20行数据,且3,8,9列初始化数据为空,收到数据后3,8,9列才有值,当收到当前页的某行数据时,当前行变颜色。请思考一下,给出代码用例。
时间: 2024-03-16 18:44:36 浏览: 62
这里是一个简单的示例代码,实现了Qt5中的MVC模型视图代理,并展示了包含ico图片的第7列,同时初始化了第3、8、9列的数据为空。当收到当前页的某行数据时,该行变为蓝色。
```cpp
// Model
class MyModel : public QAbstractTableModel {
public:
MyModel(QObject *parent = nullptr) : QAbstractTableModel(parent) {}
int rowCount(const QModelIndex &parent = QModelIndex()) const override { return m_data.size(); }
int columnCount(const QModelIndex &parent = QModelIndex()) const override { return 9; }
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override {
if (!index.isValid() || role != Qt::DisplayRole && role != Qt::DecorationRole) return QVariant();
const auto &rowData = m_data[index.row()];
if (index.column() == 6) {
if (role == Qt::DecorationRole) return rowData.icon;
} else {
return rowData.values[index.column()];
}
return QVariant();
}
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override {
if (orientation != Qt::Horizontal || role != Qt::DisplayRole) return QVariant();
switch (section) {
case 0: return "#";
case 1: return "Name";
case 2: return "Age";
case 3: return "Address";
case 4: return "Phone";
case 5: return "Email";
case 6: return "Icon";
case 7: return "Note1";
case 8: return "Note2";
default: return QVariant();
}
}
void setPageData(const QVector<RowData> &newData) { m_data = newData; }
void setRowColor(const QModelIndex &index, const QColor &color) {
Q_EMIT dataChanged(index, index, {Qt::BackgroundRole});
m_rowColors[index.row()] = color;
}
QColor getRowColor(int row) const {
if (m_rowColors.contains(row)) return m_rowColors[row];
return QColor();
}
private:
QVector<RowData> m_data;
QMap<int, QColor> m_rowColors;
};
// Delegate
class MyDelegate : public QStyledItemDelegate {
public:
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override {
if (index.column() == 6) {
const auto &icon = index.data(Qt::DecorationRole).value<QIcon>();
if (!icon.isNull()) {
const auto rect = option.rect;
const auto iconSize = icon.actualSize(rect.size());
const auto x = rect.center().x() - iconSize.width() / 2;
const auto y = rect.center().y() - iconSize.height() / 2;
painter->drawPixmap(x, y, icon.pixmap(iconSize));
return;
}
}
QStyledItemDelegate::paint(painter, option, index);
}
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override {
if (index.column() == 6) {
const auto &icon = index.data(Qt::DecorationRole).value<QIcon>();
if (!icon.isNull()) {
return icon.actualSize(option.rect.size());
}
}
return QStyledItemDelegate::sizeHint(option, index);
}
};
// View
class MyTableView : public QTableView {
public:
MyTableView(QWidget *parent = nullptr) : QTableView(parent) {
const auto model = new MyModel(this);
setModel(model);
const auto delegate = new MyDelegate(this);
setItemDelegate(delegate);
setShowGrid(false);
setAlternatingRowColors(true);
setEditTriggers(QAbstractItemView::NoEditTriggers);
// 初始化数据
QVector<RowData> data;
for (int i = 0; i < 100; ++i) {
auto rowData = RowData{i + 1, QString("name%1").arg(i+1), i+20, "address", "phone", "email"};
if (i % 2 == 0) rowData.icon = QIcon(":/icons/person.png");
data.append(rowData);
}
model->setPageData(data);
resizeColumnsToContents();
setColumnWidth(6, 50);
// 当收到数据时,设置第3、8、9列的值,并且设置当前行颜色为蓝色
connect(this, &MyTableView::clicked, [=](const QModelIndex &index) {
if (!index.isValid()) return;
auto model = qobject_cast<MyModel *>(this->model());
auto data = model->getPageData();
if (index.row() >= data.size()) return;
data[index.row()].values[2] = 30;
data[index.row()].values[7] = "note1";
data[index.row()].values[8] = "note2";
model->setPageData(data);
model->setRowColor(index, Qt::blue);
});
}
};
// Usage
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MyTableView view;
view.show();
return app.exec();
}
```
注意:这里的代码只是一个示例,实际使用时需要根据自己的需求进行修改。
阅读全文