Results 1 to 11 of 11

Thread: Saving color setting

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Saving color setting

    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:

    Qt Code:
    1. void MainWindow::setColors() {
    2. QColor color = QColorDialog::getColor();
    3. QPalette palette = QPalette(color);
    4. QApplication::setPalette(palette);
    5.  
    6. QSettings settings(this);
    7. settings.setValue("MainWindow/Color", color);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void MainWindow::saveLook() {
    2. QSettings settings(this);
    3. QString color = settings.value("MainWindow/Color").toString(); // tried .value<QColor>() and it didn't work either
    4. QFont font = settings.value("MainWindow/Font").value<QFont>();
    5.  
    6. qDebug() << "save color will be " << color;
    7. qDebug() << "save font will be " << font;
    8.  
    9.  
    10. QSqlDatabase db = QSqlDatabase::database(ctrlConn);
    11. QSqlQuery query(db);
    12. query.prepare("UPDATE settings set color =?, font =? where id=1");
    13. query.addBindValue(color);
    14. query.addBindValue(font);
    15. query.exec();
    16.  
    17. qDebug() << "saving look " << query.lastError();
    18. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Saving color setting

    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 ?

  3. #3
    Join Date
    Sep 2009
    Posts
    72
    Thanked 10 Times in 10 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: Saving color setting

    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();

  4. The following user says thank you to vishwajeet.dusane for this useful post:

    waynew (25th January 2010)

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Saving color setting

    Quote Originally Posted by waynew View Post
    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.
    Qt Code:
    1. QColor c = QColor(145,23,0);
    2. qWarning() << c;
    3. s.setValue("test", c);
    4. QColor cc = s.value("test").value<QColor>();
    5. qWarning() << cc;
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to Lykurg for this useful post:

    waynew (25th January 2010)

  7. #5
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Saving color setting

    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!

  8. #6
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Thanks
    7
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: Saving color setting

    Quote Originally Posted by Lykurg View Post
    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.
    Qt Code:
    1. QColor c = QColor(145,23,0);
    2. qWarning() << c;
    3. s.setValue("test", c);
    4. QColor cc = s.value("test").value<QColor>();
    5. qWarning() << cc;
    To copy to clipboard, switch view to plain text mode 
    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

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Saving color setting

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #8
    Join Date
    Oct 2012
    Location
    The land of pain (NY)
    Posts
    99
    Thanks
    7
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: Saving color setting

    Quote Originally Posted by wysota View Post
    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:

    Qt Code:
    1. [Testing]
    2. MarkerColor=@QColor(128,0,0)
    To copy to clipboard, switch view to plain text mode 

    That produced Invalid color using the following code

    Qt Code:
    1. m_MarkerColor = IniSettings.value("Testing/MarkerColor").value<QColor>();
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. MarkerColor=@Variant(\0\0\0\x43\x1\xff\xff\0\0\0\0\x80\x80\0\0)
    To copy to clipboard, switch view to plain text mode 

    from this code that wrote the key prior to exit

    Qt Code:
    1. QColor finalColor = QColor(0,0,128);
    2. IniSettings.setValue("Testing/MarkerColor", finalColor);
    To copy to clipboard, switch view to plain text mode 


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

  11. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Saving color setting

    If I do this:
    Qt Code:
    1. #include <QtCore>
    2. #include <QColor>
    3.  
    4. int main(int argc, char **argv) {
    5. QCoreApplication app(argc, argv);
    6. QSettings f("./test.ini", QSettings::IniFormat);
    7. if(app.arguments().contains("-set"))
    8. f.setValue("color", QColor(Qt::red));
    9. else {
    10. QColor c = f.value("color").value<QColor>();
    11. qDebug() << c;
    12. }
    13. return 0;
    14. }
    To copy to clipboard, switch view to plain text mode 
    Then I can write and read back the value stored.

    However you can always do this:

    Qt Code:
    1. #include <QtCore>
    2. #include <QColor>
    3.  
    4. int main(int argc, char **argv) {
    5. QCoreApplication app(argc, argv);
    6. QSettings f("./test.ini", QSettings::IniFormat);
    7. if(app.arguments().contains("-set"))
    8. f.setValue("color", QColor(Qt::red).name());
    9. else {
    10. QColor c = QColor(f.value("color").toString());
    11. qDebug() << c;
    12. }
    13. return 0;
    14. }
    To copy to clipboard, switch view to plain text mode 

    which leads to:

    [General]
    color=#ff0000
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Saving color setting

    Quote Originally Posted by waynew View Post
    The problem is (just for Wysota ) qDebug() save color is empty.
    Hey... I'm not that dumb, you know!
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #11
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Saving color setting

    I just thought it would save you the trouble of saying: "so what is the problem, exactly?"

Similar Threads

  1. SETTING THE TEXT COLOR FOR QMessageBox
    By vinkakarun in forum Newbie
    Replies: 2
    Last Post: 5th November 2009, 16:32
  2. setting background color and repositioning
    By mind_freak in forum Qt Programming
    Replies: 2
    Last Post: 11th August 2009, 06:22
  3. setting background color of QMessageBox
    By wagmare in forum Qt Programming
    Replies: 7
    Last Post: 23rd May 2009, 13:26
  4. QTreeWidgetItem setting color for the text
    By arjunasd in forum Qt Programming
    Replies: 1
    Last Post: 29th August 2007, 17:22
  5. setting text also changes color?
    By drhex in forum Qt Programming
    Replies: 5
    Last Post: 22nd November 2006, 16:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.