Storing QTextCharFormat and QColor to text file?
-
I want a setting that allows users to import/export font setting and color from a setting file (text file). How do I convert them into string for storing? For
QColor
I think I can convert it to hex but I have no idea forQTextCharFormat
, -
@lansing You can implement
QTextStream & operator<<(const QTextCharFormat&) QTextStream & operator>>(QTextCharFormat&)
and serialise/de-serialise what ever QTextCharFormat properties you want.
See https://doc.qt.io/qt-5/qtextstream.html -
@lansing There are examples in the link I posted.
// I assume stream is a QTextStream here QTextCharFormat charFormat; stream >> charFormat;
-
This is my test to write to a text file:
QTextCharFormat textCharFormat; // this has value inside QFile data("output.txt"); if (data.open(QFile::WriteOnly | QFile::Truncate)) { QTextStream out(&data); out << textCharFormat; }
I got an error
invalid operands to binary expression ('QTextStream' to 'QTextCharFormat')
. -
@lansing That's why I wrote before:
You can implementQTextStream & operator<<(const QTextCharFormat&)
QTextStream & operator>>(QTextCharFormat&)You have to implement those operators first.
-
@jsulm said in Storing QTextCharFormat and QColor to text file?:
QTextStream & operator<<(const QTextCharFormat&)
Hi, I have searched around how to write overloaded function for the operator, but I have not seen any example on the syntax you're using.
I have declared
QTextStream & operator<<(const QTextCharFormat&)
in the class' header file, and in the source file, it should be something like this?QTextStream &SettingsDialog::operator<<(const QTextCharFormat &b) { stream << "font: " << b.font().toString() << " font-family: " << b.fontFamily(); return stream; }
But I don't know where do I pass in the
stream
in the implementation.
5/7