QT5实现鼠标点击窗口外部关闭该窗口

有些时候我们希望通过鼠标点击窗口外部来关闭当前窗口,这里主要用到两种方法;

  •  [virtual] bool QObject::event(QEvent *e);
  •  [virtual] bool QObject::eventFilter(QObject *watched, QEvent *event);

 
方式一:重写窗口[virtual] bool QObject::event(QEvent *e)
该方式最简单,但是有一个弊病,需要为每个要实现该功能的窗口都重写该函数。

bool Form::event(QEvent *event)
{
    if (event->type() == QEvent::ActivationChange)
    {
        if(QApplication::activeWindow() != this)
        {
            this->close();
        }
    }

    return QWidget::event(event);
}

方式二:重写[virtual] bool QObject::eventFilter(QObject *watched, QEvent *event)

步骤一:为每个要实现该功能的窗口安装事件过滤void QObject::installEventFilter(QObject *filterObj),一般在构造函数里安装;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //f和f1都是窗口类实例
    f=new Form;
    f1=new Form1;
    f->installEventFilter(this);
    f1->installEventFilter(this);    
}

步骤二:重写事件过滤函数

bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
    if (event->type() == QEvent::ActivationChange)
    {
        if(watched == f || watched == f1)//需要实现该功能的控件
        {
            if(QApplication::activeWindow() != watched)
            {
                QWidget *w=static_cast<QWidget *>(watched);
                w->close();
            }
        }
    }
    return QMainWindow::eventFilter(watched, event);
}

 这样就在父窗口里实现了每个子窗口的点击窗口外关闭该窗口的功能,这样的实现还有一个缺陷,注册了事件过滤的窗口类只能有一个窗口可以显示,在打开其他窗口的同时会自动关闭现有激活的子窗口

Qt 中,弹出窗口(Popup)是通过 QPopupWindow 类来实现的。QPopupWindow 类继承自 QWidget 类,因此它可以像普通窗口一样显示和隐藏。 QPopupWindow 中实现鼠标按下事件事件过滤器来实现点击外部区域自动关闭的功能。具体来说,当弹出窗口显示时,它会在 QApplication 中安装一个事件过滤器,用于监听所有鼠标事件。当用户按下鼠标时,QPopupWindow 会检查鼠标位置是否在窗口外部,如果是,则会发出关闭信号,隐藏窗口。 以下是一个简单的示例代码,演示如何使用 QPopupWindow 类来实现点击外部自动关闭的弹出窗口: ```cpp #include <QApplication> #include <QMouseEvent> #include <QPopupWindow> #include <QLabel> #include <QDebug> class PopupWindow : public QPopupWindow { public: PopupWindow(QWidget *parent = nullptr) : QPopupWindow(parent) { QLabel *label = new QLabel("Hello, World!", this); label->setAlignment(Qt::AlignCenter); setWidget(label); } protected: bool eventFilter(QObject *object, QEvent *event) override { if (event->type() == QEvent::MouseButtonPress) { QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event); if (!rect().contains(mouseEvent->pos())) { hide(); return true; } } return QPopupWindow::eventFilter(object, event); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); PopupWindow popup; popup.setGeometry(100, 100, 200, 100); popup.show(); return app.exec(); } ``` 在上面的示例代码中,我们创建了一个 PopupWindow 类,它继承自 QPopupWindow 类。在 PopupWindow 类的构造函数中,我们创建了一个 QLabel,用于显示文本内容,并将其设置为窗口的子控件。在 PopupWindow 类中,我们重写了 eventFilter() 函数,用于过滤鼠标事件。当用户按下鼠标时,我们检查鼠标位置是否在窗口外部,如果是,则发出关闭信号,隐藏窗口。在主函数中,我们创建了一个 PopupWindow 对象,设置它的位置和大小,并显示出来。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

家有一枚袁宝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值