PDA

View Full Version : Tooltip on item in tableview



steg90
16th May 2007, 11:32
Hi,

Is it possible to display tooltips when hovering over items in the tableview?

Regards,
Steve

high_flyer
16th May 2007, 11:37
QTableWidgetItem::setToolTip()

wysota
16th May 2007, 11:39
or QAbstractItemModel::setData with Qt::ToolTipRole.

steg90
16th May 2007, 11:46
Hi,

setData, would I use this in my custom model? I'm presuming that I can then do a comparison in my data function in my model to see if the role is Qt::ToolTipRole ?


Snippet below :


QVariant DACanTreeModel::data(const QModelIndex &index, int role) const
{
QVariant data;
if( role == Qt::ToolTipRole) // do whatever



If so, what do I do?

Regards,
Steve

marcel
16th May 2007, 12:02
Yes, like that.
From data you return whatever text you want the tooltip to display.

Regards

steg90
16th May 2007, 12:05
Thanks,

Ok, but I don't call setData anywhere in my model, I just update the data structures it uses. Will the role be Qt::ToolTipRole at some point or do I need to use setData for this to happen??

Regards,
Steve

wysota
16th May 2007, 12:34
The default delegate will ask the model for tooltip. If you return one, it'll display it.

steg90
16th May 2007, 13:20
Hi,

I thought this also, but no tooltip is displayed?

In my data method I just did this so I thought the tooltip would be shown as 'Signal', but nothing appears.




if( role == Qt::ToolTipRole )
{
data = "Signal";
}



Regards,
Steve

wysota
16th May 2007, 13:31
return "Signal" and not data = "Signal". What is "data" anyway?

steg90
16th May 2007, 13:52
Hi,

Sorry wysota, data is QVariant and at the bottom of the function I return data. I set a breakpoint in my data method to break when the role is Qt::ToolTipRole, but it never breaks.

Regards,
Steve

wysota
16th May 2007, 13:58
It should if you didn't change the delegate. Could you show us the complete data() method?

steg90
16th May 2007, 14:09
Sure, here it is :



QVariant DACanTreeModel::data(const QModelIndex &index, int role) const
{
QVariant data;

if (!index.isValid())
return QVariant();
if(index.column() > 4 )
{
return QVariant();
}
if( role == Qt::ToolTipRole )
{
data = "Signal";
}
if( role == Qt::DisplayRole )
{
CDADcb::CSignal* pSignals;
pSignals = (CDADcb::CSignal*)theApp->m_dcb.GetSignalList()->at(index.row());
switch( index.column() )
{
case SIGNALNAME : {
data = pSignals->name;
break;
}
case DATA : {
data = pSignals->m_strRawData;
break;
}
case UNIT : {
data = pSignals->unit;
}
break;
case COUNT : {
data = pSignals->m_nCount;
}
break;
}
}
return data;
}


Regards,
Steve

steg90
16th May 2007, 14:13
Sorry about this, the tool tip is now displaying...unfortunately it won't go away unless I click on the table...even if I move the mouse it won't disappear?!

Regards,
Steve

wysota
16th May 2007, 14:28
It could be wise to sometimes returning something else than the same text :)

steg90
16th May 2007, 14:39
:D :D

I guess it is! It does now work. I must admit, I'd prefer a custom tooltip. Is it possible to set up a different delegate so when user hovers over an item, a custom tooltip appears instead of the standard one?

Kind regards,
Steve

wysota
16th May 2007, 17:34
What kind of custom tooltip? You can use rich text in your tooltips if you want. Should be sufficient in most cases. See attachment for one of my tooltips.

steg90
17th May 2007, 07:49
Thanks,

That would be fine, this standard QToolTip?

Regards,
Steve

wysota
17th May 2007, 09:23
Yes, stardard tooltip formatted with html as a table.

steg90
17th May 2007, 09:55
Ok, so I could just display the tooltip with the QToolTip::showText method? Do I just format the text as html in the QString?

Regards,
Steve

wysota
17th May 2007, 09:57
You can just set the appropriate tooltip in your model and Qt will display it. No need to mangle the default tooltip system.Yes, just use the subset of HTML supported by Qt.

steg90
17th May 2007, 10:49
One more thing...is it possible to fade the tooltip in and out?

Regards,
Steve

marcel
17th May 2007, 10:58
Yes, but you will have to paint it yourself.
You will have to use a QTimer ( for fading effect ) in conjunction with QPixmap::grabWidget.

Once you get the pixmap, you can play with it's transparency, painting it as you increase or decrease the alpha component.

wysota
17th May 2007, 11:03
Or you can take a look at qeffects.h and/or use a style that fades tooltips (using qeffects, by the way).