如何使非模态对话框窗口始终位于顶部? [英] How to make a non-modal Dialog window be always on top?

查看:56
本文介绍了如何使非模态对话框窗口始终位于顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用 Dialog {} 的实例来显示一个小的控制器窗口,用户可以与之交互以影响主窗口中的功能(有点像遥控器).我可以将此对话框设为模态(modality: Qt.WindowModalmodality: Qt.ApplicationModal),或者我可以使用 modality: Qt.NonModal 将其设为非模态.

我的问题是我需要将其设为非模态但始终位于主窗口的顶部.如果我使用 Qt.NonModal 我仍然可以单击主窗体,但是我的 Dialog 在它后面.Dialog 类似乎没有 flags: 属性,所以我不能只将其设置为 Qt.WindowsStaysOnTopHint.

有没有什么方法可以像这样纯粹从 QML 方面设置对话框的标志?或者是否可以在 C++ 中编写一个简单的实用程序方法,我可以从对话框的 Component.onCompleted: 调用它并传入对话框以在那里设置其窗口标志?

更新:为了说明我在说什么,这是我的主窗口顶部的对话框:

这是我的主窗口下方的对话框:

我希望我的对话框不会像这样进入我的主窗口下方,但我仍然希望能够单击并与我的主窗口进行交互.换句话说,我希望我的对话框是非模态的,但总是在顶部.

解决方案

尝试使用

I am using an instance of a Dialog {} in my application to display a small controller window that the user can interact with to affect the functions in the main window (sort of a remote control). I can make this Dialog modal (modality: Qt.WindowModal or modality: Qt.ApplicationModal) or I can make it non-modal with modality: Qt.NonModal.

My problem is that I need to make it non-modal but be always on top of the main window. If I use Qt.NonModal I can still click on the main form but then my Dialog goes behind it. The Dialog class does not seem to have a flags: property, so I can't just set it to Qt.WindowsStaysOnTopHint.

Is there any way to set the flags of a Dialog like this purely from the QML side? Or is it possible to write a simple utility method in c++ that I could call from my Dialog's Component.onCompleted: and pass in the dialog to set its windows flags there?

Update: to illustrate what I'm talking about, here is my dialog on top of my main window:

Here is my dialog underneath my main window:

I want my dialog to not go underneath my main window like this, but I still want to be able to click on and interact with my main window. In other words, I want my dialog to be non-modal, but always-on-top.

解决方案

Try to use Window instead of Dialog this way you will have access to the flags property.

You can set flags to Qt.WindowStaysOnTopHint to have your window always on top of other ones. You can find the list of flags here. (Don't forget to replace the :: by . in QML)

Main.qml :

import QtQuick 2.5
import QtQuick.Controls 2.0
import QtQuick.Dialogs 1.2

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")


    Button {
        id: btn
        width: 100 ; height : 40
        text: "click me"
    }

    Text {
        anchors.top : btn.bottom
        text: "Button currently pressed"
        visible: btn.pressed
    }

    DialogOnTop {

    }
}

DialogOnTop.qml :

import QtQuick 2.0
import QtQuick.Window 2.0
import QtQuick.Controls 1.4

Window {
    id: myWindow

    width: 200
    height: 200

    flags:  Qt.Window | Qt.WindowSystemMenuHint
            | Qt.WindowTitleHint | Qt.WindowMinimizeButtonHint
            | Qt.WindowMaximizeButtonHint | Qt.WindowStaysOnTopHint


    visible: true
    modality: Qt.NonModal // no need for this as it is the default value


    Rectangle {
        color: "lightskyblue"
        anchors.fill: parent
        Text {
            text: "Hello !"
            color: "navy"
            anchors.centerIn: parent
        }
    }
}

Result :

这篇关于如何使非模态对话框窗口始终位于顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆