如何在QSplashScreen中禁用鼠标单击

时间:2018-10-02 14:15:18

标签: c++ qt

我创建了一个继承自QSplashScreen的类,并尝试覆盖mousePressEvent方法,但是当我单击时,将其隐藏起来。

    class Splash : public QSplashScreen
    {
    public:
        Splash();
        ~Splash() override;
    protected:
        void mousePressEvent(QMouseEvent *) override;
    };

1 个答案:

答案 0 :(得分:0)

我之前也遇到过同样的问题,希望对您有帮助
头文件

#ifndef MYSPLASH_H
#define MYSPLASH_H

#include <QObject>
#include <QSplashScreen>

class MySplash : public QSplashScreen
{
public:
    MySplash(const QPixmap &pixmap = QPixmap(), Qt::WindowFlags f = Qt::WindowFlags());
    bool eventFilter(QObject *target, QEvent *event);
};

#endif // MYSPLASH_H

CPP文件

#include "mysplash.h"
#include <QEvent>
#include <QDebug>
MySplash::MySplash(const QPixmap &pixmap, Qt::WindowFlags f)
{                
    this->setPixmap(pixmap);
    this->installEventFilter(this);
}

bool MySplash::eventFilter(QObject *target, QEvent *event)
{
    Q_UNUSED(target)
    if((event->type() == QEvent::MouseButtonPress) ||
            (event->type() == QEvent::MouseButtonDblClick) ||
            (event->type() == QEvent::MouseButtonRelease) ||
            (event->type() == QEvent::KeyPress) ||
            (event->type() == QEvent::KeyRelease))
        return true;

    return false;
}

在main.cpp中,您可以添加以下行

// Splash Screen
MySplash *splash = new MySplash(QPixmap(":/Images/SplashScreen.png"));
splash->show();
a.processEvents();

// Start the application
HomePage w;
w.showMaximized();

// Finish
splash->finish(&w);
delete splash;
a.processEvents();
return a.exec();