【问题标题】:how to disable mouse click in QSplashScreen如何在 QSplashScreen 中禁用鼠标单击
【发布时间】:2018-10-02 14:15:17
【问题描述】:

我创建了一个继承自 QSplashScreen 的类,并尝试覆盖 mousePressEvent 方法,但是当我单击时,会隐藏启动画面。

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

【问题讨论】:

  • 覆盖 closeEvent.
  • 我覆盖了closeEvent,情况都是一样的:/
  • 请编辑您的问题以提供可用于重现问题的minimal reproducible example。平台和Qt 版本也可能很重要。
  • 如何覆盖那些事件处理程序?

标签: c++ qt


【解决方案1】:

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

#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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 2023-04-06
    • 2020-11-16
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    相关资源
    最近更新 更多