如何使Qt应用程序主窗口始终位于windows操作系统中其他窗口的顶部?

如何使Qt应用程序主窗口始终位于windows操作系统中其他窗口的顶部?,qt,qt5.6,Qt,Qt5.6,平台-Windows 7,8,10 我已经从QMainWindow创建了一个QApplication。 我希望它始终位于所有其他窗口的顶部 我使用了Qt标志(Qt::WindowStaysOnTopHint)来实现这一点。 但是这个Qt标志不起作用。 该应用程序是一个无框架的应用程序 请在下面找到我的Qt应用程序的构造函数代码 myApp::myApp(QWidget *parent) : QMainWindow(parent) { setWindowFlags(Qt::Widget | Qt

平台-Windows 7,8,10

我已经从QMainWindow创建了一个QApplication。 我希望它始终位于所有其他窗口的顶部

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

请在下面找到我的Qt应用程序的构造函数代码

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);
奇怪的是,这在我的机器上根本不起作用。 当我创建一个安装程序或复制所需的文件并在不同的计算机(Windows7、8、10)上运行时,我的应用程序将位于所有其他窗口之上。 注意:我使用的是Visual Studio社区版2015 操作系统-Windows 7专业服务包1。

请参阅:

使用WindowStaysOnTopHint标志。从Qt文档:

“通知窗口系统窗口应位于所有窗口的顶部 其他窗口。请注意,在X11上的某些窗口管理器上也有 传递Qt::X11ByPassWindowManagerInt以使此标志生效 正确。”

您的错误是分别为无框架SwindowHint和WindowStaysOnTopHint调用了两次setWindowFlags。尝试:

   Qt::WindowFlags flags = windowFlags();
   setWindowFlags(flags | Qt::X11BypassWindowManagerHint |     Qt::WindowStaysOnTopHint);
或者您可以使用Windows系统API:

#include <Windows.h>
....

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

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

myApp.h

    class myApp: public QMainWindow
    {
        Q_OBJECT
        public:
        explicit myApp(QWidget *parent = 0);
        ~myApp();
    protected:
        bool event(QEvent *event);
        ----
    };
myApp.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);
}
#包括
#包括
myApp::myApp(QWidget*父项):QMainWindow(父项)
{
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
ui.setupUi(这个);
}
bool myApp::事件(QEvent*事件){
开关(事件->类型()
{
案例QEvent::显示:
{
HWND winHWND=(HWND)winId();
if(winHWND){

qDebug()您必须在
ui.setupUi(此)之后设置
setWindowFlags

以下代码最终对我起作用,使我的窗口始终高于其他窗口

SetForegroundWindow((HWND)winId());
Qt::WindowFlags flags = this->windowFlags();
flags = flags & ~Qt::WindowMinimizeButtonHint;
this->setWindowFlags(flags|Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint );
ui.setupUi(this);
我还通过取消设置最小化标志阻止了窗口的最小化选项。
但仍然有一个问题。窗口的下半部分经过任务栏。我必须单击应用程序图标,使下半部分位于任务栏上方。

在构造函数中只写一行。(无需包含任何其他标题)


我尝试过你的解决方案。有时有效,有时无效。我不知道为什么有时无效。我尝试过你的解决方案。我也尝试过另一个答案。没有什么对我合适。有时我将窗口作为最顶部的窗口,而有时我没有。有什么方法可以调试标志出了什么问题吗我也尝试过这个选项。我尝试过的所有建议在我的机器上都不起作用,但在其他机器上效果很好。此外,我想补充一点,您需要包括“windows.h”才能使用SetForegroundWindow()
SetForegroundWindow((HWND)winId());
Qt::WindowFlags flags = this->windowFlags();
flags = flags & ~Qt::WindowMinimizeButtonHint;
this->setWindowFlags(flags|Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint );
ui.setupUi(this);
setWindowFlag(Qt::WindowStaysOnTopHint);