PDA

View Full Version : Problem with QSettings



A_Stone
26th May 2013, 08:20
I wrote the application using Qt 4.8 . Now I need rewrite it into Qt 5.0. Using 4.8 QSettings worked right and now it doesn't. I try to save setting to settings.ini and read settings from there, but QSettings doesn't want to write and read.

My code:
#include "settingsdialog.h"
#include "ui_settingsdialog.h"
#include "QMessageBox"
#include "QSettings"

SettingsDialog::SettingsDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SettingsDialog)
{
ui->setupUi(this);
ui->admin_passwordEdit->setEchoMode(QLineEdit::Password);
connect(ui->save_settingsButton,SIGNAL(clicked()),this,SLOT(sa ve_setting()));
}

SettingsDialog::~SettingsDialog()
{
delete ui;
}

void SettingsDialog::save_setting()
{
QSettings setting("setting.ini",QSettings::IniFormat);
setting->setIniCodec("UTF-8");
QString host = ui->hostEdit->text();
QString db_name = ui->db_nameEdit->text();
QString admin_login=ui->hostEdit->text();
QString admin_password =ui->hostEdit->text();
if (!host.isEmpty()) setting->setValue("MySQL/host", host);
if (!db_name.isEmpty()) setting->setValue("MySQL/db_name", db_name);
if (!admin_login.isEmpty()) setting->setValue("MySQL/admin_login",admin_login );
if (!admin_password.isEmpty()) setting->setValue("MySQL/admin_password",admin_password );
setting->sync();

close();
}

Santosh Reddy
26th May 2013, 08:32
This code will not even compile. In save_setting() function setting is defined as variable and later is used as if it is a pointer?

A_Stone
26th May 2013, 08:52
This code will not even compile. In save_setting() function setting is defined as variable and later is used as if it is a pointer?

You are right. Before my tried to change it's been like
setting.setValue("MySQL/host", host); But all the same Qsettings doesn't write new settings in ini-file

ChrisW67
26th May 2013, 09:01
You are using a relative file path to the INI file. Make sure the working directory of the process is what you think it is.

A_Stone
26th May 2013, 09:35
You are using a relative file path to the INI file. Make sure the working directory of the process is what you think it is.
In Qt 4.8 it works.In Qt5.0 I tried to write
QString path = QApplication::applicationDirPath() + "/settings.ini"; QSettings setting(path, QSettings::IniFormat); It did't help.

A_Stone
26th May 2013, 12:49
I solved the promblem. The programm was on a flash card. When I copied it on my computer, QSettings again started to work.