PDA

View Full Version : Saving color setting



waynew
25th January 2010, 02:20
Thanks to the Mole for trying to help with my previous post, but the problem remains.

I'm just trying to set the window color and save it so I can restore it on startup, and it's not working. When I try to get the color to save it to the database, the color is empty or invalid.
Here's the code:



void MainWindow::setColors() {
QColor color = QColorDialog::getColor();
QPalette palette = QPalette(color);
QApplication::setPalette(palette);

QSettings settings(this);
settings.setValue("MainWindow/Color", color);
}




void MainWindow::saveLook() {
QSettings settings(this);
QString color = settings.value("MainWindow/Color").toString(); // tried .value<QColor>() and it didn't work either
QFont font = settings.value("MainWindow/Font").value<QFont>();

qDebug() << "save color will be " << color;
qDebug() << "save font will be " << font;


QSqlDatabase db = QSqlDatabase::database(ctrlConn);
QSqlQuery query(db);
query.prepare("UPDATE settings set color =?, font =? where id=1");
query.addBindValue(color);
query.addBindValue(font);
query.exec();

qDebug() << "saving look " << query.lastError();
}


The problem is (just for Wysota :) ) qDebug() save color is empty.
If I use .value<QColor>() instead of .toString(), the qDebug() says the color is invalid.

I just want to save the user chosen color to my database and restore it on start up.
Any ideas?

aamer4yu
25th January 2010, 04:34
What if you try -
QColor color = settings.value("MainWindow/Color"); ??
From the code we can see that you are storing a QColor but trying to read QString !!
is that intended ?

vishwajeet.dusane
25th January 2010, 06:10
Hi

Try

settings.setValue("MainWindow/Color", color.name ());
while setting up QColor

and not sure about why u have done this
QString color = settings.value("MainWindow/Color").toString();
but here you can retrieve same color using
QColor color = settings.value("MainWindow/Color").toString();

Lykurg
25th January 2010, 07:55
The problem is (just for Wysota :) ) qDebug() save color is empty.
If I use .value<QColor>() instead of .toString(), the qDebug() says the color is invalid.

I just want to save the user chosen color to my database and restore it on start up.
Any ideas?

Well, you probably have some errors in your code. See following example, try it and if it works, you messed up your code with QString/QColor etc.

QColor c = QColor(145,23,0);
qWarning() << c;
QSettings s;
s.setValue("test", c);
QColor cc = s.value("test").value<QColor>();
qWarning() << cc;

waynew
25th January 2010, 12:54
Thanks for the tips. value<QColor> was also giving me an invalid color.
The problem has been solved by saving the color to the database at the time the user chooses it with the dialog instead of waiting until the user quits.
Works fine now!

wysota
26th January 2010, 10:54
The problem is (just for Wysota :) ) qDebug() save color is empty.
Hey... I'm not that dumb, you know! :)

waynew
28th January 2010, 22:10
I just thought it would save you the trouble of saying: "so what is the problem, exactly?" :)

astodolski
12th January 2015, 21:43
Well, you probably have some errors in your code. See following example, try it and if it works, you messed up your code with QString/QColor etc.

QColor c = QColor(145,23,0);
qWarning() << c;
QSettings s;
s.setValue("test", c);
QColor cc = s.value("test").value<QColor>();
qWarning() << cc;

I know this is old but it doesn't address the original issue of reading a QColor from a QSettings (INI) file. All this does is sets and gets in the same instance

wysota
13th January 2015, 06:32
If it reads the same instance then it means that the operation of reading a QColor from QSettings was successfull, doesn't it? If it weren't, the color would be empty.

astodolski
13th January 2015, 12:41
If it reads the same instance then it means that the operation of reading a QColor from QSettings was successfull, doesn't it? If it weren't, the color would be empty.

Of course - in that context. Specifically, I need to read in the color from an INI file entry, not set it in code. The issue is how to form the entry in the key field of the file. For example:



[Testing]
MarkerColor=@QColor(128,0,0)


That produced Invalid color using the following code



m_MarkerColor = IniSettings.value("Testing/MarkerColor").value<QColor>();


What I then tried was to leverage off the code presented as a way to see what the INI key would look like on a write - even if I didn't need to store it. I see that I may have not been forming the key properly because upon inspection of the INI file, I saw:



MarkerColor=@Variant(\0\0\0\x43\x1\xff\xff\0\0\0\0 \x80\x80\0\0)


from this code that wrote the key prior to exit



QColor finalColor = QColor(0,0,128);
IniSettings.setValue("Testing/MarkerColor", finalColor);



When I tried to read that key in, I still get Invalid color.

wysota
13th January 2015, 13:15
If I do this:

#include <QtCore>
#include <QColor>

int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
QSettings f("./test.ini", QSettings::IniFormat);
if(app.arguments().contains("-set"))
f.setValue("color", QColor(Qt::red));
else {
QColor c = f.value("color").value<QColor>();
qDebug() << c;
}
return 0;
}
Then I can write and read back the value stored.

However you can always do this:


#include <QtCore>
#include <QColor>

int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
QSettings f("./test.ini", QSettings::IniFormat);
if(app.arguments().contains("-set"))
f.setValue("color", QColor(Qt::red).name());
else {
QColor c = QColor(f.value("color").toString());
qDebug() << c;
}
return 0;
}

which leads to:

[General]
color=#ff0000