PDA

View Full Version : QTextCharFormat and QVariant



fullmetalcoder
15th May 2007, 19:53
According to the docs, it should be possible to write custom types to a QVariant and to read them from it. As QSettings supports QVariant it implicitly means that you can write custom types to QSettings and get them back. Nice theory but does it work? I tried with QTextCharFormat (declaring the metatype, registering it and registering stream operators) : segfault when reading the QSettings previusly written. I tried the same thing with QTextFormat (base class of QTexCharFormat, which has operators defined) : segfault at the same place... However, when I tried to save/reload QTextCharFormat objects using a QDataStream, everything went fine... Is it me or is QSettings badly broken???

P.S : I'm using Qt 4.2.2

wysota
15th May 2007, 20:41
What does it segfault on? Can you provide a minimal compilable example reproducing the problem?

fullmetalcoder
16th May 2007, 11:55
What does it segfault on?
Some internal stream function (datastream >> QTextFormat I guess).


Can you provide a minimal compilable example reproducing the problem?
Sure! Here it comes :



#include <QSettings>
#include <QTextCharFormat>

Q_DECLARE_METATYPE(QTextFormat)

int main(int argc, char **argv)
{
qRegisterMetaType<QTextFormat>("QTextFormat");
qRegisterMetaTypeStreamOperators<QTextFormat>("QTextFormat");

QSettings s("formats.ini", QSettings::IniFormat);
QTextCharFormat f;

s.setValue("normal", f);

f.setForeground(Qt::red);
s.setValue("text", f);

f.setFontItalic(true);
f.setForeground(Qt::darkBlue);
s.setValue("comment", f);

f.setForeground(Qt::lightGray);
s.setValue("doxygen", f);

f.setForeground(Qt::black);
f.setFontWeight(10);
f.setFontItalic(false);
s.setValue("text", f);

s.sync();

foreach ( const QString& key, s.allKeys() )
f = qvariant_cast<QTextFormat>(s.value(key)).toCharFormat();

return 0;
}