C++ 为QHeaderView项设置动态工具提示
我想为QtableView的一个标题项提供动态工具提示 按alt键时,工具提示的内容将发生更改。我在以下站点中找到了一些与使用代理相关的代码: 在类似的行中,我尝试了以下代码:C++ 为QHeaderView项设置动态工具提示,c++,qt,tooltip,qtableview,qheaderview,C++,Qt,Tooltip,Qtableview,Qheaderview,我想为QtableView的一个标题项提供动态工具提示 按alt键时,工具提示的内容将发生更改。我在以下站点中找到了一些与使用代理相关的代码: 在类似的行中,我尝试了以下代码: class HeaderToolTipDelegate : public QStyledItemDelegate { Q_OBJECT public: HeaderToolTipDelegate( QObject* inParent ); ~HeaderToolTipDelegate(); pu
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 );
}
然后我尝试为headerViewhorizontalHeader()->setItemDelegate(新HeaderItemDelegate)
设置委托。但它不起作用
看来ItemDelgate不适用于headerViews。我在QHeaderView中找不到任何与工具提示相关的事件
实现这一点的正确方法是什么?您是否尝试使用树模型的
qabstractemmodel::headerData()
函数?@vahancho谢谢。它正在使用headerData()函数。