Save changes after closing a form.



  • Hello everyone,

    I created a mainwindow project using NetBeans which it has QtDesigner. I connected a signal to a QPushButton to show a new window (Settings.ui) as Dialog with two push buttons (Ok/Cancel). Here is my code for opening the dialog:
    The dialog was set up in mySettings class.

    @ mySettings a;
    if(a.exec()){
    a.close();
    }else{
    a.show();
    a.raise();
    a.activateWindow();
    a.exec();
    } @

    I have some widgets on my dialog (radio button, check button), how can I save the changes or modifications made by the user when he press Ok button? Because when I open the dialog again, all the widgets are set to default. Any documentations, hints or examples will be greatly appreciated!

    I would really appreciate your help.

    Massi


  • Moderators

    You need to save it somewhere. Either preserve the dialog between invocations (store as class member variable, preferably a pointer), or use QSettings to save the choices the user has made, then load them when the dialog is restarted (this will preserve them even between application runs).



  • thank you for replying!

    I have tried QSettings to write an ini file as you can see below.
    @[Basic_Registartion]
    same_modality=false
    different_modality=true@

    But it doesn't work when it comes to read it every time I try to open my dialog window. I have two radio buttons and i want to set the selected values accordingly.

    @ mySettings a;
    if(a.exec()){
    a.close();
    }else{
    a.raise();
    QSettings settings("_set.ini", QSettings::IniFormat);
    settings.beginGroup("Basic_Registration");
    a.radioButton_4->setChecked(settings.value("same_modality").toBool());
    a.radioButton_5->setChecked(settings.value("different_modality").toBool());
    settings.endGroup();
    a.exec();
    } @

    thank you for your help!

    Massi



  • Hello!

    How have you saved your settings when pressing the OK button? Does it look something like this?

    @QSettings yourSettings;
    yourSettings.setValue("same_modality", a.radioButton_4->isChecked());
    yourSettings.setValue("different_modality", a.radioButton_5->isChecked());@

    And read them like you just posted.



  • Hello,

    This is exactly what I have done to read the settings. I don't know where is the problem but I guess it's related to the dialog window. It stops working when I added (a.ReadSettings()). By the way, I have a push button in my mainwidow (ImageCoregistration) used to open the dialog window.

    @void ImageCoregistration::OpenDialog(){
    mySettings a;
    a.ReadSettings();
    a.raise();
    a.exec();
    }@

    @void mySettings::WriteSettings(){
    QSettings* settings = new QSettings("_set.ini",QSettings::IniFormat);
    settings->beginGroup("Basic_Registration");
    settings->setValue("same_modality", swidget.radioButton_4->isChecked());
    settings->setValue("different_modality", swidget.radioButton_5->isChecked());
    settings->endGroup();
    }@

    and for reading:

    @void mySettings::ReadSettings(){
    mySettings a;
    QSettings settings("_set.ini", QSettings::IniFormat);
    settings.beginGroup("Basic_Registration");
    a.radioButton_4->setChecked(settings.value("same_modality").toBool());
    a.radioButton_5->setChecked(settings.value("different_modality").toBool());
    settings.endGroup();
    }@

    Thx for your help!

    Massi



  • Hi!

    Ok, let's try something else. First of all I think you have a memory leak on WriteSettings, as you are not deleting your QSettings variable (just create it on the stack). Next, instead of calling readSettings inside your mainWindow after creating the dialog, do it in the dialog's constructor. To make thing clear: on your ImageCoresgistration:

    @void ImageCoregistration::OpenDialog(){
    mySettings a;
    a.raise();
    a.exec();
    }@

    And in your dialog:

    @mySettings::mySettings(){
    //whatever you are doing
    ReadSettings();
    }

    mySettings::whenYouPressYourOkButtonForClosingTheDialog(){
    //everything you typed here
    WriteSettings();
    }@

    Does it work?

    Good luck



  • Thank you for your quick reply QMartin!

    I have tried to read the settings from the constructor but the problem still persists. It just won't open and stop the program!
    I'm using QtDesigner.

    This is what I have in my .h file.
    @class mySettings : public QDialog, public Ui::Settings
    {
    Q_OBJECT
    public:
    mySettings();
    void ReadSettings();
    public slots:
    void WriteSettings();
    private:
    Ui::Settings swidget;
    };@

    my constructor has:
    @mySettings::mySettings(){
    swidget.setupUi(this);
    ReadSettings();
    }@

    The writing is good but the ReadSettings() function freezes my program.

    Thank you again!!

    Massi



  • Ok!

    In your ReadSettings() function, try removing mySettings declaration, you don't need it because the dialog "already exists" in your class as swidget (I think, just as you used it in WriteSettings()). So:

    @void mySettings::ReadSettings(){
    QSettings settings("_set.ini", QSettings::IniFormat);
    settings.beginGroup("Basic_Registration");
    swidget.radioButton_4->setChecked(settings.value("same_modality").toBool());
    swidget.radioButton_5->setChecked(settings.value("different_modality").toBool());
    settings.endGroup();
    }@

    What happens now?



  • Thanks QMartin!! it works.
    I really appreciate your help!

    Massi



  • You're welcome! Good job.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.