How do I toggle 'always on top' for a QMainWindow in Qt without causing a flicker or a flash?

前端 未结 4 1114
猫巷女王i
猫巷女王i 2020-12-28 15:07
void MainWindow::on_actionAlways_on_Top_triggered(bool checked)
{
    Qt::WindowFlags flags = this->windowFlags();
    if (checked)
    {
        this->setWind         


        
4条回答
  •  北海茫月
    2020-12-28 15:26

    Well, for a solution I figured I'd look in the Mono sources, since I know the .NET Form class (System.Windows.Forms) has a TopMost property.

    The solution I found for my Qt program was:

    void MainWindow::on_actionAlways_on_Top_triggered(bool checked)
    {
    #ifdef Q_OS_WIN
        // #include 
        if (checked)
        {
            SetWindowPos(this->winId(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
        }
        else
        {
            SetWindowPos(this->winId(), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
        }
    #else
        Qt::WindowFlags flags = this->windowFlags();
        if (checked)
        {
            this->setWindowFlags(flags | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint);
            this->show();
        }
        else
        {
            this->setWindowFlags(flags ^ (Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint));
            this->show();
        }
    #endif
    }
    

提交回复
热议问题