QSettings doesn't read settings at the start of the programme SOLVED
-
hi, all,
my problem is as follows:
I tried to port my software from Qt 4.8.2 to 5.3.2. The QSettings didn't work anymore, so I wrote a little piece of code to understand what happens and to fix the error, but unfortunately with no success. I am able to write values and read them from the conf-file (I'm using linux) "manually" (which means by clicking a push-button and the depending slot), but not in the constructor, when I start the programme.
mainwindow.h:
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();private slots:
void on_pushButton_save_clicked();private:
Ui::MainWindow *ui;
};#endif // MAINWINDOW_H
@mainwindow.cpp:
@#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QSettings>
// constructor; here I try to read values from settings and
// fill out the linedit 'lineEdit_read' when starting the
// programme, but nothing happens
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{ui->setupUi(this); QSettings settings; QString read = settings.value("InsertedText").toString(); ui->lineEdit_read->setText(read);
}
MainWindow::~MainWindow()
{
delete ui;
}// slot for push button; after clicking the lineEdit_insert will
// be red, written to the settings file and red once again and
// written to the line_Edit_read; this function works properly
void MainWindow::on_pushButton_save_clicked()
{
QString insertion = ui->lineEdit_insert->text();QSettings settings; settings.setValue("InsertedText", insertion); QString read = settings.value("InsertedText").toString(); ui->lineEdit_read->setText(read);
}@
main.cpp:
@#include "mainwindow.h"
#include <QApplication>
#include <QSettings>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;QCoreApplication::setOrganizationName("MySoft"); QCoreApplication::setApplicationName("Star Runner"); w.show(); return a.exec();
}
@I have no idea, what I'm doing wrong, could one of the experts give me a tip?
thanks in advance,
vittorio -
You seem to be constructing your main window before you specify the organization name and application name. Try moving line 8 of your main.cpp file just after line 11, in the end it should look like this:
@#include "mainwindow.h"
#include <QApplication>
#include <QSettings>int main(int argc, char *argv[])
{
QApplication a(argc, argv);QCoreApplication::setOrganizationName("MySoft"); QCoreApplication::setApplicationName("Star Runner"); MainWindow w; w.show(); return a.exec();
}@
Also, I think you don't need to #include <QSettings> in main.cpp.
-
you are right in both cases, now it seems to work! uuhps, a little bit embarrassing... :-)
thanks a lot!!
greetings, vittorio
-
Hi,
No need for the QSettings include in main, only include what you will actually use. But danieltm64 has a point, always do the complete Q(Gui/Core)Application setup before the rest
Happy coding !
-
thx, SGaist!