我的应用程序有一个带有按钮的主窗口,当我单击按钮时,我用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
}
Run Code Online (Sandbox Code Playgroud)
在我的主窗口中,我有这个函数(remoteControl是一个保持对远程窗口的引用的属性):
function showRemoteWindow() {
remoteControl.x = Screen.width - remoteControl.width
remoteControl.y = Screen.height - remoteControl.height
remoteControl.show()
}
Run Code Online (Sandbox Code Playgroud)
同样在我的主窗口我有一个按钮,在它的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
}
}
Run Code Online (Sandbox Code Playgroud)
如果我注释掉showRemoteWindow函数中的.x和的设置.y,那么我的RemoteWindow总是出现在与我的主窗口相同的屏幕上(macbook屏幕或连接的监视器).但是,如果我将这两行留下未注释(或进行任何其他尝试设置窗口的x或y位置),那么无论我的主窗口在哪个屏幕,我的RemoteWindow 总是出现在macbook屏幕上.
就像@Blabdouze所说,现在在Qt 5.9中有一个screen属性Window.您可以为其分配一个Qt.application.screens数组元素.
如果要在第一个屏幕中显示窗口,您可以执行以下操作:
import QtQuick.Window 2.3 // the 2.3 is necessary
Window {
//...
screen: Qt.application.screens[0]
}
Run Code Online (Sandbox Code Playgroud)
将屏幕分配给窗口似乎将其定位在屏幕的中心.如果你想精确控制窗口的位置,你可以使用x而y不是screen.例如,如果要在第一个屏幕的左下角显示一个窗口:
Window {
//...
screen: Qt.application.screens[0] //assigning the window to the screen is not needed, but it makes the x and y binding more readable
x: screen.virtualX
y: screen.virtualY + screen.height - height
}
Run Code Online (Sandbox Code Playgroud)
如果你还没有使用Qt 5.9,那么你可以从c ++中公开屏幕数组,如下所示:
QList<QObject*> screens;
for (QScreen* screen : QGuiApplication::screens())
screens.append(screen);
engine.rootContext()->setContextProperty("screens", QVariant::fromValue(screens));
Run Code Online (Sandbox Code Playgroud)
而随着进入屏幕的几何形状geometry/ virtualGeometry而非virtualX/ virtualY:
x: screens[0].geometry.x
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2863 次 |
| 最近记录: |