@pyqtslot return 值未定义
@pyqtslot return value is undefined
我有一个使用设置的 QML 应用程序。所以我创建了一个 python 槽来读取 settings.toml
和 return 的值。我已经正确设置了上下文属性,我可以在没有 return 值的情况下从 QML 调用其他函数而不会出现问题。
...
class Settings(QObject)
@pyqtSlot(str, str)
def getSettings(self, category, key):
try:
with open("settings.toml", "r") as settings:
toml_object = toml.load(settings)
return str(toml_object[category][key])
except FileNotFoundError:
self.settingsFileNotFound.emit()
except toml.TomlDecodeError:
self.settingsError.emit()
except BaseException:
self.fatalError.emit()
...
settings.
toml 看起来像这样。
...
[last-used-font-settings]
font = "Arial"
...
打印 python 中的函数时,它按预期工作。
...
print(Settings.getSettings("last-used-font-settings", "font")) # Returns Arial
...
但是当从 qml 登录时:
...
Component.onCompleted: console.log(Settings.getSettings("last-used-font-settings", "font"))
/* Returns qml: undefined */
...
如何解决这个问题?
我不太擅长创建@pyqtProperty,但如果我设法创建一个,我绝对不想为每个值创建一个 属性!
你看,信号不会用于此目的。 :(
如果您想 return 来自 python 的值,那么您必须在 pyqtSlot 中使用“result”:
import os
from PyQt5.QtCore import pyqtProperty, pyqtSlot, QObject, QUrl
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine
import toml
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
class Settings(QObject):
def __init__(self, parent=None):
super().__init__(parent)
self._error_string = ""
@pyqtProperty(str, constant=True)
def errorString(self):
return self._error_string
@pyqtSlot(result=bool)
def hasError(self):
return bool(self.errorString)
@pyqtSlot(str, str, result=str)
def getSettings(self, category, key):
self._error_string = ""
error_string = ""
value = ""
try:
with open(os.path.join(CURRENT_DIR, "settings.toml"), "r") as settings:
toml_object = toml.load(settings)
value = str(toml_object[category][key])
except FileNotFoundError:
error_string = "FileNotFoundError"
except toml.TomlDecodeError:
error_string = "TomlDecodeError"
except Exception as e:
error_string = str(e)
self._error_string = error_string
return value
if __name__ == "__main__":
import sys
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
settings = Settings()
engine.rootContext().setContextProperty("Settings", settings)
filename = os.path.join(CURRENT_DIR, "main.qml")
engine.load(QUrl.fromLocalFile(filename))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec())
import QtQuick 2.12
import QtQuick.Controls 2.12
ApplicationWindow {
visible: true
width: 640
height: 480
Component.onCompleted: {
var value = Settings.getSettings("last-used-font-settings", "font")
if(Settings.hasError()){
console.error(Settings.errorString)
}
else{
console.log(value)
}
}
}
我有一个使用设置的 QML 应用程序。所以我创建了一个 python 槽来读取 settings.toml
和 return 的值。我已经正确设置了上下文属性,我可以在没有 return 值的情况下从 QML 调用其他函数而不会出现问题。
...
class Settings(QObject)
@pyqtSlot(str, str)
def getSettings(self, category, key):
try:
with open("settings.toml", "r") as settings:
toml_object = toml.load(settings)
return str(toml_object[category][key])
except FileNotFoundError:
self.settingsFileNotFound.emit()
except toml.TomlDecodeError:
self.settingsError.emit()
except BaseException:
self.fatalError.emit()
...
settings.
toml 看起来像这样。
...
[last-used-font-settings]
font = "Arial"
...
打印 python 中的函数时,它按预期工作。
...
print(Settings.getSettings("last-used-font-settings", "font")) # Returns Arial
...
但是当从 qml 登录时:
...
Component.onCompleted: console.log(Settings.getSettings("last-used-font-settings", "font"))
/* Returns qml: undefined */
...
如何解决这个问题? 我不太擅长创建@pyqtProperty,但如果我设法创建一个,我绝对不想为每个值创建一个 属性!
你看,信号不会用于此目的。 :(
如果您想 return 来自 python 的值,那么您必须在 pyqtSlot 中使用“result”:
import os
from PyQt5.QtCore import pyqtProperty, pyqtSlot, QObject, QUrl
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine
import toml
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
class Settings(QObject):
def __init__(self, parent=None):
super().__init__(parent)
self._error_string = ""
@pyqtProperty(str, constant=True)
def errorString(self):
return self._error_string
@pyqtSlot(result=bool)
def hasError(self):
return bool(self.errorString)
@pyqtSlot(str, str, result=str)
def getSettings(self, category, key):
self._error_string = ""
error_string = ""
value = ""
try:
with open(os.path.join(CURRENT_DIR, "settings.toml"), "r") as settings:
toml_object = toml.load(settings)
value = str(toml_object[category][key])
except FileNotFoundError:
error_string = "FileNotFoundError"
except toml.TomlDecodeError:
error_string = "TomlDecodeError"
except Exception as e:
error_string = str(e)
self._error_string = error_string
return value
if __name__ == "__main__":
import sys
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
settings = Settings()
engine.rootContext().setContextProperty("Settings", settings)
filename = os.path.join(CURRENT_DIR, "main.qml")
engine.load(QUrl.fromLocalFile(filename))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec())
import QtQuick 2.12
import QtQuick.Controls 2.12
ApplicationWindow {
visible: true
width: 640
height: 480
Component.onCompleted: {
var value = Settings.getSettings("last-used-font-settings", "font")
if(Settings.hasError()){
console.error(Settings.errorString)
}
else{
console.log(value)
}
}
}