1

I want to have a dynamic tooltip for one of my header items of a QtableView.

The content of the tooltip changes with alt key press. I found some code related to using delegates in the following site: Tooltips for truncated items in a QTreeView

In similar lines I tried the following piece of code:

class HeaderToolTipDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    HeaderToolTipDelegate( QObject* inParent );
    ~HeaderToolTipDelegate();
 public slots:
    bool helpEvent( QHelpEvent* inEvent, QAbstractItemView* inView, const QStyleOptionViewItem& inOption, const QModelIndex& inIndex );
};

bool HeaderToolTipDelegate::helpEvent
(
    QHelpEvent* inEvent,
    QAbstractItemView* inView,
    const QStyleOptionViewItem& inOption,
    const QModelIndex& inIndex
)
{
    if( !inEvent || !inView )
        return false;

    if( inEvent->type() == QEvent::ToolTip )
    {
        QVariant tooltip = inIndex.data( Qt::DisplayRole );
        if( QApplication::keyboardModifiers() == Qt::AltModifier )
        {
            QToolTip::showText( inEvent->globalPos(), QString( "AltKeyPressed" ), inView );
        }
        else
        {
            QToolTip::showText( inEvent->globalPos(), QString( "AltKeyNotPressed" ), inView );
        }

        if( !QStyledItemDelegate::helpEvent( inEvent, inView, inOption, inIndex ) )
            QToolTip::hideText();
        return true;
    }

    return QStyledItemDelegate::helpEvent( inEvent, inView, inOption, inIndex );
}

And then I tried setting the delegate for the headerView horizontalHeader()->setItemDelegate( new HeaderItemDelegate ). But it didn't work.

It seems ItemDelgate doesn't work for headerViews. I couldn't find any event related to tooltip in QHeaderView.

What is the correct way to achieve this?

sajas
  • 1,599
  • 1
  • 17
  • 39

0 Answers0