Results 1 to 3 of 3

Thread: Problem with converting a QSettings value to QString

  1. #1
    Join Date
    Mar 2011
    Posts
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Problem with converting a QSettings value to QString

    This is my code:
    Qt Code:
    1. fileName = QFileDialog::getOpenFileName(this, tr("Open Image File"), QDir::currentPath());//QString
    2. settings = new QSettings("Something", "Some Thing");//QSettings
    3. settings->setValue("lastimage", fileName);
    To copy to clipboard, switch view to plain text mode 

    The value appears in the Windows registry as it should be, but when i try to convert it back to QString it returns an empty string (the value field in the registry key is empty).
    Is there something wrong with this code:

    Qt Code:
    1. fileName = settings->value("lastimage").toString();
    To copy to clipboard, switch view to plain text mode 

    or do I have to do something else ?
    Last edited by high_flyer; 27th April 2011 at 09:27. Reason: code tags

  2. #2
    Join Date
    Jan 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem with converting a QSettings value to QString

    I had the same problem but figured it out. I was setting the value on a button click and retrieving in constructor of main window. I am not using QSettings constructor parameters but instead using QCoreApplication's setOrganizationName() and setApplicationName().

    The problem window was being created before I was setting applications parameters, fixing the order fixed the issue. So overall the solution was:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. // these functions should be here
    9. a.setOrganizationName("Organization");
    10. a.setApplicationName("MyApp");
    11.  
    12. MainWindow w;
    13. w.show();
    14.  
    15. // not here (where I had them previously.
    16.  
    17. return a.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem with converting a QSettings value to QString

    Perhaps you are retrieving the value through another QSettings instance that does not have the same organisation and application name.

Similar Threads

  1. converting uint to QString
    By Yayati.Ekbote in forum Newbie
    Replies: 1
    Last Post: 14th April 2010, 14:43
  2. Converting a QString to a LPCTSTR?
    By dobedidoo in forum Qt Programming
    Replies: 3
    Last Post: 7th December 2009, 15:27
  3. Problem in converting QString to QChar array?
    By KaKa in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2007, 01:38
  4. Converting u_char to QString
    By merlvingian in forum Newbie
    Replies: 7
    Last Post: 29th September 2006, 01:11
  5. QString to char* not converting
    By DPinLV in forum Qt Programming
    Replies: 17
    Last Post: 6th August 2006, 13:15

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.