第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

@pyqtslot 返回值未定義

@pyqtslot 返回值未定義

PIPIONE 2023-07-05 15:41:28
我有一個使用設(shè)置的 QML 應(yīng)用程序。所以我創(chuàng)建了一個 python 槽來讀取settings.toml, 并返回值。我已經(jīng)正確設(shè)置了上下文屬性,并且可以毫無問題地調(diào)用其他函數(shù),而無需從 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 打印函數(shù)時,它按預(yù)期工作。...print(Settings.getSettings("last-used-font-settings", "font")) # Returns Arial...但是當(dāng)從 qml 登錄時:...   Component.onCompleted: console.log(Settings.getSettings("last-used-font-settings", "font"))/* Returns qml: undefined */...如何解決這個問題?我不太擅長創(chuàng)建 @pyqtProperty,但如果我設(shè)法創(chuàng)建一個,我絕對不想為每個值創(chuàng)建一個屬性!你看,信號不能用于此目的。:(
查看完整描述

1 回答

?
呼如林

TA貢獻1798條經(jīng)驗 獲得超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)

        }

    }

}


查看完整回答
反對 回復(fù) 2023-07-05
  • 1 回答
  • 0 關(guān)注
  • 205 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號