From 5d35de4e847bb5fa095d14f8940b0adc386f606b Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Sun, 6 Oct 2019 12:57:38 +0800 Subject: [PATCH] able to toggle stay on top --- mainwindow.cpp | 17 +++++++++++++++++ mainwindow.h | 2 ++ 2 files changed, 19 insertions(+) diff --git a/mainwindow.cpp b/mainwindow.cpp index ce52483..2a18406 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -250,12 +250,19 @@ void MainWindow::resizeEvent(QResizeEvent *event) void MainWindow::contextMenuEvent(QContextMenuEvent *event) { QMenu * menu = new QMenu; + QAction * stayOnTopMode = new QAction(tr("Stay on top")); + connect(stayOnTopMode, &QAction::triggered, this, [ = ](){ + toggleStayOnTop(); + }); + stayOnTopMode->setCheckable(true); + stayOnTopMode->setChecked(stayOnTop()); QAction * protectedMode = new QAction(tr("Protected mode")); connect(protectedMode, &QAction::triggered, this, [ = ](){ toggleProtectedMode(); }); protectedMode->setCheckable(true); protectedMode->setChecked(m_protectedMode); + menu->addAction(stayOnTopMode); menu->addAction(protectedMode); menu->exec(mapToGlobal(event->pos())); menu->deleteLater(); @@ -385,3 +392,13 @@ void MainWindow::toggleProtectedMode() m_protectedMode = !m_protectedMode; m_closeButton->setVisible(!m_protectedMode); } + +void MainWindow::toggleStayOnTop() +{ + setWindowFlag(Qt::WindowStaysOnTopHint, !stayOnTop()); +} + +bool MainWindow::stayOnTop() +{ + return windowFlags().testFlag(Qt::WindowStaysOnTopHint); +} diff --git a/mainwindow.h b/mainwindow.h index 3c47175..7687b1a 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -43,6 +43,8 @@ protected slots: void closeWindow(); void updateWidgetsPosition(); void toggleProtectedMode(); + void toggleStayOnTop(); + bool stayOnTop(); private: QPoint m_oldMousePos;