C++ qml tablemodel
时间: 2023-10-08 20:11:48 浏览: 175
如果你想在C++中创建一个用于QML的TableModel,可以遵循以下步骤:
1. 创建一个继承自 QAbstractTableModel 的 C++ 类,例如 MyTableModel。
```cpp
#ifndef MYTABLEMODEL_H
#define MYTABLEMODEL_H
#include <QAbstractTableModel>
#include <QList>
class MyTableModel : public QAbstractTableModel
{
Q_OBJECT
public:
explicit MyTableModel(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;
private:
struct Item {
QString name;
int age;
QString gender;
};
QList<Item> m_items;
};
#endif // MYTABLEMODEL_H
```
2. 在实现文件中,实现 MyTableModel 类的函数。
```cpp
#include "mytablemodel.h"
MyTableModel::MyTableModel(QObject *parent) : QAbstractTableModel(parent)
{
// 初始化数据
m_items.append({"张三", 25, "男"});
m_items.append({"李四", 30, "女"});
m_items.append({"王五", 35, "男"});
}
int MyTableModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_items.count();
}
int MyTableModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return 3; // 列数为3
}
QVariant MyTableModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole) {
const Item &item = m_items.at(index.row());
if (index.column() == 0)
return item.name;
else if (index.column() == 1)
return item.age;
else if (index.column() == 2)
return item.gender;
}
return QVariant();
}
QVariant MyTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal) {
if (section == 0)
return "姓名";
else if (section == 1)
return "年龄";
else if (section == 2)
return "性别";
}
return QVariant();
}
```
3. 在 main.cpp 中将 MyTableModel 注册给 QML。
```cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "mytablemodel.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
qmlRegisterType<MyTableModel>("MyTableModel", 1, 0, "MyTableModel");
QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
```
现在,你可以在 QML 中使用 MyTableModel 了。以下是一个示例:
```qml
import QtQuick 2.15
import QtQuick.Controls 2.15
import MyTableModel 1.0
ApplicationWindow {
visible: true
width: 400
height: 300
title: "示例"
ListView {
width: 400
height: 300
model: MyTableModel {}
delegate: Row {
spacing: 10
Text {
text: model.name
width: 100
}
Text {
text: model.age
width: 100
}
Text {
text: model.gender
width: 100
}
}
}
}
```
在这个示例中,我们使用了 MyTableModel 类作为 ListView 的 model。在 delegate 中,我们可以直接访问 model 的属性,例如 name、age 和 gender。
希望这对你有帮助!如有其他问题,请随时提问。
阅读全文