QML - 启动时主窗口位置(屏幕中心)
我可以如何执行以下操作:我想在中央屏幕上显示我的主窗口。
How I can do following: I’d like show my main window on start on the center screen.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我可以如何执行以下操作:我想在中央屏幕上显示我的主窗口。
How I can do following: I’d like show my main window on start on the center screen.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
如果使用 QtQuick,可以这样做:
If using QtQuick, it's possible to do that:
迪尔森的答案要好得多,特别是因为没有提到小部件......无论如何,这是他的答案的一个更简单的版本:
正如亚历山大提到的,这种绑定可能会导致奇怪的调整大小行为。因此,最好使用迪尔森的答案。我唯一要提到的是,在 QML 中使用 setter 并不常见;例如,某些系统(我相信它们被称为属性拦截器)甚至依赖于设置的属性来执行动画。所以比较常见的做法如下:
Dielson's answer is much better, especially since widgets weren't mentioned... anyway, here's an even simpler version of his answer:
As mentioned by Alexander, this binding can result in weird resizing behaviour. Because of that, it's better to use Dielson's answer. The only thing I'd mention is that it's not common to use setters in QML; some systems (I believe they're called property interceptors) even rely on properties being set to perform animations, for example. So the more common approach is as follows:
在检查了这两个回复并使用 Qt 5.9.1 实际调试代码后,它显示了原始回复的多个问题:
Component.onCompleted
中的 [x, y] 更改似乎是合乎逻辑的,但它在 2 个不同 DPI 的显示器上无法按预期工作(如我当前开发的系统)。Window.screen
而不是Screen
单例类型。这样我们就可以得到与窗口匹配的实际屏幕。onScreenChanged
,它是screen
属性的处理程序改变。这个解决方案更完整,使用 Window.screen 属性:
PS 如果是这样,我使用了从
Window
派生的ApplicationWindow
类型,它应该与Window
定位行为一致。After examining both replies and actually debugging the code with Qt 5.9.1 it shows more than one issue with original replies:
Component.onCompleted
seems to be logical it does not work as expected with 2 monitors of different DPI (as on systems I currently develop on).Window.screen
instead ofScreen
singleton type. That way we get actual screen matching the window on.onScreenChanged
which is a handler for thescreen
property change.This solution is more complete and uses Window.screen property:
P.S. If so I used
ApplicationWindow
type which is derived fromWindow
and it should be consistent withWindow
positioning behavior.缺少所有其他答案来考虑屏幕定位,可能导致窗口显示在错误的屏幕上。也许这曾经适用于以前版本的 Qt,但它似乎不再适用于最新的 Qt 版本。
以下解决方案适用于具有不同 DPI 设置的多屏幕(需要 Qt 5.9 或更高版本;在 macOS 上使用 Qt 5.15 进行测试):
All other answers are missing to take the screen positioning into account potentially causing the window to show up on the wrong screen. Maybe this used to work in previous version of Qt but it doesn't seem work with recent Qt versions anymore.
The following solution works for me with a multi-screen with different DPI setup (requires Qt 5.9 or later; tested with Qt 5.15 on macOS):
您需要
setGeometry
在显示它之前在您的顶级小部件上添加 。我能想到的计算出您需要的几何图形的最简单方法是通过 <代码>QDesktopWidget。尝试下面的示例(创建一个 QPushButton,在各个屏幕上移动小部件时按下它),您就会明白我的意思:从那里应该相当简单地提出一个有效的通用版本用户的中心屏幕(如果存在)。
You'll need to
setGeometry
on your top-level widget before you show it. The easiest way I can think of to work out what geometry you need is viaQDesktopWidget
. Try the example below (create aQPushButton
, press it while moving the widget around various screens) and you'll see what I mean:Should be reasonably simple from there to come up with a generic version that works out a user's center screen (if it exists).
亚历山大的回答几乎已经足够好了。但是,在 KDE 上,我观察到以下行为:窗口首先在监视器 1 上打开,然后立即移动到监视器 2。在这种情况下,引用的答案始终强制窗口到监视器 1。
因为尝试检测此行为可能会需要相当多的代码,我只是通过使用计时器寻求一个简单的解决方案:
这会在等待 10 毫秒后设置窗口的坐标(希望在这段时间 DE 已经完成其工作)。
Alexander's answer is almost good enough. However, on KDE I observe the following behaviour: the window is first opened on Monitor 1 and then is immediately moved to Monitor 2. In this situation, the referenced answer always forces the window to Monitor 1.
Since trying to detect this behaviour would probably require quite a bit of code, I just went for a simple solution by using a Timer:
This sets the window's coordinates after waiting for 10ms (hopefully in that time the DE has done its job).