new serialize function
This commit is contained in:
parent
159488b35a
commit
ce852b0817
37
Serialize.py
37
Serialize.py
|
@ -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):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user