Qt World Summit: Register Today!

[QML] Access the value of a different column in TableView

  • I have the following code:

    TableView {
                    id: table
                    width:parent.width *0.95
                    anchors.horizontalCenter: parent.horizontalCenter
                    anchors.topMargin: 10
                    selectionMode: SelectionMode.SingleSelection
                    itemDelegate :editableDelegate
                    TableViewColumn{ role: "min"  ; title: "Nb. min" ;width:(tableauTours.width/5); delegate: minComponent}
                    TableViewColumn{ role: "max" ; title: "Nb. max" ;width:(tableauTours.width/5); delegate: maxComponent}

    Inside the component i found that i could have access (in the component) to the current data ( styleData.value ) , the current row index ( styleData.row ) and the current column index ( styleData.column) .
    However, if i'm clicking on the minComponent for example, i can have access the the min value, but how can i have access to the max value of the current row ?

    Thanks for reading

  • Oh and by the way the model for the TableView is added dynamically when a button is clicked

  • Moderators

    Hi @Kaxu,
    Access it from model instead of delegate as you already have row.

  • @p3c0

    When i'm trying :

    I get the following output :

    TypeError: Property 'get' of object SqlQueryModel(0x2599230) is not a function

    This is because the model come from a SqlDatabase i got from a class i created called SqlQueryModel that inherit from QSqlQueryModel.

    What can I do ?

  • Moderators

    Using get wont work here. Since you have your own implemented model you will need to provide your own method in it which will return data at that row. Make this method Q_INVOKABLE to be able to access it from QML.

  • Ok, here is the code i implemented :

    In SqlQueryModel.h
    Q_INVOKABLE QVariant data(const QModelIndex &index, int role) const;

    In SqlQueryModel.cpp

    QVariant SqlQueryModel::data(const QModelIndex &index, int role) const
    QVariant value = QSqlQueryModel::data(index, role);
    if(role < Qt::UserRole)
    value = QSqlQueryModel::data(index, role);
    int columnIdx = role - Qt::UserRole - 1;
    QModelIndex modelIndex = this->index(index.row(), columnIdx);
    value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
    qDebug() << "Error: " << lastError();
    return value;

    And here is my qml code :

    but it keep returning "undefined", do you know why ?

  • Moderators

    @Kaxu data is already a method in QSqlQueryModel. Moreover QML won't understand QModelIndex too.
    Instead create a separate Q_INVOKABLE method. Since you are using QSqlQueryModel you can use record to get row at particular index and then extract the data
    For eg:

    //in header file
    Q_INVOKABLE QString getData(int row);
    //in source
    QString getData(int row)
        return record(row).value(0).toString(); //will fetch row at index row and get data of particular database column

    Then call this from QML using model property

    table.model.getData(3) //get data at row 3

  • Wow this actually works ...
    Thanks a lot p3c0 you're the best !!!

    Do you work for Qt or something ?

  • Moderators

    @Kaxu Thanks :).
    No, Just a normal Qt addicted user :)

Log in to reply