为了账号安全,请及时绑定邮箱和手机立即绑定

@pyqtslot 返回值未定义

@pyqtslot 返回值未定义

PIPIONE 2023-07-05 15:41:28
我有一个使用设置的 QML 应用程序。所以我创建了一个 python 槽来读取settings.toml, 并返回值。我已经正确设置了上下文属性,并且可以毫无问题地调用其他函数,而无需从 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()...tomlsettings.看起来像这样。...[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,但如果我设法创建一个,我绝对不想为每个值创建一个属性!你看,信号不能用于此目的。:(
查看完整描述

1 回答

?
呼如林

TA贡献1798条经验 获得超3个赞

如果你想从 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)

        }

    }

}


查看完整回答
反对 回复 2023-07-05
  • 1 回答
  • 0 关注
  • 154 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信