[Solved] Display text and image in Tableview



  • I have a QAbstractTableModel and use QML's TableView to display it.

    • Roles are used to determine the column to display.
    • itemDelegate is used for the TableView cells

    Depending on the information in table, for some cells a icon has to display beside the text.
    What is a approach to transfer:

    a text from the data model

    a status value from the data model

    My first approaches were:

    • return value like: "Text:"text to display"Icon:TheImage.png" --> requires text parsing in qml file
    • overlay two TableViews: one for text, one for icons --> both icons's size and text size does not influence each other
      So I dismiss these.


  • Solution is to use a QMap containing the different values for text and icon.

    • in the datatype:

    @
    const QMap<QString,QVariant>& toMap()
    {
    QMap result;
    result.insert("text", m_text);
    ....
    return result;
    }
    @

    • In the model:

    @
    QVariant MyDataModel::data(const QModelIndex& index, int role) const
    {
    ...
    CellData myData; // Value for the current table cell
    return QVariant(myData.toMap());
    }
    @

    • in the qml

    @
    itemDelegate:
    Item {
    ...
    Text {
    ...
    text: styleData.value["text"]
    ...
    }
    Image {
    ...
    source: styleData.value["image"]
    ...
    }
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.