qt – 使用QML保存窗口状态
发布时间:2020-12-14 02:47:06 所属栏目:Windows 来源:网络整理
导读:有没有一种很好的方法来记录QtQuick应用程序的窗口状态?该文档提供了以下方法: Settings { property alias x: mainWindow.x property alias y: mainWindow.y property alias width: mainWindow.width property alias height: mainWindow.height 然而,这有
有没有一种很好的方法来记录QtQuick应用程序的窗口状态?该文档提供了以下方法:
Settings { property alias x: mainWindow.x property alias y: mainWindow.y property alias width: mainWindow.width property alias height: mainWindow.height 然而,这有三个缺陷: >当您调整窗口大小/移动窗口时,它会连续写入设置文件. 有没有人有更好的代码? 解决方法
就我的有限测试显示,我设法制作了一些效果很好的东西.我确实需要做一个hacky位,因为很遗憾Window.visibility在Window.x / y / width / height之后更新,这意味着如果你试图记录窗口几何,你不能只检查状态onXChanged中的Window.visibility.相反,我必须记录前两个值,然后如果窗口最大化则丢弃最近的值.
编辑:这不太完美.如果您最大化窗口,请关闭应用程序.然后打开它然后再关闭它.然后再次打开它,当你取消最大化时它将不会回到正确的窗口大小.我认为修复这个QML是非常丑陋的,我可能会在它真正属于的C中实现它. import QtQuick 2.3 import QtQuick.Window 2.2 import QtQuick.Controls 1.3 import Qt.labs.settings 1.0 Item { property Window window // Default properties for the application's first run. property int defaultX: 100 property int defaultY: 100 property int defaultWidth: 500 property int defaultHeight: 500 property bool defaultMaximised: false Settings { id: windowStateSettings category: "WindowState" property int x property int y property int width property int height property bool maximised } Component.onCompleted: { if (windowStateSettings.width === 0 || windowStateSettings.height === 0) { // First run,or width/height are screwed up. curX = defaultX; curY = defaultY; curWidth = defaultWidth; curHeight = defaultHeight; curMaximised = defaultMaximised } else { curX = windowStateSettings.x; curY = windowStateSettings.y; curWidth = windowStateSettings.width; curHeight = windowStateSettings.height; curMaximised = windowStateSettings.maximised } window.x = prevX = curX; window.y = prevY = curY; window.width = prevWidth = curWidth; window.height = prevHeight = curHeight; if (curMaximised) window.visibility = Window.Maximized; } // Remember the windowed geometry,and whether it is maximised or not. // Internal use only. property int curX property int curY property int curWidth property int curHeight property bool curMaximised // We also have to save the previous values of X/Y/Width/Height so they can be restored if we maximise,since we // can't tell that the updated X,Y values are because of maximisation until *after* the maximisation. property int prevX property int prevY property int prevWidth property int prevHeight Connections { target: window onVisibilityChanged: { if (window.visibility === Window.Maximized) { curMaximised = true; // Ignore the latest X/Y/width/height values. curX = prevX; curY = prevY; curWidth = prevWidth; curHeight = prevHeight; } else if (window.visibility === Window.Windowed) { curMaximised = false; } else if (window.visibility === Window.Hidden) { // Save settings. windowStateSettings.x = curX; windowStateSettings.y = curY; windowStateSettings.width = curWidth; windowStateSettings.height = curHeight; windowStateSettings.maximised = curMaximised; } } // We can't use window.visibility here to ignore the maximised geometry because it changes after the geometry. // Instead we cache the two previous values and revert them if maximised. onXChanged: { prevX = curX; curX = window.x; } onYChanged: { prevY = curY; curY = window.y; } onWidthChanged: { prevWidth = curWidth; curWidth = window.width; } onHeightChanged: { prevHeight = curHeight; curHeight = window.height; } } } 像这样使用它: ApplicationWindow { id: mainWindow WindowStateSaver { window: mainWindow defaultWidth: 1000 // Or whatever. You can also specify the defaultX/Y if you want. defaultHeight: 700 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
推荐文章
站长推荐
- CouchDB延迟构建索引(Windows Server 2008 R2上的
- winforms – 每个监视器DPI感知Windows系统映像列
- windows-server-2012-r2 – 将服务器链接到RD授权
- 有谁知道如何在Windows上设置ZeroMQ以使用OpenPG
- windows-server-2003 – 无法调整从gparted启动的
- 远程映像Windows Server
- windows – 如何从WndProc内部获取窗口句柄?
- windows-server-2008 – Windows Server 2003 R2
- windows – 如何使用批处理文件获取文件的独占锁
- 使用Microsoft Web Platform Installer安装Umbra
热点阅读