0

Good morning,

I have some problems to create a tableview from file.

Basically I just have a Button to load csv files and I want to show the files in a QML TableView...

I think my main problem is that I have a dynamic number of columns.

TableView
{
    id: tableView

    enabled:                dynVars.csvVarTableModel.b_csvEnabled
    frameVisible:           false
    sortIndicatorVisible:   false

    model:                  dynVars.csvVarTableModel

    resources:
    {
        var roleList = dynVars.csvVarTableModel.roleStringList
        var temp = []
        for(var i=0; i<roleList.length; i++)
        {
            var role  = roleList[i]
            temp.push(columnComponent.createObject(tableView, { "role": role, "title": role}))}
            return temp
        }
    }    
}

columnComponent is just a simple TableViewColumn... I worked with the QAbstractTableModel. And i have done all the basic stuff so far i reimplemented the following funtions:

public:
        int         rowCount    (const QModelIndex &parent = QModelIndex()) const;
        int         columnCount (const QModelIndex &parent = QModelIndex()) const;
       // QVariant    headerData(int section, Qt::Orientation orientation, int role) const;
        QVariant    data(const QModelIndex & index, int role = Qt::DisplayRole) const;
    private:
        QHash<int, QByteArray> roleNames() const;

I tried to overload the roleNames function so that I have a role for every Column. That's how i understood the documentation...

QHash<int,QByteArray> CSVVarTableModel::roleNames() const
{
    QHash<int, QByteArray> roles = QAbstractTableModel::roleNames();
    for(int i = 0; i < m_v_headers.size();i++)
        roles[i + Qt::UserRole] = m_v_headers.at(i).toLatin1();
    return roles;
}

in data() I simply return m_vv_table.at(index.row()).at(role); if the role is one of the UserRoles....

This works fine for the first csv file I load to the table... But after that, when I want to load anotherfile it seems that the roleNames are not updated in the QML. I already tried several combinations to fix the problem... that's also the reason why i don't have a good Code example at the moment, it's all a bit mixed up...

I'm probably on the completely wrong way I can't imagine that it's so complicated to load some simple files.... that's making me crazy

It would be awesome if someone just give hint or a small example, how to load a file with dynamic amount of colums.

kind regards,

Moe

mBucks
  • 15
  • 5
  • I guess you mean the `QAbstractTableModel` because there is no `QAbstractTableView`? The `QAbstractItemModel::roleNames()` should not be changed, it just returns the display roles, not the actual column headers that are displayed with a table view, for that you should implement the `QAbstractItemModel::headerData`. Check the documentation for more info about the "header data". – xander Oct 27 '17 at 09:59
  • Hi, thanks yes your're right i mean the QAbstractTableModel... mhh I already tried that but if I remember that right the headerData wasn't called when i tried that...I'll give that another try maybe I've overlooked something – mBucks Oct 27 '17 at 10:05
  • I'm not sure if the QML TableView makes use of it, but usually it should, because that is the correct way to provide the column header names. Maybe create a small new project to test only the `TableView` with a simple `QAbstractTableModel` and see how they work together. I don't have access to a Qt dev environment atm to test that sadly. – xander Oct 27 '17 at 10:12
  • Mhh thanks for your advise... Now i have a small project for the Table. But unfortunately i don't think that headerData will fix the problem for QML... According to the "wonderful" documentation i think i have to define a role for every column... _The header sections are attached to values in the model by defining the model role they attach to. Each property in the model will then be shown in their corresponding column._ – mBucks Oct 27 '17 at 13:00

1 Answers1

1

You can dynamically add columns on model change event.

Example from my project:

            onModelChanged: {
            for(var index = tableView.columnCount-1; index>=0; index--) {
                tableView.removeColumn(index)
            }
            for(var i = 0; i< model.columnCount(); i++) {
                tableView.addColumn(columnComponent.createObject(
                    {                                                                        
                        "title":model.headerData(i, 1).toString(),                                                                       
                        "role":model.headerData(i, 1).toString(),                                                                        
                        "delegate": textDelegate,
                        "movable": false
                    })
                )
            }

        }

All other information about using c++ model in qml is in Qt documentation.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Andrey Semenov
  • 901
  • 11
  • 17