当前位置:网站首页>QT interface optimization: double click effect

QT interface optimization: double click effect

2022-04-23 14:27:00 Spiritual health

Qt Interface optimization : Double click the mouse effect


One 、 Double click special effects

 Insert picture description here

Two 、 Use steps

1. .h part

The code is as follows :

#include <QMovie>
#include <QLabel>
#include <QMouseEvent>
#include <QLine>
protected:
    void mouseDoubleClickEvent(QMouseEvent *event); // Double click event 

2. .cpp part

The code is as follows :

// Double click the mouse effect 
void MainWindows::mouseDoubleClickEvent(QMouseEvent *event)
{
    
    // Judge whether it is double clicking with the left mouse button 
    if(event->button() == Qt::LeftButton)
    {
    
        QLabel * label = new QLabel(this);
        QMovie * movie = new QMovie("://images/mouse.gif");// load gif picture 
        // Set up label Automatic adaptation gif Size 
        label->setScaledContents(true);

        label->setMovie(movie);
        // Here to call move convenient , Conduct resize, What we need to know is gif The size of is 150*150
        label->resize(180,180);
        label->setStyleSheet("background-color:rgba(0,0,0,0);");
        // Set mouse penetration 
        label->setAttribute(Qt::WA_TransparentForMouseEvents, true);
        // Give Way label The center of the is at the double click position of the current mouse 
        label->move(event->pos().x()-label->width()/2,event->pos().y()-label->height()/2);
        // Start playing gif
        movie->start();

        label->show();

        // binding QMovie The signal of , Judge gif plays 
        connect(movie, &QMovie::frameChanged, [=](int frameNumber) {
    
            if (frameNumber == movie->frameCount() - 1)//gif The number of plays is 1, Turn off the tag 
                label->close();
        });
    }
}

Be careful

gif The background of the moving picture must be transparent !!!

The following is my mouse double click effect picture , You can click the picture , Long press to save and use .

 Insert picture description here

版权声明
本文为[Spiritual health]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231411423135.html