Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to implement mouseEnter and mouseLeave event in QWidget?

Tags:

qt

how to implement mouseEnter and mouseLeave event in QWidget?

if the mouseEnter to the QWidget then i need to set the Background color into Gray, if the mouseLeave from the QWidget then i need to set the background color is white

i tried

void enterEvent(QEvent *);
void leaveEvent(QEvent *);

in the inside of the enter&leave event i am using bool varibale set true & false. and i am calling the QPainter event update();

the code below:

void Test::enterEvent(QEvent *)
{
   _mouseMove=true;
    update();

}

void Test::leaveEvent(QEvent *)
{
   _mouseMove=false;
    update();

}

void Test::paintEvent(QPaintEvent *)
{
    QPainter painter;
    painter.begin(&m_targetImage);
    painter.setRenderHint(QPainter::Antialiasing);

        if(_mouseMove){
            painter.fillRect(QRect(0,0,width(),height()),Qt::white);}
        else{
            painter.fillRect(QRect(0,0,width(),height()),Qt::gray);}

    painter.end();

    QPainter p;
    p.begin(this);
    p.drawImage(0, 0, m_targetImage);
    p.end();
}

i am getting following error when i am moving the mouse in the QWidget

QPainter::begin: Paint device returned engine == 0, type: 3
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::end: Painter not active, aborted

Please help me to fix this. if any one having sample code please provide me....

like image 883
saravanan Avatar asked Dec 02 '10 08:12

saravanan


1 Answers

QWidgets also support the underMouse method which could be used instead of the StyleOption or Attribute solution:

if(underMouse()){
    painter.fillRect(QRect(0,0,width(),height()),Qt::white);}
else{
    painter.fillRect(QRect(0,0,width(),height()),Qt::gray);}
like image 94
Elias Avatar answered Nov 15 '22 05:11

Elias