Search code examples
c++gtklabelmouseeventgtk2

How can I have a GTKLabel which changes based on mouse position?


I've read the documentation but can't find anything for this. For example, if I have a table and mouse over the top left, it'll say (0, 0), and it'll change accordingly based on where I mouse next.


Solution

  • Not sure what your purpose is and probably there are better ways to achieve your final goal but anyway,

    You must connect a signal to the widget you want the mouse to be tracked at. The signal is from GtkWidget and it's called motion-notify-event. The signal handler will receive the event with mice x and y coordinates.

    If you want to track the mouse when it's over the GtkTable (which inherits from GtkWidget) then just connect the signal to it and have the callback update the GtkLabel.

    EDIT:

    As @jku stated, some widgets don't receive these events because they don't have a GdkWindow. Probably the best option would be to add the table to a EventBox container and then proceed as stated previously. Also, this question has been made here.