qt-interest@trolltech.com
[Top] [All Lists]

Re: QHeader, mouse right click

Subject: Re: QHeader, mouse right click
From: Nathan Carter
Date: Wed, 16 Mar 2005 12:32:40 -0500
>> Hi,
>> 
>> Is there a solution to have a popup menu on a QHeader ?
> 
> I'd say you'll need to subclass QHeader and reimplement the appropriate
> event handler to popup your contextual menu. The logical choice would be
> to reimplement mouseReleaseEvent(QMouseEvent*) and check the event to
> see which button caused the event.
> 
> QHeader also inherits contextMenuEvent(QContextMenuEvent*), but the way
> QContextMenuEvents are generated is platform-dependent. If you don't
> bother with portability, this could be a good alternative.

No this is not necessary, and is a bit invasive.  You can instead use an
event filter on a custom QTable subclass.  This way you don't have to figure
out how to get your favorite QHeader inside a QTable.  I've done it this way
in a project of mine:

bool MyTable::eventFilter ( QObject* o, QEvent* e )
{
  if ( ( o == horizontalHeader() ) && ( e->type() == QEvent::ContextMenu ) )
  {
    QContextMenuEvent* cme = (QContextMenuEvent*)e;
    int column = columnAt( cme->pos().x() );
    QPopupMenu contextMenu( this );
    connect( &contextMenu, SIGNAL(activated(int)), FILL THIS PART IN );
    FILL THIS PART IN, TOO, SETTING UP YOUR MENU
    contextMenu.exec( cme->globalPos() );
    return TRUE; // true means we handled this event, so don't pass it on
  }
  // call parent class's filter...necessary in tables:
  return QTable::eventFilter( o, e );
}

Obviously, don't forget to declare bool MyTable::eventFilter ( QObject* o,
QEvent* e ) in the class declaration of MyTable.
 
Nathan

--
List archive and information: http://lists.trolltech.com/qt-interest/

<Prev in Thread] Current Thread [Next in Thread>