How to notify a widget for mouse event?



  • hi,
    i want to create a class that when i bring my mouse to a widget(e.g. qPushButton or qLabel etc.) its color changes or its text becomes bold…
    please help me how can i do that?
    tnx :)



  • To enable mouse tracking:
    [quote]mouseTracking : bool

    This property holds whether mouse tracking is enabled for the widget.

    If mouse tracking is disabled (the default), the widget only receives mouse move events when at least one mouse button is pressed while the mouse is being moved.

    If mouse tracking is enabled, the widget receives mouse move events even if no buttons are pressed.[/quote]

    Then you can simply re-implement QWidget::mouseMoveEvent() in your widget to process the events - or you install an event filter on the widget object (the latter is when you don't want to sub-class).



  • i wanna create a class to do that. could you help me in that?



  • @class MyWidget : public QLabel
    {
    MyWidget ( QWidget * parent = 0)
    :
    QLabel(parent)
    {
    setMouseTracking(true);
    }
    virtual void mouseMoveEvent ( QMouseEvent * event )
    {
    qDebug("Mouse has been moved!");
    }
    virtual void enterEvent ( QEvent * event )
    {
    qDebug("Mouse cursor has entered!");
    }
    virtual void leaveEvent ( QEvent * event )
    {
    qDebug("Mouse cursor has left!");
    }
    }@


Log in to reply
 

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