How to save a QIcon in a settings.ini using QSettings?
-
I'm trying to save a QIcon in a settings.ini using QSettings.
I'm saving the icon this way:
QIcon icon = button->icon(); if (!icon.isNull()) iniSettings->setValue("icon", icon.pixmap(32, 32).toImage());And trying to load it as:
qDebug() << iniSettings->value("icon"); QIcon icon = iniSettings->value("icon").value<QIcon>(); if (!icon.isNull()) button->setIcon(icon);But icon is NULL, whats wrong?
-
I have been able to get it working as:
To save the icon in the settings:QIcon icon = pushButton->icon(); if (!icon.isNull()) iniSettings->setValue(key, icon.pixmap(32, 32));And to load:
QIcon icon = iniSettings->value(key).value<QPixmap>(); if (!icon.isNull()) pushButton->setIcon(icon); -
I'm trying to save a QIcon in a settings.ini using QSettings.
I'm saving the icon this way:
QIcon icon = button->icon(); if (!icon.isNull()) iniSettings->setValue("icon", icon.pixmap(32, 32).toImage());And trying to load it as:
qDebug() << iniSettings->value("icon"); QIcon icon = iniSettings->value("icon").value<QIcon>(); if (!icon.isNull()) button->setIcon(icon);But icon is NULL, whats wrong?
@Daniella Why do you want to store icons in an ini file?!
Ini format is quite simple and is not meant to store any binary data...
If you really want to do that then encode the icon binary data as base64 string and store that string in your ini file. -
@Daniella Why do you want to store icons in an ini file?!
Ini format is quite simple and is not meant to store any binary data...
If you really want to do that then encode the icon binary data as base64 string and store that string in your ini file. -
@jsulm it just small icons that i would like to save between sessions, how is it of base64?
-
D Daniella has marked this topic as solved on
-
@jsulm it just small icons that i would like to save between sessions, how is it of base64?
-
D Daniella has marked this topic as unsolved on
-
Is possible somehow to append different widgets to a container at once, in another way than iterating their list and pushing it to a
QList<QWidget*>?I'm current doing this:
for (auto keySquence : findChildren<QKeySequenceEdit*>()) { loadSettings(keySquence); } for (auto spinBox : findChildren<QSpinBox*>()) { loadSettings(spinBox); } // ... other specific widget typesI mean if is possible something like this:
QList<QWidget*> widgetList; widgetList.append(findChildren<QSpinBox*>()); // <- this doesnt compile, error widgetList.append(findChildren<QKeySequenceEdit*>()); for (auto widget : widgetList) { loadSettings(widget); }I dont want to call findChildren<QWidget*> because im looking only for specific widget types.
-
Is possible somehow to append different widgets to a container at once, in another way than iterating their list and pushing it to a
QList<QWidget*>?I'm current doing this:
for (auto keySquence : findChildren<QKeySequenceEdit*>()) { loadSettings(keySquence); } for (auto spinBox : findChildren<QSpinBox*>()) { loadSettings(spinBox); } // ... other specific widget typesI mean if is possible something like this:
QList<QWidget*> widgetList; widgetList.append(findChildren<QSpinBox*>()); // <- this doesnt compile, error widgetList.append(findChildren<QKeySequenceEdit*>()); for (auto widget : widgetList) { loadSettings(widget); }I dont want to call findChildren<QWidget*> because im looking only for specific widget types.
@Daniella said in How to save a QIcon in a settings.ini using QSettings?:
widgetList.append(findChildren<QSpinBox*>()); // <- this doesnt compile, errorI'm surprised. What error message? void QList::append(const QList<T> &value). Does it help if you assign the
findChildren<>()to a variable? So that variable can beQList<QWidget*>instead ofQList<QSpinBox*>etc., to matchQList<QWidget*> widgetListexactly? -
@Daniella said in How to save a QIcon in a settings.ini using QSettings?:
widgetList.append(findChildren<QSpinBox*>()); // <- this doesnt compile, errorI'm surprised. What error message? void QList::append(const QList<T> &value). Does it help if you assign the
findChildren<>()to a variable? So that variable can beQList<QWidget*>instead ofQList<QSpinBox*>etc., to matchQList<QWidget*> widgetListexactly?@JonB No, QList<Foo*> is something other than QList<Bar*> even if Foo derives from Bar.
-
@JonB No, QList<Foo*> is something other than QList<Bar*> even if Foo derives from Bar.
@Christian-Ehrlicher
Interesting. Yep I get it.@Daniella
So you would indeed need to iterate and append one at a time. But in any case for efficiency I wouldn't do it your way anyway. Every time you callfindChildren<>(Type)you re-traverse the whole widget hierarchy, and generate a list, for each type you want to deal with. I would traverse once and useifs, for efficiency:for (auto widget : findChildren<QWidget *>()) { if (qobject_cast<QSpinBox *>(widget)) else if (qobject_cast<QKeySequenceEdit *>(widget)) ... } -
@Christian-Ehrlicher
Interesting. Yep I get it.@Daniella
So you would indeed need to iterate and append one at a time. But in any case for efficiency I wouldn't do it your way anyway. Every time you callfindChildren<>(Type)you re-traverse the whole widget hierarchy, and generate a list, for each type you want to deal with. I would traverse once and useifs, for efficiency:for (auto widget : findChildren<QWidget *>()) { if (qobject_cast<QSpinBox *>(widget)) else if (qobject_cast<QKeySequenceEdit *>(widget)) ... }@JonB I wonder what settings are to load for a plain QWidget...
-
@jsulm it just small icons that i would like to save between sessions, how is it of base64?
@Daniella said in How to save a QIcon in a settings.ini using QSettings?:
@jsulm it just small icons that i would like to save between sessions
OOI, how did these icons get created that you want to save them as pixmaps? Usually you would have a filepath or a url or a resource id or similar, wouldn't you be better saving that in settings?
For the rest of the widgets, as @Christian-Ehrlicher says what is it from them you would want to save in settings?
-
I'm trying to save a QIcon in a settings.ini using QSettings.
I'm saving the icon this way:
QIcon icon = button->icon(); if (!icon.isNull()) iniSettings->setValue("icon", icon.pixmap(32, 32).toImage());And trying to load it as:
qDebug() << iniSettings->value("icon"); QIcon icon = iniSettings->value("icon").value<QIcon>(); if (!icon.isNull()) button->setIcon(icon);But icon is NULL, whats wrong?
@Daniella said in How to save a QIcon in a settings.ini using QSettings?:
But icon is NULL, whats wrong?
You are saving a QImage and are reading a QIcon. These are not the same. Either save a QIcon (if that is possible at all) or read in a QImage and create a QIcon from that.
-
I have been able to get it working as:
To save the icon in the settings:QIcon icon = pushButton->icon(); if (!icon.isNull()) iniSettings->setValue(key, icon.pixmap(32, 32));And to load:
QIcon icon = iniSettings->value(key).value<QPixmap>(); if (!icon.isNull()) pushButton->setIcon(icon); -
D Daniella has marked this topic as solved on