Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSettings doesn't read settings at the start of the programme SOLVED
Qt 6.11 is out! See what's new in the release blog

QSettings doesn't read settings at the start of the programme SOLVED

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vittorio
    wrote on last edited by
    #1

    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_OBJECT

    public:
    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(&#41;;
    

    }
    @

    I have no idea, what I'm doing wrong, could one of the experts give me a tip?

    thanks in advance,
    vittorio

    1 Reply Last reply
    0
    • D Offline
      D Offline
      danieltm64
      wrote on last edited by
      #2

      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&#40;&#41;;
      

      }@

      Also, I think you don't need to #include <QSettings> in main.cpp.

      Daniel

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Vittorio
        wrote on last edited by
        #3

        you are right in both cases, now it seems to work! uuhps, a little bit embarrassing... :-)

        thanks a lot!!

        greetings, vittorio

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 !

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • V Offline
            V Offline
            Vittorio
            wrote on last edited by
            #5

            thx, SGaist!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved