Qml 设置:python 解决方案/设置
Qml Setting: python solution / Settings
我正在制作幻灯片,但我正在尝试让矩形保存其位置,以便用户可以保存他的图像或文本的大小。
file:///C:/Users//Desktop/Hp_Daniel/QML Components/galery test/drag.qml:14:5: QML Settings: Failed to initialize QSettings instance. Status code is: 1
file:///C:/Users//Desktop/Hp_Daniel/QML Components/galery test/drag.qml:14:5: QML Settings: The following application identifiers have not been set: QVector("organizationName", "organizationDomain")
python
import os
from pathlib import Path
import sys
from PySide2 import QtCore
from PySide2.QtWidgets import QApplication
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtCore import QUrl, QSettings
if __name__ == "__main__":
app = QApplication(sys.argv)
#app = QtCore.QCoreApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load(os.fspath(Path(__file__).resolve().parent / "drag.qml"))
#app.setOrganizationName("somename")
#app.setOrganizationDomain("somename")
app.setOrganizationName("MySoft")#problem
app.setOrganizationDomain("mysoft.com") #problem
app.setApplicationName("Star Runner")#problem
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
qml
import QtQuick 2.5
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.15
import Qt.labs.settings 1.0
import QtQuick.Window 2.2
Window {
title: qsTr("Test Crop")
width: 640
height: 480
visible: true
Component.onCompleted: ensureValidWindowPosition()
Component.onDestruction: saveScreenLayout()
Settings {
id: settings
property alias x: image1.x
property alias y: image1.y
property var desktopAvailableWidth
property var desktopAvailableHeight
}
function saveScreenLayout() {
settings.desktopAvailableWidth = Screen.desktopAvailableWidth
settings.desktopAvailableHeight = Screen.desktopAvailableHeight
}
function ensureValidWindowPosition() {
var savedScreenLayout = (settings.desktopAvailableWidth === Screen.desktopAvailableWidth)
&& (settings.desktopAvailableHeight === Screen.desktopAvailableHeight)
image1.x = (savedScreenLayout) ? settings.x : Screen.width / 2 - image1.width / 2
image1.y = (savedScreenLayout) ? settings.y : Screen.height / 2 - image1.height / 2
}
Rectangle {
id: image1
anchors.fill: parent
color: "lightgrey"
Rectangle {
x:parent.width / 4
y: parent.height / 4
width: parent.width / 2
height: parent.width / 2
id: selComp
border {
width: 2
color: "steelblue"
}
color: "#354682B4"
Rectangle {
width: 18
height: 18
color: "steelblue"
anchors.verticalCenter:parent.top
anchors.horizontalCenter: parent.left
MouseArea {
anchors.fill: parent
drag{ target: parent; axis: Drag.XAndYAxis }
onPositionChanged: {
if(drag.active){
var delta = Math.max(mouseX, mouseY)
var newWidth = selComp.width - delta
var newHeight = selComp.height - delta;
if (newWidth < width || newHeight < height)
return
selComp.width = newWidth
selComp.x = selComp.x + delta
selComp.height = newHeight
selComp.y = selComp.y + delta
}
}
}
}
}
}
}
您必须在加载 qml 之前初始化 organizationName、organizationDomain 和 applicationName:
import os
from pathlib import Path
import sys
from PySide2.QtCore import QCoreApplication, Qt, QUrl
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine
if __name__ == "__main__":
app = QGuiApplication(sys.argv)
QCoreApplication.setOrganizationName("MySoft")
QCoreApplication.setOrganizationDomain("mysoft.com")
QCoreApplication.setApplicationName("Star Runner")
engine = QQmlApplicationEngine()
filename = os.fspath(Path(__file__).resolve().parent / "drag.qml")
url = QUrl.fromLocalFile(filename)
def handle_object_created(obj, obj_url):
if obj is None and url == obj_url:
QCoreApplication.exit(-1)
engine.objectCreated.connect(handle_object_created, Qt.QueuedConnection)
engine.load(url)
sys.exit(app.exec_())
我正在制作幻灯片,但我正在尝试让矩形保存其位置,以便用户可以保存他的图像或文本的大小。
file:///C:/Users//Desktop/Hp_Daniel/QML Components/galery test/drag.qml:14:5: QML Settings: Failed to initialize QSettings instance. Status code is: 1
file:///C:/Users//Desktop/Hp_Daniel/QML Components/galery test/drag.qml:14:5: QML Settings: The following application identifiers have not been set: QVector("organizationName", "organizationDomain")
python
import os
from pathlib import Path
import sys
from PySide2 import QtCore
from PySide2.QtWidgets import QApplication
from PySide2.QtQml import QQmlApplicationEngine
from PySide2.QtCore import QUrl, QSettings
if __name__ == "__main__":
app = QApplication(sys.argv)
#app = QtCore.QCoreApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.load(os.fspath(Path(__file__).resolve().parent / "drag.qml"))
#app.setOrganizationName("somename")
#app.setOrganizationDomain("somename")
app.setOrganizationName("MySoft")#problem
app.setOrganizationDomain("mysoft.com") #problem
app.setApplicationName("Star Runner")#problem
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec_())
qml
import QtQuick 2.5
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.15
import Qt.labs.settings 1.0
import QtQuick.Window 2.2
Window {
title: qsTr("Test Crop")
width: 640
height: 480
visible: true
Component.onCompleted: ensureValidWindowPosition()
Component.onDestruction: saveScreenLayout()
Settings {
id: settings
property alias x: image1.x
property alias y: image1.y
property var desktopAvailableWidth
property var desktopAvailableHeight
}
function saveScreenLayout() {
settings.desktopAvailableWidth = Screen.desktopAvailableWidth
settings.desktopAvailableHeight = Screen.desktopAvailableHeight
}
function ensureValidWindowPosition() {
var savedScreenLayout = (settings.desktopAvailableWidth === Screen.desktopAvailableWidth)
&& (settings.desktopAvailableHeight === Screen.desktopAvailableHeight)
image1.x = (savedScreenLayout) ? settings.x : Screen.width / 2 - image1.width / 2
image1.y = (savedScreenLayout) ? settings.y : Screen.height / 2 - image1.height / 2
}
Rectangle {
id: image1
anchors.fill: parent
color: "lightgrey"
Rectangle {
x:parent.width / 4
y: parent.height / 4
width: parent.width / 2
height: parent.width / 2
id: selComp
border {
width: 2
color: "steelblue"
}
color: "#354682B4"
Rectangle {
width: 18
height: 18
color: "steelblue"
anchors.verticalCenter:parent.top
anchors.horizontalCenter: parent.left
MouseArea {
anchors.fill: parent
drag{ target: parent; axis: Drag.XAndYAxis }
onPositionChanged: {
if(drag.active){
var delta = Math.max(mouseX, mouseY)
var newWidth = selComp.width - delta
var newHeight = selComp.height - delta;
if (newWidth < width || newHeight < height)
return
selComp.width = newWidth
selComp.x = selComp.x + delta
selComp.height = newHeight
selComp.y = selComp.y + delta
}
}
}
}
}
}
}
您必须在加载 qml 之前初始化 organizationName、organizationDomain 和 applicationName:
import os
from pathlib import Path
import sys
from PySide2.QtCore import QCoreApplication, Qt, QUrl
from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine
if __name__ == "__main__":
app = QGuiApplication(sys.argv)
QCoreApplication.setOrganizationName("MySoft")
QCoreApplication.setOrganizationDomain("mysoft.com")
QCoreApplication.setApplicationName("Star Runner")
engine = QQmlApplicationEngine()
filename = os.fspath(Path(__file__).resolve().parent / "drag.qml")
url = QUrl.fromLocalFile(filename)
def handle_object_created(obj, obj_url):
if obj is None and url == obj_url:
QCoreApplication.exit(-1)
engine.objectCreated.connect(handle_object_created, Qt.QueuedConnection)
engine.load(url)
sys.exit(app.exec_())