new serialize function

This commit is contained in:
Paul Ebbers 2024-11-29 13:56:47 +01:00
parent 159488b35a
commit ce852b0817

View File

@ -3,19 +3,34 @@ from PySide import QtGui
import json import json
def iconToBase64(icon, sz=QtCore.QSize(64, 64), mode=QtGui.QIcon.Mode.Normal, state=QtGui.QIcon.State.On): def iconToBase64(icon: QtGui.QIcon, sz=QtCore.QSize(64, 64), mode=QtGui.QIcon.Mode.Normal, state=QtGui.QIcon.State.On):
buf = QtCore.QBuffer() """
buf.open(QtCore.QIODevice.WriteOnly) Converts a QIcon to a Base64-encoded string representation of its pixmap.
icon.pixmap(sz, mode, state).save(buf, "PNG")
result = None Args:
icon (QIcon): The icon to encode.
sz (QSize): The size of the pixmap to generate.
mode (QIcon.Mode): The mode of the pixmap (e.g., Normal, Disabled).
state (QIcon.State): The state of the pixmap (e.g., On, Off).
Returns:
str: The Base64-encoded string of the icon's pixmap.
"""
buf = QtCore.QBuffer()
buf.open(QtCore.QIODevice.OpenModeFlag.WriteOnly)
# Save the pixmap of the icon to the buffer in PNG format
pixmap: QtGui.QPixmap = icon.pixmap(sz, mode, state)
try: try:
result = QtCore.QTextCodec.codecForName("UTF-8").toUnicode(buf.data().toBase64()) pixmap.save(buf, "PNG")
except Exception: except Exception as e:
t = QtCore.QTextStream(buf.data().toBase64()) # raise ValueError("Failed to save icon to buffer. Ensure the icon is valid.")
t.setEncoding(QtCore.QStringDecoder.Encoding.Utf8) print(e)
result = t.readAll()
return result # Use standard Base64 encoding
base64_data = buf.data().toBase64().data().decode("utf-8")
buf.close()
return base64_data
def iconToHTML(icon, sz=12, mode=QtGui.QIcon.Mode.Normal, state=QtGui.QIcon.State.On): def iconToHTML(icon, sz=12, mode=QtGui.QIcon.Mode.Normal, state=QtGui.QIcon.State.On):