<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QSettings doesn&#x27;t load the value]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">My header file:</p>
<pre><code>#ifndef DIALOGTEST_H
#define DIALOGTEST_H

#include &lt;QDialog&gt;

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
</code></pre>
<p dir="auto">The implementation:</p>
<pre><code>#include "DialogTest.h"
#include &lt;QPushButton&gt;
#include &lt;QComboBox&gt;
#include &lt;QHBoxLayout&gt;
#include &lt;QVBoxLayout&gt;
#include &lt;QMessageBox&gt;
#include &lt;QSettings&gt;
#include &lt;QLabel&gt;
#include &lt;QDebug&gt;

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("&amp;Save"));

    topHLayout = new QHBoxLayout;
    topHLayout-&gt;addWidget(select);
    topHLayout-&gt;addWidget(cboBox);

    middleHLayout = new QHBoxLayout;
    middleHLayout-&gt;addWidget(ItemSelected);
    middleHLayout-&gt;addStretch();

    bottomHLayout = new QHBoxLayout;
    bottomHLayout-&gt;addStretch();
    bottomHLayout-&gt;addWidget(save);

    mainLayout = new QVBoxLayout;
    mainLayout-&gt;addLayout(topHLayout);
    mainLayout-&gt;addLayout(middleHLayout);
    mainLayout-&gt;addLayout(bottomHLayout);

    setLayout(mainLayout);
    
    init();
    load();

    connect(save, &amp;QPushButton::clicked, this, &amp;DialogTest::saveBtnClicked);
    connect(cboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(itemChanged(int)));
}

void DialogTest::saveBtnClicked()
{
   QSettings settings;
   settings.setValue("settings", cboBox-&gt;currentIndex());
   QMessageBox::information(this, "Save", "Your item is saved, Please re-open the application");
}

void DialogTest::itemChanged(int index)
{
  ItemSelected-&gt;setText(QString::number(index) + " = " + cboBox-&gt;currentText());
}

void DialogTest::init()
{
    cboBox-&gt;clear();

    for (int i=0; i&lt;10; i++)
        cboBox-&gt;addItem("Item number " + QString::number(i));
}

void DialogTest::load()
{
   QSettings settings;
   QVariant value = settings.value("settings",0);

   bool ok;
   int index = value.toInt(&amp;ok);

   if(!ok)
   {
       QMessageBox::critical(this, "Error", "Error in loading the file");
       return;
   }

   if(index &lt; cboBox-&gt;count()) 
    cboBox-&gt;setCurrentIndex(index);
  
   else 
    cboBox-&gt;setCurrentIndex(0);
}
</code></pre>
<p dir="auto">The problem is that the program can't return the value and it's always "Item number 0"!</p>
]]></description><link>https://forum.qt.io/topic/114669/qsettings-doesn-t-load-the-value</link><generator>RSS for Node</generator><lastBuildDate>Mon, 02 Nov 2026 15:22:07 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/114669.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 10 May 2020 20:43:03 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QSettings doesn&#x27;t load the value on Mon, 11 May 2020 19:04:19 GMT]]></title><description><![CDATA[<p dir="auto">If you want to have your connected widgets working as expected, you should connect them before loading your saved settings.</p>
]]></description><link>https://forum.qt.io/post/594100</link><guid isPermaLink="true">https://forum.qt.io/post/594100</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Mon, 11 May 2020 19:04:19 GMT</pubDate></item><item><title><![CDATA[Reply to QSettings doesn&#x27;t load the value on Mon, 11 May 2020 19:07:45 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a></p>
<p dir="auto">Wow, Mr. Gaist, perfect. It solved that mysterious problem. Thanks.</p>
]]></description><link>https://forum.qt.io/post/594103</link><guid isPermaLink="true">https://forum.qt.io/post/594103</guid><dc:creator><![CDATA[qcoderpro]]></dc:creator><pubDate>Mon, 11 May 2020 19:07:45 GMT</pubDate></item><item><title><![CDATA[Reply to QSettings doesn&#x27;t load the value on Mon, 11 May 2020 19:04:19 GMT]]></title><description><![CDATA[<p dir="auto">If you want to have your connected widgets working as expected, you should connect them before loading your saved settings.</p>
]]></description><link>https://forum.qt.io/post/594100</link><guid isPermaLink="true">https://forum.qt.io/post/594100</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Mon, 11 May 2020 19:04:19 GMT</pubDate></item><item><title><![CDATA[Reply to QSettings doesn&#x27;t load the value on Sun, 10 May 2020 21:24:42 GMT]]></title><description><![CDATA[<p dir="auto">thanks, I use:</p>
<pre><code>QSettings settings("MySoft", "Star Runner");
</code></pre>
<p dir="auto">And now it works. But when the program is re-run and it returns the value saved for the <em>combobox</em>, the label won't change while the slot <strong>itemChanged(int index)</strong> must be called by the second connection, populating the label!</p>
]]></description><link>https://forum.qt.io/post/593902</link><guid isPermaLink="true">https://forum.qt.io/post/593902</guid><dc:creator><![CDATA[qcoderpro]]></dc:creator><pubDate>Sun, 10 May 2020 21:24:42 GMT</pubDate></item><item><title><![CDATA[Reply to QSettings doesn&#x27;t load the value on Sun, 10 May 2020 21:05:58 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">Did you properly call the various application related information setters like shown <a href="https://doc.qt.io/qt-5/qsettings.html#details" target="_blank" rel="noopener noreferrer nofollow ugc">QSettings details</a>.</p>
]]></description><link>https://forum.qt.io/post/593899</link><guid isPermaLink="true">https://forum.qt.io/post/593899</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Sun, 10 May 2020 21:05:58 GMT</pubDate></item><item><title><![CDATA[Reply to QSettings doesn&#x27;t load the value on Sun, 10 May 2020 21:07:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/qcoderpro">@<bdi>qcoderpro</bdi></a> said in <a href="/post/593895">QSettings doesn't load the value</a>:</p>
<blockquote>
<p dir="auto"><code>QSettings settings;</code></p>
</blockquote>
<p dir="auto">Since you don't set organization name etc., <a href="https://doc.qt.io/qt-5/qsettings.html#QSettings-4" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/qsettings.html#QSettings-4</a></p>
<blockquote>
<p dir="auto">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.</p>
</blockquote>
<p dir="auto">Check <code>status()</code>, you should see an error on your calls.</p>
]]></description><link>https://forum.qt.io/post/593898</link><guid isPermaLink="true">https://forum.qt.io/post/593898</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Sun, 10 May 2020 21:07:12 GMT</pubDate></item></channel></rss>