PDA

View Full Version : QSettings problem?



cyberboy
28th June 2008, 14:02
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:

void Database::loadDatabase()
{
QSettings settings;

QString path = settings.value("Database/path").toString();

qDebug() << settings.value("Database/path").toString();

QFileInfo file(path);

if(file.exists()){
//database exists keep on going
qDebug("Database::Database(): Database exists keep on going");

//create database
database = QSqlDatabase::addDatabase("QSQLITE", defaultCon);

//set database path
database.setDatabaseName(path);

//try to open the database if needed
if(!database.isOpen())
database.open();


//is the database open, if so continue
if(database.isOpen()){

QStringList tables = database.tables();
//check if the tables exists
if(!tables.contains("clients", Qt::CaseInsensitive) && !tables.contains("orders", Qt::CaseInsensitive) && !tables.contains("articles", Qt::CaseInsensitive)){
//tables doesn't exists yet, create the tables
qDebug("Database::Database(): There are no tables, create some tables");

//create the tables, and if it went wrong popup the messagebox
if(!this->createTables())
QMessageBox::critical(0, tr("Database error"), tr("Kan geen tabellen genereren") );

}

}else{

//couldn't connect to the database
qDebug("Database::Database(): Connection failed");
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."));

}

}else{
//There is no database yet, popup the file dialog
qDebug("Database::Database(): There is no database yet, popup the file dialog");
this->databaseDialog();
}


}

void Database::databaseDialog(){
qDebug("Database::databaseDialog(): constructing the dialog..");

QStringList savePath;

QFileDialog dialog;
dialog.setDirectory(QDir::current());
dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setWindowTitle("Database opslag plaats");

//add the db extension to the filename
dialog.setDefaultSuffix("db");

//if somebody presses cancel quit the application
QObject::connect(&dialog, SIGNAL(rejected()), qApp, SLOT(quit()));

//popup the dialog
if(dialog.exec())
savePath = dialog.selectedFiles();

if(savePath.count() == 1){
qDebug("Put the path in the settings file");

//put it in the settings
QSettings settings;
settings.setValue("Database/path", savePath.at(0));

//create an temporary database object, just for creating the file....
QSqlDatabase tmpDatabase;
tmpDatabase = QSqlDatabase::addDatabase("QSQLITE", QLatin1String("tempConnection"));

//create database file
tmpDatabase.setDatabaseName(savePath.at(0));
tmpDatabase.open();
tmpDatabase.close();
tmpDatabase.removeDatabase(savePath.at(0));

//try loading the database again
this->loadDatabase();


}

}


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:


#pragma mark settings

void MainWindow::writeSettings()
{
QSettings settings;
settings.setValue("MainWindow/Size", size());
settings.setValue("MainWindow/Properties", saveState());
}

void MainWindow::readSettings()
{
QSettings settings;
resize(settings.value("MainWindow/Size", sizeHint()).toSize());
restoreState(settings.value("MainWindow/Properties").toByteArray());
}

#pragma mark destructor

MainWindow::~MainWindow()
{
//write settings
writeSettings();

}



Does somebody know how to solve it?

Thanks,

Cyberboy

janus
28th June 2008, 16:48
Hi,

I think for QSettings you need something like this

QSettings settings("MySoft", "Star Runner");

On Windows it is stored in the Registry by default.

jpn
28th June 2008, 21:14
MainWindow::~MainWindow()
{
//write settings
writeSettings();

}


It might be a better idea to store settings during the close event (as advised in QSettings docs).



I think for QSettings you need something like this

QSettings settings("MySoft", "Star Runner");
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.