【发布时间】:2017-07-14 03:07:07
【问题描述】:
我的应用程序有一个带有按钮的主窗口,当我单击该按钮时,我使用createComponent 创建Window {} 的子类并显示它(纯粹在QML 中)。我正在我的 Macbook 上运行该应用程序,并连接了另一台显示器。
如果我不尝试设置新窗口的.x 或.y 属性,那么无论主窗口是在我的macbook 屏幕上还是在连接的监视器上,它都会显示在我的主窗口顶部(即新窗口始终显示在与主窗口相同的屏幕上)。但是,如果我确实设置了新窗口的.x 或.y 属性(任何值),那么无论我的主窗口在哪个屏幕上,新窗口都会始终显示在macbook 屏幕上。
如何控制我的新窗口显示在哪个可用屏幕上?以及相关的,如何才能精确控制新窗口在屏幕中的位置(例如,如何让新窗口始终出现在右下角)?
编辑:基本代码。 RemoteWindow.qml:
Window {
id: myWindow
flags: Qt.Window | Qt.WindowTitleHint | Qt.WindowStaysOnTopHint
| Qt.WindowCloseButtonHint
modality: Qt.NonModal
height: 500
width: 350
// window contents, doesn't matter
}
在我的主窗口中,我有这个功能(remoteControl 是一个保留对远程窗口的引用的属性):
function showRemoteWindow() {
remoteControl.x = Screen.width - remoteControl.width
remoteControl.y = Screen.height - remoteControl.height
remoteControl.show()
}
我的主窗口还有一个按钮,在它的onClicked: 事件中我有这个代码:
if (remoteControl) {
showRemoteWindow()
} else {
var component = Qt.createComponent("RemoteWindow.qml")
if (component.status === Component.Ready) {
remoteControl = component.createObject(parent)
showRemoteWindow() // window appears even without this call,
// but calling this method to also set the initial position
}
}
如果我在 showRemoteWindow 函数中注释掉 .x 和 .y 的设置,那么我的 RemoteWindow 总是与我的主窗口(macbook 屏幕或连接的监视器)出现在同一个屏幕上。但是,如果我不注释这两行(或进行任何其他尝试设置窗口的 x 或 y 位置),那么无论我的主窗口在哪个屏幕上,我的 RemoteWindow always 都会出现在 macbook 屏幕上在里面。
【问题讨论】:
-
我之前在 Qt 中处理过这些问题。多显示器是坚果。您能否分享您的代码,最好是作为一个最小可行的完整示例?
-
@selbie:添加了我的基本代码 sn-ps。抱歉,我正在开发的应用程序(遗留代码)非常庞大,很难解开其中的各个部分。
-
@MusiGenesis 您使用哪个版本的 Qt?在 Qt 5.9 中,他们在
Window上添加了一个screen属性,这可能会满足您的需求。