要在QML的TableView中显示一个model中的数据,你需要完成以下几个步骤:

  1. 定义一个QAbstractItemModel的子类,作为你的数据模型。在这个模型中,你需要实现rowCount()、columnCount()、data()等方法,来提供数据给TableView使用。
class MyModel : public QAbstractItemModel
{
    // ...
};
  1. 在QML中引入这个数据模型,并且创建一个TableView来显示这个模型的数据。
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

TableView {
    id: tableView
    width: 400
    height: 300
    model: myModel
    anchors.centerIn: parent
    
    TableViewColumn {
        role: "name"
        title: "Name"
        width: 150
    }
    
    TableViewColumn {
        role: "age"
        title: "Age"
        width: 100
    }
}
  1. 创建一个实例化了你的模型的对象,并且将其设置为TableView的model属性。
QQmlApplicationEngine engine;
MyModel myModel;
engine.rootContext()->setContextProperty("myModel", &myModel);

现在,当你运行这个应用程序时,TableView将显示你的模型中的数据。你可以通过在MyModel类中的相应方法中提供数据来更新TableView的内容

qml TableView显示model中的数据

原文地址: http://www.cveoy.top/t/topic/hIsE 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录