Question

I've wrote a simple application to use QSettings. Can someone tell me what wrong I'm doing here..

mainwindow.h

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    QWidget *pMainWidget;
    QHBoxLayout *pMainLayout;
    QSettings *pSetting;

    QLabel *pLabel;
    QPushButton *pButtonShow;
    QPushButton *pButtonSet;
    QLineEdit *pLineEdit;

    QString pSettingFile;

public slots:
    void showSettingData();
    void setData();
};

mainwindow.cpp

#include "mainwindow.h"
#include <QtCore/QCoreApplication>
#include <QApplication>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{

    pMainWidget = new QWidget (parent);
    pMainLayout = new QHBoxLayout(pMainWidget);

    pLabel      = new QLabel("Output comes here",pMainWidget);
    pLineEdit   = new QLineEdit();
    pButtonShow = new QPushButton("Show", pMainWidget);
    pButtonSet  = new QPushButton("Set", pMainWidget);

    setCentralWidget(pMainWidget);
    pMainWidget->setLayout(pMainLayout);
    pMainLayout->addWidget(pLabel);
    pMainLayout->addWidget(pButtonShow);
    pMainLayout->addWidget(pLineEdit);
    pMainLayout->addWidget(pButtonSet);

    pSettingFile = QApplication::applicationDirPath()+"settings.ini";

    QObject::connect(pButtonShow, SIGNAL(clicked()), this, SLOT(showSettingData()));
    QObject::connect(pButtonSet, SIGNAL(clicked()), this, SLOT(setData()));

}

MainWindow::~MainWindow()
{

}

void MainWindow::setData()
{
    QSettings Setting(pSettingFile, QSettings::NativeFormat);

    QString data = pLineEdit->text();
    Setting.setValue("baseurl", data);
}

void MainWindow::showSettingData()
{
    QSettings Setting(pSettingFile, QSettings::NativeFormat);

    if (Setting.contains("baseurl"))
    {
        QString data = Setting.value("baseurl").toString();
        pLabel->setText(data);
    }

}

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

When I debug, it doesn't returns false for "Setting.contains("baseurl")"... Do we have to create the setting file?

Was it helpful?

Solution

The issue is that you are not calling sync after setting the data. Try to insert this line after the setting:

Setting.setValue("baseurl", data);
Setting.sync();

This should not be needed on Windows, but it seems to be necessary on Windows.

Also, as suggested in comments, I would suggest to use QSettings::IniFormat instead of QSettings::NativeFormat if you really want to use files rather than potentially registry on Windows.

You should also consider making the settings object as a class member rather than constructing it all the time.

Also, note that you may be using the wrong path unintentionally because you would have to add the "slash" character explicitly before the "settings.ini" file name. This is just a side note, however.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top