我想为我的 QtableView 标题项之一提供动态工具提示。
工具提示的内容随着 alt 键的按下而改变。我在以下站点中找到了一些与使用委托相关的代码:QTreeView 中截断项目的工具提示
在类似的行中,我尝试了以下代码:
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 );
}
然后我尝试为 headerView 设置委托horizontalHeader()->setItemDelegate( new HeaderItemDelegate )
。但它没有用。
似乎 ItemDelgate 不适用于 headerViews。我在 QHeaderView 中找不到与工具提示相关的任何事件。
实现这一目标的正确方法是什么?