Solved Saving QByteArray to text similar to QSettings' format in ini file?
-
@lansing I'm not sure I follow: do you want to store memory dump of an object (like QFont instance) as binary data in ini files?! In my opinion you should rethink this approach:
- This is unreadable for user
- This is unchangeable by user
- This is most probably not going to work because of internal d pointer used by Qt classes to hide internal implementation. Also endianess and memory alignment needs to be considered...
-
@lansing You should check documentation: https://doc-snapshots.qt.io/qt5-dev/qfont.html
"If you want to store a user's font preferences you could use QSettings, writing the font information with toString() and reading it back with fromString(). The operator<<() and operator>>() functions are also available, but they work on a data stream."
There is really no need for binary data. -
I want to store a list of font as selection. User can add or remove font preference to this file
[theme one] id_1 ; font-setting ; other stuff [theme two] id_2; font-setting; ...
I don't think QSettings is right for this case?
I also tried the operator<<() to store them separately as font family, font size etc but I have to reconstruct them back together when I loaded them, which is tedious.
-
@lansing said in Saving QByteArray to text similar to QSettings' format in ini file?:
but I have to reconstruct them back together
Why? fromString() should do it just fine if you stored it before using toString() as described in the documentation.
"I want to store a list of font as selection" - you could construct this list by yourself calling toString() on each font and joining these strings into one using a separator. Later, when loading, just read the string from settings split it using the separator and use fromString() for each font. This is not that much work.
-
@jsulm said in Saving QByteArray to text similar to QSettings' format in ini file?:
fromString
Oh I don't know that we can do that. What is the syntax of fromString() when reading back from the text?
-
@lansing said in Saving QByteArray to text similar to QSettings' format in ini file?:
What is the syntax of fromString() when reading back from the text?
There is documentation: https://doc.qt.io/qt-5/qfont.html#fromString
-
Yes I read it, this is what I got so far
QFont preferenceFont; preferenceFont.fromString(fontString);
Is there a one liner for this? I tried
QFont::fromString(fontString)
but it doesn't work. -
@lansing said in Saving QByteArray to text similar to QSettings' format in ini file?:
I tried QFont::fromString(fontString) but it doesn't work.
Of course it does not since fromString() is not static.
Why do you need one liner? -
int main(int argc, char *argv[]) { QApplication app(argc, argv); QFont defaultFont; qDebug() << defaultFont; defaultFont.setPixelSize(25); defaultFont.setBold(true); qDebug() << defaultFont; QString stringRepresentation = defaultFont.toString(); qDebug() << stringRepresentation; QFont loadedFont; loadedFont.fromString(stringRepresentation); qDebug() << defaultFont << loadedFont; // return app.exec(); }
output:
QFont( ".AppleSystemUIFont,13,-1,5,50,0,0,0,0,0" ) QFont( ".AppleSystemUIFont,-1,25,5,75,0,0,0,0,0" ) ".AppleSystemUIFont,-1,25,5,75,0,0,0,0,0" QFont( ".AppleSystemUIFont,-1,25,5,75,0,0,0,0,0" ) QFont( ".AppleSystemUIFont,-1,25,5,75,0,0,0,0,0" )
-
I will be passing it as an argument after the conversion, so it would be shorter to do it in one line instead of three.
QFont preferenceFont; preferenceFont.fromString(fontString); document->setFont(preferenceFont)
-
constexpr auto stringToFont = [](const QString &str)->QFont{ QFont preferenceFont; preferenceFont.fromString(str); return preferenceFont; }; document->setFont(stringToFont(fontString));
made it constexpr to force inline-ness