4

平台 - Windows 7、8、10

我从 QMainWindow 创建了一个 QApplication。我希望它始终位于所有其他窗口之上。

我已经使用 Qt 标志( Qt::WindowStaysOnTopHint )来实现这一点。但是这个 Qt 标志不起作用。该应用程序是无框架应用程序。

请在下面找到我的 Qt App 的构造函数的代码。

myApp::myApp(QWidget *parent)
: QMainWindow(parent)
{
setWindowFlags(Qt::Widget |  Qt::FramelessWindowHint);
setWindowFlags(this->windowFlags() | Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint );
ui.setupUi(this);
}

我怎样才能使这个标志工作?

我已经尝试了社区中几位成员建议的所有选项。我现在的代码如下

Qt::WindowFlags flags = this->windowFlags();
this->setWindowFlags(flags  | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
ui.setupUi(this);

奇怪的事实是,这永远不会在我的机器上运行。当我创建安装程序或复制所需文件并在不同的机器(Windows 7、8、10)上运行时,我的应用程序将位于所有其他窗口之上。注意:我使用的是 Visual Studio Community Edition 2015 OS - Windows 7 Professional Service Pack 1。

4

5 回答 5

3

下面的代码终于让我的窗口始终高于其他窗口

SetForegroundWindow((HWND)winId());
Qt::WindowFlags flags = this->windowFlags();
flags = flags & ~Qt::WindowMinimizeButtonHint;
this->setWindowFlags(flags|Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint );
ui.setupUi(this);

我还通过取消设置最小化标志来阻止窗口的最小化选项。但仍然存在一个问题。窗口的下部越过任务栏。我必须单击应用程序图标才能将下部带到任务栏上方。

于 2016-09-28T12:18:37.763 回答
2

使窗口位于所有应用程序之上。

我的应用程序.h

    class myApp: public QMainWindow
    {
        Q_OBJECT
        public:
        explicit myApp(QWidget *parent = 0);
        ~myApp();
    protected:
        bool event(QEvent *event);
        ----
    };

我的应用程序.cpp

#include <windows.h>
#include <winuser.h>    
myApp::myApp(QWidget *parent): QMainWindow(parent)
{
    setWindowFlags(Qt::FramelessWindowHint |Qt::WindowStaysOnTopHint);
    ui.setupUi(this);
}
bool myApp::event(QEvent *event){
    switch (event->type())
    {
    case QEvent::Show:
    {
        HWND winHWND =(HWND) winId();
        if( winHWND ){
            qDebug() << endl << "Setting up associated console window ON TOP !";
            SetWindowPos(
                        winHWND, // window handle
                        HWND_TOPMOST, // "handle to the window to precede
                        // the positioned window in the Z order
                        // OR one of the following:"
                        // HWND_BOTTOM or HWND_NOTOPMOST or HWND_TOP or HWND_TOPMOST
                        0, 0, // X, Y position of the window (in client coordinates)
                        0, 0, // cx, cy => width & height of the window in pixels
                        SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW // The window sizing and positioning flags.
                        );
            // OPTIONAL ! - SET WINDOW'S "SHOW STATE"
            ShowWindow(
                        winHWND, // window handle
                        SW_NORMAL // how the window is to be shown
                        // SW_NORMAL => "Activates and displays a window.
                        // If the window is minimized or maximized,
                        // the system restores it to its original size and position.
                        // An application should specify this flag
                        // when displaying the window for the first time."
                        );
            qDebug() << endl << "Done.";
        } else {
            qDebug() << endl << "There is no console window associated with this app :(";
        }
        break;
    }
    default:
        break;
    }

    return QMainWindow::event(event);
}

如需更多帮助

于 2016-07-21T10:06:13.323 回答
0

write one simple line in constructor. (no need to include any other headers)

setWindowFlag(Qt::WindowStaysOnTopHint);
于 2020-08-29T12:19:22.950 回答
-1

看:

http://doc.qt.io/qt-5/qt.html http://doc.qt.io/qt-5/qtwidgets-widgets-windowflags-example.html

使用 WindowStaysOnTopHint 标志。来自 Qt 文档:

“通知窗口系统该窗口应位于所有其他窗口的顶部。请注意,在 X11 上的某些窗口管理器上,您还必须传递 Qt::X11BypassWindowManagerHint 才能使此标志正常工作。”

您的错是您分别为 FramelessWindowHint 和 WindowStaysOnTopHint 调用了两次 setWindowFlags 。尝试:

   Qt::WindowFlags flags = windowFlags();
   setWindowFlags(flags | Qt::X11BypassWindowManagerHint |     Qt::WindowStaysOnTopHint);

或者您可以使用 Windows 系统 API:

#include <Windows.h>
....

SetForegroundWindow((HWND)winId());
setWindowFlags(Qt::WindowStaysOnTopHint);

在您的 .pro 文件中:

win32-g++:LIBS += libUser32
win32-msvc*:LIBS += User32.lib
于 2016-07-21T09:48:09.657 回答
-1

你必须在setWindowFlags之后设置ui.setupUi(this);

于 2016-07-22T12:19:22.840 回答