Results 1 to 3 of 3

Thread: QSettings problem?

  1. #1
    Join Date
    Jan 2008
    Posts
    58
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QSettings problem?

    Hi,

    I've made a program on the Mac platform! But Qt is cross platform so I compiled it for the windows platform.

    And it works fine except for one thing, it seems that the program can't handle the settings well on windows.

    Here's an example, my program checks if there's an database in the settings if not popup a dialog and retrieve the save path from the dialog. Create a database check if it exists if so open a connection and run the program.
    This is the code:
    Qt Code:
    1. void Database::loadDatabase()
    2. {
    3. QSettings settings;
    4.  
    5. QString path = settings.value("Database/path").toString();
    6.  
    7. qDebug() << settings.value("Database/path").toString();
    8.  
    9. QFileInfo file(path);
    10.  
    11. if(file.exists()){
    12. //database exists keep on going
    13. qDebug("Database::Database(): Database exists keep on going");
    14.  
    15. //create database
    16. database = QSqlDatabase::addDatabase("QSQLITE", defaultCon);
    17.  
    18. //set database path
    19. database.setDatabaseName(path);
    20.  
    21. //try to open the database if needed
    22. if(!database.isOpen())
    23. database.open();
    24.  
    25.  
    26. //is the database open, if so continue
    27. if(database.isOpen()){
    28.  
    29. QStringList tables = database.tables();
    30. //check if the tables exists
    31. if(!tables.contains("clients", Qt::CaseInsensitive) && !tables.contains("orders", Qt::CaseInsensitive) && !tables.contains("articles", Qt::CaseInsensitive)){
    32. //tables doesn't exists yet, create the tables
    33. qDebug("Database::Database(): There are no tables, create some tables");
    34.  
    35. //create the tables, and if it went wrong popup the messagebox
    36. if(!this->createTables())
    37. QMessageBox::critical(0, tr("Database error"), tr("Kan geen tabellen genereren") );
    38.  
    39. }
    40.  
    41. }else{
    42.  
    43. //couldn't connect to the database
    44. qDebug("Database::Database(): Connection failed");
    45. QMessageBox::critical(0, tr("Database connectie error"), tr("Kan geen verbinding maken met de database, neem contact op met de ontwikkelaar of probeer het nog een keer."));
    46.  
    47. }
    48.  
    49. }else{
    50. //There is no database yet, popup the file dialog
    51. qDebug("Database::Database(): There is no database yet, popup the file dialog");
    52. this->databaseDialog();
    53. }
    54.  
    55.  
    56. }
    57.  
    58. void Database::databaseDialog(){
    59. qDebug("Database::databaseDialog(): constructing the dialog..");
    60.  
    61. QStringList savePath;
    62.  
    63. QFileDialog dialog;
    64. dialog.setDirectory(QDir::current());
    65. dialog.setAcceptMode(QFileDialog::AcceptSave);
    66. dialog.setWindowTitle("Database opslag plaats");
    67.  
    68. //add the db extension to the filename
    69. dialog.setDefaultSuffix("db");
    70.  
    71. //if somebody presses cancel quit the application
    72. QObject::connect(&dialog, SIGNAL(rejected()), qApp, SLOT(quit()));
    73.  
    74. //popup the dialog
    75. if(dialog.exec())
    76. savePath = dialog.selectedFiles();
    77.  
    78. if(savePath.count() == 1){
    79. qDebug("Put the path in the settings file");
    80.  
    81. //put it in the settings
    82. QSettings settings;
    83. settings.setValue("Database/path", savePath.at(0));
    84.  
    85. //create an temporary database object, just for creating the file....
    86. QSqlDatabase tmpDatabase;
    87. tmpDatabase = QSqlDatabase::addDatabase("QSQLITE", QLatin1String("tempConnection"));
    88.  
    89. //create database file
    90. tmpDatabase.setDatabaseName(savePath.at(0));
    91. tmpDatabase.open();
    92. tmpDatabase.close();
    93. tmpDatabase.removeDatabase(savePath.at(0));
    94.  
    95. //try loading the database again
    96. this->loadDatabase();
    97.  
    98.  
    99. }
    100.  
    101. }
    To copy to clipboard, switch view to plain text mode 

    And the first time you run the program there's a dialog who's asking you the save path of your database. Until now everything went as it suppose to went but than if you hit the save button the database is created but there's another dialog popping up asking the same thing, so it looks like the settings aren't saved.

    After hitting cancel the window opens at the size you closed it, and if there are no settings open on normal size. On mac everything works, the first time you run the program it opens with the width and height of the design. And the second time it opens with the saved width and height. But that doesn't work neither on windows. It opens as small as possible.

    This is the settings code:
    Qt Code:
    1. #pragma mark settings
    2.  
    3. void MainWindow::writeSettings()
    4. {
    5. QSettings settings;
    6. settings.setValue("MainWindow/Size", size());
    7. settings.setValue("MainWindow/Properties", saveState());
    8. }
    9.  
    10. void MainWindow::readSettings()
    11. {
    12. QSettings settings;
    13. resize(settings.value("MainWindow/Size", sizeHint()).toSize());
    14. restoreState(settings.value("MainWindow/Properties").toByteArray());
    15. }
    16.  
    17. #pragma mark destructor
    18.  
    19. MainWindow::~MainWindow()
    20. {
    21. //write settings
    22. writeSettings();
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 

    Does somebody know how to solve it?

    Thanks,

    Cyberboy

  2. #2
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: QSettings problem?

    Hi,

    I think for QSettings you need something like this
    Qt Code:
    1. QSettings settings("MySoft", "Star Runner");
    To copy to clipboard, switch view to plain text mode 

    On Windows it is stored in the Registry by default.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSettings problem?

    Quote Originally Posted by cyberboy View Post
    Qt Code:
    1. MainWindow::~MainWindow()
    2. {
    3. //write settings
    4. writeSettings();
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 
    It might be a better idea to store settings during the close event (as advised in QSettings docs).

    Quote Originally Posted by janus View Post
    I think for QSettings you need something like this
    Qt Code:
    1. QSettings settings("MySoft", "Star Runner");
    To copy to clipboard, switch view to plain text mode 
    QSettings has several variations of constructors. The one cyberboy uses, uses the application and organization names of QApplication so he doesn't have to pass them explicitly.
    J-P Nurmi

Similar Threads

  1. QSettings and QGLWidgets
    By Rayven in forum Qt Programming
    Replies: 1
    Last Post: 31st May 2008, 18:01
  2. problem with paint and erase in frame
    By M.A.M in forum Qt Programming
    Replies: 9
    Last Post: 4th May 2008, 20:17
  3. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  4. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.