Important: Please read the Qt Code of Conduct -

How to add tooltip to Tableview from C++ model

  • I am having difficulty to add tooltip that comes from C++ model in QML. Example from ( The model is defined as follows:

    #include <QtDebug>
    #include "mulmodel.h"
    MulModel::MulModel(QObject *parent) : QAbstractTableModel(parent)
    int MulModel::rowCount(const QModelIndex & /*parent*/) const
      qDebug() << "rowCount() called";
      return 5;
    int MulModel::columnCount(const QModelIndex & /* parent */) const
      qDebug() << "columnCount() called";
      return 3;
    QHash<int, QByteArray> MulModel::roleNames() const
      qDebug() << "roleNames() called";
      QHash<int, QByteArray> rn = QAbstractItemModel::roleNames();
      rn[RoleFoo] = "foo";
      rn[RoleBar] = "bar";
      rn[RoleBaz] = "baz";
      return rn;
    QVariant MulModel::data(const QModelIndex &index, int role) const
      QVariant ret = QVariant();
      qDebug() << "data() called" << index << role;
      if (index.isValid()) {
        switch(role) {
        case RoleFoo:
        case RoleBar:
        case RoleBaz:
        case Qt::ToolTipRole:
          ret.setValue(QString("This tooltip%1").arg(index.row()));
          qDebug() << "data(Invalid Role!)" << index;
      return ret;
    #ifndef MULMODEL_H
    #define MULMODEL_H
    #include <QAbstractTableModel>
    #include <QStandardItem>
    class MulModel : public QAbstractTableModel
      explicit MulModel(QObject *parent = 0);
      int rowCount(const QModelIndex &parent) const;
      int columnCount(const QModelIndex &parent) const;
      QVariant data(const QModelIndex &index, int role) const;
      QHash<int, QByteArray> roleNames() const;
      enum {
        RoleFoo = Qt::UserRole + 1,
        RoleBar = Qt::UserRole + 2,
        RoleBaz = Qt::UserRole + 3,
    #endif // MULMODEL_H
        TableView {
            anchors.centerIn: parent
            anchors.fill: parent
            model: tableModel
            TableViewColumn {
                role: "foo"
                title: "Foo"
                width: 80
            TableViewColumn {
                role: "bar"
                title: "Bar"
                width: 80
            TableViewColumn {
                role: "baz"
                title: "Baz"
                width: 80

    Can somebody help on this.

  • @milan
    I would expect your code to report data(Invalid Role!) but still set the tooltip. Is that what you want help on, or are you saying it does not set the tooltip?

  • @JonB . I would like to show "This tooltip" (for example) coming from C++ model to QML. But it is not setting tooltip while hovering the table. It displays other table cells without problem.

  • @milan
    Well let's start by: does it at least output (to debug window/console) data(Invalid Role!) when you move the mouse over an item in the view?

  • @JonB . No, it does not display data(Invalid Role!)

  • @milan
    Then, assuming you have pasted your code correctly, I don't understand/am very surprised. It would imply to me that Qt is never asking for a tooltip, which seems very odd....

    Meanwhile you can perhaps address your original question, depending on what you "C++ model" is, via . That does not answer to me why you are not presently getting ...

    EDIT Hang on! Having said you do not get data(Invalid Role!) you have just edited your code to change its behaviour on that without saying anything. Why have you done that if you say it was not displaying that message?

  • @JonB . I edited the code because while pasting the code to qt forum, I missed the break; in case Qt::ToolTipRole:. I am returning the QVariant from the QAbstractTableModel, I do not know how to use QStandardItem in this Model.

  • @milan
    Well it does help if you say you're editing instead of making me look dumb!


    1. Put a qDebug() into the case Qt::ToolTipRole:. Does that output? It should do.

    2. Assuming #1 works, you should be seeing a tooltip of This tooltip. Do you? You should also change your QString("This tooltip").arg(index.row()) as the format string is wrong for the argument --- or is that too not actually what you have in your code?

    3. Assuming #2 works, you should now fetch/generate whatever is supposed to be the correct tooltip from the model or wherever you have it to correspond to the specified index.

  • @JonB. It was just a mistake I realized later that I missed that line. But, as you suggested, I tried adding qDebug(), but it does not output the qDebug statements too.

  • @milan
    If you are saying it does not hit your case Qt::ToolTipRole: in any situation then I am out of ideas. (I assume you have ensured that your MulModel::data() is hit at all, nothing to do with tooltips, else ....! Now I wonder, can you at least confirm? I'm not a C++-er --- are you supposed to specify override for your override functions, or does that not matter?)

    Looking at the code, you print data(Invalid Role!) and return an empty QVariant for every other role. So, for example your view will never return any data to be shown to the user. Maybe in that case the tooltip does not get shown too, I don't know? Make your QVariant MulModel::data() at least functionable before proceeding.

  • @JonB . If I add following code to QML, I can also see the tooltip but in last column of the table.

                    TableViewColumn {
                        role: "toolTip"
                        title: "ToolTipTitle"
                        width: 80

    But this is not what I want, I want the tooltip to be displayed while the mouse gets hovered over table's cells.

  • This may be unrelated to your problem, but your string is missing the "%1" for the argument.

    ret.setValue(QString("This tooltip").arg(index.row()));

  • @mranger90 . Yes, I copied from other cases. I cannot display "This tooltip" also. But you are right, I missed %1 for arg.

  • @milan

    1. Once and for all, does your function MulModel::data() get hit at all? Yes or no?

    2. from the QAbstractTableModel, I do not know how to use QStandardItem in this Model.

    Why are you deriving all the way from QAbstractTableModel? If you started from, say, QStandardItemModel you would have QStandardItem and could use its setTooltip(). I believe standard advice is not to go down to QAbstractTable/Item... if you don't have to, the Standard classes do a lot of leg work for you....

  • @JonB . Yes, it gets hit many times. I changed the MulModel to TableModel. You can see here.

    And you can also see the output table UI.

  • @milan Hi,
    You can do it using a delegate and a ToolTip component:

    import QtQuick.Controls 2.2 as QC2
    TableViewColumn {
        id: titleColumn
        title: "Title"
        role: "title"
        movable: false
        resizable: false
        width: tableView.viewport.width - authorColumn.width
        delegate: QC2.ItemDelegate{
            id: lb
            hoverEnabled: true
            text: model.title
            width: parent.width
            QC2.ToolTip {
                delay: 250
                parent: lb
                visible: lb.hovered
                text: "Tooltip text" //put styleData.tooltip here

    This code is extracted from the TableView example which I have modified to add the ToolTip.