Pregunta

I've been working on my first gui application. I need to store some values for everything to function so I decided to use QSettings and learn how to use windows registry. I have found an example for the size and position so I have do have a slight grasp on what is going on here, but for some reason I cannot get it to work when I try on my own. I have been struggling with this for a few weeks now and just cant find any good references on how to add my own strings to the registry. Below is all the pertinent code needed for this to happen. Any help or point in the right direction will be greatly appreciated. Also I have the fundamentals of Qt 4 or something like that but it was not that much help to me in this instance, so if anyone has any quick references to some quality related articles or more comprehensive works on the more advanced QT topics i would be interested to hear them, not to get to far off the topic at hand though, I'm sure there's tons of good books out there.

MainWindow.cpp

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
createActions();
createMenus();
createMainWidget();
createIcons();
createDocks();
createStatusBar();

setMinimumSize(950,600);
setWindowTitle(tr("Black Ops Toolbox: Vrs. 0.0.0.12.01"));

QString defaultDirPath = "";

readSettings();
}

void MainWindow::readSettings()
{
QSettings settings;
QPoint pos = settings.value("pos", QPoint(25,25)).toPoint();
move(pos);
QSize size = settings.value("size",QSize(1200,900)).toSize();
resize(size);
QString defaultDirPath = settings.value("defaultDirPath", QString("c:/programfiles/")).toString();

}

void MainWindow::writeSettings()
{
QSettings settings;
settings.setValue("pos",pos());
settings.setValue("size",size());
settings.setValue("defaultDirPath", QVariant(QString *defaultDirPath).toString());
}

void MainWindow::closeEvent(QCloseEvent *event)
{
writeSettings();
}

MainWindow.h

class MainWindow : public QMainWindow
{
Q_OBJECT

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

protected:
void closeEvent(QCloseEvent *event);

private:
void createActions();
void createActionEvents();
void createMenus();
void createMainWidget();
void createIcons();
void createDocks();
void createStatusBar();
void readSettings();
void writeSettings();

QString *defaultDirPath;
};
¿Fue útil?

Solución

I really did not use QSettings yet, but from documentation it seems that in your code some information misses.

In particular, you must give (from Basic usage section), organization name and application name, using the QSettings constructor, like

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

or using QCoreApplication settings and then the default QSettings constructor

 QCoreApplication::setOrganizationName("MySoft");
 QCoreApplication::setOrganizationDomain("mysoft.com");
 QCoreApplication::setApplicationName("Star Runner");
 ...
 QSettings settings;

Have you tried one of these methods to create your QSettings?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top