McLan
McLan

Reputation: 2678

Qt: C++: Restoring geometry of a pop-up dialog-box

I am trying to save the geometry of a pop-up dialog-box and then restore it back whenever I call the pop-up dialog box (while the application is still running).

But I couldn't figure it out.

The code runs without error. But the window of the pop-up keeps changing it position vertically everytime it is open. Unless I close the whole application and then re-opened it again, the pop-up never goes to its original position in the center of the screen.

I am trying to use QcloseEvent, QSettings and restoreGeometry. But something is not right, please help.

Here is MainWindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ui_addmembersdialog.h"
#include "addmembersdialog.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    mpAddMembersDialog = new AddMembersDialog;
    connect(ui->testBtn,SIGNAL(clicked()),this,SLOT(openPopUpForm()));
}

void MainWindow::openPopUpForm(){
    mpAddMembersDialog->readSettings();
    mpAddMembersDialog->exec();
}

Here is AddMembersDialog.cpp:

#include "addmembersdialog.h"
#include "ui_addmembersdialog.h"
#include <QMessageBox>

AddMembersDialog::AddMembersDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddMembersDialog)
{
    ui->setupUi(this);
    connect(ui->closeFormBtn,SIGNAL(clicked()),this,SLOT(Exit()));
}
void AddMembersDialog::Exit()
{
    close();
}

void AddMembersDialog::closeEvent(QCloseEvent *event){
    QSettings settings("DevSuda", "Muwassa");
    settings.setValue("geometery", saveGeometry());
    QDialog::closeEvent(event);
}

void AddMembersDialog::readSettings(){
    QSettings settings("DevSuda", "Muwassa");
    restoreGeometry(settings.value("QDialog/geometry").toByteArray());
}

Upvotes: 1

Views: 684

Answers (1)

A.Fagrell
A.Fagrell

Reputation: 1104

Please compare the key used for the following two lines:

settings.setValue("geometery", saveGeometry());

and

restoreGeometry(settings.value("QDialog/geometry").toByteArray());

"geometry" vs "QDialog/geometry". Should be the same!

also I would predefine your keys/organization/application name in the cpp file such as:

...
static const char * ksOrganization{"DevSuda"};
static const char * ksApp         {"Muwassa"};
static const char * ksKey         {"geometery"};
...
QSettings settings(ksOrganization, ksApp);
settings.setValue(ksKey, saveGeometry());

This would prevent you from typing the key wrong in one place...

Upvotes: 3

Related Questions