Handling mouse enter/leave events thru Delegate



  • I want to apply custom delegate for a column in QTableView. I want to draw the cell in my own way. Also I need to show the cell in slightly different way when the mouse is over that particular cell. I tried by overriding paint() function of custom QItemDelegate in following way. The painting works, but it always going to "else" loop and never goes to "if" loop. What is wrong with the code?

    	if(option.state & QStyle::State_MouseOver)
    	{
    		painter->setBrush(QBrush(Qt::yellow));
    		painter->setPen(QPen(Qt::red));
    	}
    	else
    	{
    		painter->setBrush(QBrush(Qt::gray));
    		painter->setPen(QPen(Qt::red));
    	}
    

  • Moderators

    If you want a repaint on mouse move you have to get mouse events when a button is not pressed. This is done by enabling mouse tracking on the view.



  • I tried with following change with no success

    table->setMouseTracking(true);
    

  • Moderators

    Should be enough. Can you show your delegate code and how you set it up in the table?



  • @Chris-Kawa
    Thanks for the help. Its working now. I was incorrectly overriding editorEvent() also and hence it was not receiving event.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.