Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to add tooltip to Tableview from C++ model

How to add tooltip to Tableview from C++ model

Scheduled Pinned Locked Moved Unsolved General and Desktop
tooltipqmlmodel-view
16 Posts 4 Posters 5.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M milan
    13 Sept 2018, 12:33

    @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.

    J Offline
    J Offline
    JonB
    wrote on 13 Sept 2018, 12:35 last edited by
    #4

    @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?

    M 1 Reply Last reply 13 Sept 2018, 12:37
    0
    • J JonB
      13 Sept 2018, 12:35

      @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?

      M Offline
      M Offline
      milan
      wrote on 13 Sept 2018, 12:37 last edited by
      #5

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

      J 1 Reply Last reply 13 Sept 2018, 12:43
      0
      • M milan
        13 Sept 2018, 12:37

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

        J Offline
        J Offline
        JonB
        wrote on 13 Sept 2018, 12:43 last edited by JonB
        #6

        @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 http://doc.qt.io/qt-5/qstandarditem.html#setToolTip . 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?

        M 1 Reply Last reply 13 Sept 2018, 12:46
        0
        • J JonB
          13 Sept 2018, 12:43

          @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 http://doc.qt.io/qt-5/qstandarditem.html#setToolTip . 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?

          M Offline
          M Offline
          milan
          wrote on 13 Sept 2018, 12:46 last edited by
          #7

          @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.

          J 1 Reply Last reply 13 Sept 2018, 12:55
          0
          • M milan
            13 Sept 2018, 12:46

            @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.

            J Offline
            J Offline
            JonB
            wrote on 13 Sept 2018, 12:55 last edited by
            #8

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

            So:

            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.

            M 1 Reply Last reply 13 Sept 2018, 12:59
            0
            • J JonB
              13 Sept 2018, 12:55

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

              So:

              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.

              M Offline
              M Offline
              milan
              wrote on 13 Sept 2018, 12:59 last edited by milan
              #9

              @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.

              J 1 Reply Last reply 13 Sept 2018, 13:15
              0
              • M milan
                13 Sept 2018, 12:59

                @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.

                J Offline
                J Offline
                JonB
                wrote on 13 Sept 2018, 13:15 last edited by
                #10

                @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.

                M 1 Reply Last reply 13 Sept 2018, 13:18
                0
                • J JonB
                  13 Sept 2018, 13:15

                  @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.

                  M Offline
                  M Offline
                  milan
                  wrote on 13 Sept 2018, 13:18 last edited by milan
                  #11

                  @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.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mranger90
                    wrote on 13 Sept 2018, 13:24 last edited by
                    #12

                    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()));
                    
                    M 1 Reply Last reply 13 Sept 2018, 13:26
                    0
                    • M mranger90
                      13 Sept 2018, 13:24

                      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()));
                      
                      M Offline
                      M Offline
                      milan
                      wrote on 13 Sept 2018, 13:26 last edited by
                      #13

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

                      J 1 Reply Last reply 13 Sept 2018, 13:45
                      0
                      • M milan
                        13 Sept 2018, 13:26

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

                        J Offline
                        J Offline
                        JonB
                        wrote on 13 Sept 2018, 13:45 last edited by
                        #14

                        @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....

                        M 1 Reply Last reply 13 Sept 2018, 13:53
                        0
                        • J JonB
                          13 Sept 2018, 13:45

                          @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....

                          M Offline
                          M Offline
                          milan
                          wrote on 13 Sept 2018, 13:53 last edited by milan
                          #15

                          @JonB . Yes, it gets hit many times. I changed the MulModel to TableModel. You can see here.
                          0_1536846777667_ce6e5b35-821c-4644-893d-d451826660fa-image.png

                          And you can also see the output table UI.
                          0_1536846819563_795e40cf-41b8-41fa-90c8-80f9688730d6-image.png

                          1 Reply Last reply
                          0
                          • M milan
                            13 Sept 2018, 11:21

                            I am having difficulty to add tooltip that comes from C++ model in QML. Example from (https://qiita.com/illness072/items/a6f2ce9f7a1bfff44049). 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:
                                  ret.setValue(QString("Foo%1").arg(index.row()));
                                  break;
                                case RoleBar:
                                  ret.setValue(QString("Bar%1").arg(index.row()));
                                  break;
                                case RoleBaz:
                                  ret.setValue(QString("Baz%1").arg(index.row()));
                                  break;
                                case Qt::ToolTipRole:
                                  ret.setValue(QString("This tooltip%1").arg(index.row()));
                                  break;
                                default:
                                  qDebug() << "data(Invalid Role!)" << index;
                                  break;
                                }
                              }
                              return ret;
                            }
                            
                            
                            #ifndef MULMODEL_H
                            #define MULMODEL_H
                            
                            #include <QAbstractTableModel>
                            #include <QStandardItem>
                            
                            class MulModel : public QAbstractTableModel
                            {
                              Q_OBJECT
                            
                            public:
                              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;
                            
                            private:
                              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.

                            G Offline
                            G Offline
                            Gojir4
                            wrote on 13 Sept 2018, 14:08 last edited by
                            #16

                            @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.

                            1 Reply Last reply
                            1

                            13/16

                            13 Sept 2018, 13:26

                            • Login

                            • Login or register to search.
                            13 out of 16
                            • First post
                              13/16
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved