How to add tooltip to Tableview from C++ model
-
@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? -
@milan
Well it does help if you say you're editing instead of making me look dumb!So:
-
Put a
qDebug()
into thecase Qt::ToolTipRole:
. Does that output? It should do. -
Assuming #1 works, you should be seeing a tooltip of
This tooltip
. Do you? You should also change yourQString("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? -
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.
-
-
@milan
If you are saying it does not hit yourcase Qt::ToolTipRole:
in any situation then I am out of ideas. (I assume you have ensured that yourMulModel::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 specifyoverride
for your override functions, or does that not matter?)Looking at the code, you print
data(Invalid Role!)
and return an emptyQVariant
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 yourQVariant 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.
-
-
Once and for all, does your function
MulModel::data()
get hit at all? Yes or no? -
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 haveQStandardItem
and could use itssetTooltip()
. I believe standard advice is not to go down toQAbstractTable/Item...
if you don't have to, theStandard
classes do a lot of leg work for you.... -
-
@milan Hi,
You can do it using a delegate and aToolTip
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.