PDA

View Full Version : how to find mouse position relative to a cell



winkle99
30th December 2009, 21:32
Hi,

I'm trying to figure out how to find out exactly where the user clicked within cell in a QTableWidget. (I'm using Qt 4.5.2)

I've got a table in a cell with several icons that indicate state (they are QImages drawn in the delegate's paint method). I want the user to be able to click on one of of the icons and have a dialog pop up. I've figured out how to isolate the mouse for displaying a tool tip depending on which icon the mouse is hovering over but I am at a loss as to how to get the information I need to the delegate for a click.

Here's what I am doing right now:

I've connected the cellClicked signal to my slot.


connect( myTable, SIGNAL( cellClicked(int, int) ), this, SLOT( myCellClicked(int,int)) );

Then I gather some information and pass it to the delegate for handling. In the delegate I can calculate the QRect that surrounds the specific icon. Just am not seeing how to properly map the mouse location.



void myCellClicked( int row, int column )
{
QModelIndex index = myTable->currentIndex();
//
// this QRect is relative to the selected cell. Yay!
//
qDebug() << " if I call visual Rect I get " << myTable->visualRect(index );
QPoint mousePos = QCursor::pos();
QPoint localPos = myTable->mapFromGlobal( mousePos );
qDebug() << " mouse positions global= " << mousePos << "local = " << localPos;
//
// The problem is that localPos seems to be relative to myTable's viewport
// and not specifically to the cell. That's the piece of information I'm trying to find.
//
DetailDelegate *itsDelegate = qobject_cast<lDetailDelegate*>(myTable->itemDelegate( index ) );
itsDelegate->handleClick( index, myTable->visualRect(index), localPos );

}