PDA

View Full Version : Question on Context Menu



Sarol
16th June 2011, 18:17
Good Day Everyone,

Before getting to my question here is a brief overview of what I'm working on:

I have a QMainWindow Form that has a few push buttons and a QTableWidget. I have created a customContextMenu for when a user right clicks on the QTableWidget.

For the most part everything works as intended, but here are my questions/issues.

1) Once I right on the QTableWidget the menu appears where the mouse cursor is, but will not disappear until another click ( left/right ) is detected. Is there a way to hide the menu once the cursor is no longer on top of it, or moves a certain distance away? ( I thought I saw a post on this once before, but am having trouble locating it to see if it helps with my issue )

2) Is there a way to prevent the context menu from displaying on empty rows? My general thought is to do something with the cursor position and check where it is in relation to the table, and then see if there is a valid row at that particular point. However, I am unsure of how to really do this.

Here is the method I used for creating my menu



void QTMainform::contextMenuRequested( const QPoint& )
{
//QPoint globalPos = this->mapToGlobal( pos ) ;

QMenu myMenu ;
myMenu.addAction( "Start" , this , SLOT( start()) ) ;
myMenu.addAction( "Stop" , this , SLOT( stop() ) ) ;
myMenu.addAction( "Help" , this , SLOT( help() ) ) ;

myMenu.exec( QCursor::pos() ) ;
}




Any help is appreciated.

Thanks

Santosh Reddy
16th June 2011, 21:32
1) Once I right on the QTableWidget the menu appears where the mouse cursor is, but will not disappear until another click ( left/right ) is detected. Is there a way to hide the menu once the cursor is no longer on top of it, or moves a certain distance away? ( I thought I saw a post on this once before, but am having trouble locating it to see if it helps with my issue )
One of the ways to do, Install a event filter on the QTableWidget, which captures the mouse move event and check if mouse is not over context menu then hide / delete the menu, you can also install the filter on context menu itself


2) Is there a way to prevent the context menu from displaying on empty rows? My general thought is to do something with the cursor position and check where it is in relation to the table, and then see if there is a valid row at that particular point. However, I am unsure of how to really do this.


void QTMainform::contextMenuRequested( const QPoint& point )
{
...
QTableWidgetItem * item = tableWidget.itemAt(point);
if(item != 0)
{
if(!item->text().isEmpty())
{
//Then Show Context Menu
QMenu myMenu ;
...
}
}
...
}

Sarol
20th June 2011, 15:05
Santosh,

Thank you very much for the response. I was able to solve the second question with your help.

As for my first issue, I have not dealt with Event Filters before, so I will have to research these and then implement your suggestion.

Have a great day,

Sarol