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 load the value
Qt 6.11 is out! See what's new in the release blog

QSettings doesn't load the value

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 619 Views 2 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.
  • Q Offline
    Q Offline
    qcoderpro
    wrote on last edited by
    #1

    Hi,

    My header file:

    #ifndef DIALOGTEST_H
    #define DIALOGTEST_H
    
    #include <QDialog>
    
    class QPushButton;
    class QComboBox;
    class QVBoxLayout;
    class QHBoxLayout;
    class QLabel;
    
    class DialogTest : public QDialog
    {
        Q_OBJECT
    
    public:
        DialogTest(QWidget *parent = nullptr);
    
    private slots:
        void saveBtnClicked();
        void itemChanged(int);
        void init();
        void load();
    
    private:
        QComboBox* cboBox = nullptr;
    
        QHBoxLayout* topHLayout = nullptr;
        QHBoxLayout* middleHLayout = nullptr;
        QHBoxLayout* bottomHLayout = nullptr;
        QVBoxLayout* mainLayout = nullptr;
    
        QLabel* select = nullptr;
        QLabel* ItemSelected = nullptr;
        QPushButton* save = nullptr;
    };
    #endif // DIALOGTEST_H
    

    The implementation:

    #include "DialogTest.h"
    #include <QPushButton>
    #include <QComboBox>
    #include <QHBoxLayout>
    #include <QVBoxLayout>
    #include <QMessageBox>
    #include <QSettings>
    #include <QLabel>
    #include <QDebug>
    
    DialogTest::DialogTest(QWidget *parent)
        : QDialog(parent)
    {
        select = new QLabel(tr("Select an Item"));
        cboBox = new QComboBox;
        ItemSelected = new QLabel(tr("Selected Item"));
        save = new QPushButton(tr("&Save"));
    
        topHLayout = new QHBoxLayout;
        topHLayout->addWidget(select);
        topHLayout->addWidget(cboBox);
    
        middleHLayout = new QHBoxLayout;
        middleHLayout->addWidget(ItemSelected);
        middleHLayout->addStretch();
    
        bottomHLayout = new QHBoxLayout;
        bottomHLayout->addStretch();
        bottomHLayout->addWidget(save);
    
        mainLayout = new QVBoxLayout;
        mainLayout->addLayout(topHLayout);
        mainLayout->addLayout(middleHLayout);
        mainLayout->addLayout(bottomHLayout);
    
        setLayout(mainLayout);
        
        init();
        load();
    
        connect(save, &QPushButton::clicked, this, &DialogTest::saveBtnClicked);
        connect(cboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(itemChanged(int)));
    }
    
    void DialogTest::saveBtnClicked()
    {
       QSettings settings;
       settings.setValue("settings", cboBox->currentIndex());
       QMessageBox::information(this, "Save", "Your item is saved, Please re-open the application");
    }
    
    void DialogTest::itemChanged(int index)
    {
      ItemSelected->setText(QString::number(index) + " = " + cboBox->currentText());
    }
    
    void DialogTest::init()
    {
        cboBox->clear();
    
        for (int i=0; i<10; i++)
            cboBox->addItem("Item number " + QString::number(i));
    }
    
    void DialogTest::load()
    {
       QSettings settings;
       QVariant value = settings.value("settings",0);
    
       bool ok;
       int index = value.toInt(&ok);
    
       if(!ok)
       {
           QMessageBox::critical(this, "Error", "Error in loading the file");
           return;
       }
    
       if(index < cboBox->count()) 
        cboBox->setCurrentIndex(index);
      
       else 
        cboBox->setCurrentIndex(0);
    }
    

    The problem is that the program can't return the value and it's always "Item number 0"!

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

      If you want to have your connected widgets working as expected, you should connect them before loading your saved settings.

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

      Q 1 Reply Last reply
      1
      • Q qcoderpro

        Hi,

        My header file:

        #ifndef DIALOGTEST_H
        #define DIALOGTEST_H
        
        #include <QDialog>
        
        class QPushButton;
        class QComboBox;
        class QVBoxLayout;
        class QHBoxLayout;
        class QLabel;
        
        class DialogTest : public QDialog
        {
            Q_OBJECT
        
        public:
            DialogTest(QWidget *parent = nullptr);
        
        private slots:
            void saveBtnClicked();
            void itemChanged(int);
            void init();
            void load();
        
        private:
            QComboBox* cboBox = nullptr;
        
            QHBoxLayout* topHLayout = nullptr;
            QHBoxLayout* middleHLayout = nullptr;
            QHBoxLayout* bottomHLayout = nullptr;
            QVBoxLayout* mainLayout = nullptr;
        
            QLabel* select = nullptr;
            QLabel* ItemSelected = nullptr;
            QPushButton* save = nullptr;
        };
        #endif // DIALOGTEST_H
        

        The implementation:

        #include "DialogTest.h"
        #include <QPushButton>
        #include <QComboBox>
        #include <QHBoxLayout>
        #include <QVBoxLayout>
        #include <QMessageBox>
        #include <QSettings>
        #include <QLabel>
        #include <QDebug>
        
        DialogTest::DialogTest(QWidget *parent)
            : QDialog(parent)
        {
            select = new QLabel(tr("Select an Item"));
            cboBox = new QComboBox;
            ItemSelected = new QLabel(tr("Selected Item"));
            save = new QPushButton(tr("&Save"));
        
            topHLayout = new QHBoxLayout;
            topHLayout->addWidget(select);
            topHLayout->addWidget(cboBox);
        
            middleHLayout = new QHBoxLayout;
            middleHLayout->addWidget(ItemSelected);
            middleHLayout->addStretch();
        
            bottomHLayout = new QHBoxLayout;
            bottomHLayout->addStretch();
            bottomHLayout->addWidget(save);
        
            mainLayout = new QVBoxLayout;
            mainLayout->addLayout(topHLayout);
            mainLayout->addLayout(middleHLayout);
            mainLayout->addLayout(bottomHLayout);
        
            setLayout(mainLayout);
            
            init();
            load();
        
            connect(save, &QPushButton::clicked, this, &DialogTest::saveBtnClicked);
            connect(cboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(itemChanged(int)));
        }
        
        void DialogTest::saveBtnClicked()
        {
           QSettings settings;
           settings.setValue("settings", cboBox->currentIndex());
           QMessageBox::information(this, "Save", "Your item is saved, Please re-open the application");
        }
        
        void DialogTest::itemChanged(int index)
        {
          ItemSelected->setText(QString::number(index) + " = " + cboBox->currentText());
        }
        
        void DialogTest::init()
        {
            cboBox->clear();
        
            for (int i=0; i<10; i++)
                cboBox->addItem("Item number " + QString::number(i));
        }
        
        void DialogTest::load()
        {
           QSettings settings;
           QVariant value = settings.value("settings",0);
        
           bool ok;
           int index = value.toInt(&ok);
        
           if(!ok)
           {
               QMessageBox::critical(this, "Error", "Error in loading the file");
               return;
           }
        
           if(index < cboBox->count()) 
            cboBox->setCurrentIndex(index);
          
           else 
            cboBox->setCurrentIndex(0);
        }
        

        The problem is that the program can't return the value and it's always "Item number 0"!

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #2

        @qcoderpro said in QSettings doesn't load the value:

        QSettings settings;

        Since you don't set organization name etc., https://doc.qt.io/qt-5/qsettings.html#QSettings-4

        If QCoreApplication::setOrganizationName() and QCoreApplication::setApplicationName() has not been previously called, the QSettings object will not be able to read or write any settings, and status() will return AccessError.

        Check status(), you should see an error on your calls.

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

          Hi,

          Did you properly call the various application related information setters like shown QSettings details.

          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
          2
          • Q Offline
            Q Offline
            qcoderpro
            wrote on last edited by qcoderpro
            #4

            thanks, I use:

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

            And now it works. But when the program is re-run and it returns the value saved for the combobox, the label won't change while the slot itemChanged(int index) must be called by the second connection, populating the label!

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

              If you want to have your connected widgets working as expected, you should connect them before loading your saved settings.

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

              Q 1 Reply Last reply
              1
              • SGaistS SGaist

                If you want to have your connected widgets working as expected, you should connect them before loading your saved settings.

                Q Offline
                Q Offline
                qcoderpro
                wrote on last edited by
                #6

                @SGaist

                Wow, Mr. Gaist, perfect. It solved that mysterious problem. Thanks.

                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