Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Save changes after closing a form.
Forum Updated to NodeBB v4.3 + New Features

Save changes after closing a form.

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 3.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Massinissa
    wrote on last edited by
    #1

    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

    Massinissa Bandou Ing.jr

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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).

      (Z(:^

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Massinissa
        wrote on last edited by
        #3

        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

        Massinissa Bandou Ing.jr

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          QMartin
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Massinissa
            wrote on last edited by
            #5

            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

            Massinissa Bandou Ing.jr

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              QMartin
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Massinissa
                wrote on last edited by
                #7

                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

                Massinissa Bandou Ing.jr

                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  QMartin
                  wrote on last edited by
                  #8

                  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?

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Massinissa
                    wrote on last edited by
                    #9

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

                    Massi

                    Massinissa Bandou Ing.jr

                    1 Reply Last reply
                    0
                    • Q Offline
                      Q Offline
                      QMartin
                      wrote on last edited by
                      #10

                      You're welcome! Good job.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved