事件过滤器

QMainWindow的子类对象的构造函数中安装事件过滤器

1
centralWidget()->installEventFilter(this);

信号和槽

由QAction触发进入全屏

1
2
3
4
5
6
connect(this->Internals->actionFull_Screen, &QAction::triggered, this, [=](bool flag) {
auto widget = centralWidget();
m_windowFlags = widget->windowFlags();
widget->setWindowFlag(Qt::Window);
widget->showFullScreen();
});

退出全屏

重写QMainWindow子类对象的eventFilter虚函数,在里边添加以下代码

1
2
3
4
5
6
7
8
9
10
if(obj == centralWidget() && ev->type() == QEvent::KeyPress) {
auto keyEvent = static_cast<QKeyEvent *>(ev);
if(keyEvent->key() == Qt::Key_Escape) {
auto widget = centralWidget();
widget->setWindowFlags(m_windowFlags);
widget->showNormal();
this->Internals->actionFull_Screen->setChecked(false);
return true;
}
}

© 2025 hywing 使用 Stellar 创建
总访问 113701 次 | 本页访问 326