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. Saving the whole application state( ```QTableWidget```, ```QPushButton```, ```QProcess```, objects created with ```new```)
Forum Updated to NodeBB v4.3 + New Features

Saving the whole application state( ```QTableWidget```, ```QPushButton```, ```QProcess```, objects created with ```new```)

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 1.3k Views 3 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.
  • JonBJ JonB

    @hbatalha
    While you can try to write code to "creates a "state" of your widget such that it can be reloaded ", from experience you will probably be better if you can express and keep a model of your current state and save/restore that.

    If using QTableWidget you should be saving its model(), not the widget itself. And be able to recreate its items from that.

    QWidgets are not intended to be saved. In any case, when you say

    there are a bunch of connect's to perform a lot of things in that row

    If you are doing connect()s dynamically you will not want to try to serialize connections. It is preferable to rebuild these at load time from a model.

    H Offline
    H Offline
    hbatalha
    wrote on last edited by
    #8

    @JonB said

    If using QTableWidget you should be saving its model(), not the widget itself. And be able to recreate its items from that.
    Something like this?

     QSettings qsettings(QSettings::IniFormat, QSettings::UserScope, "Company", "App");
    
     qsettings.setValue("TableModel", QVariant::fromValue(ui->tableWidget->model()));
    
    
    kshegunovK JonBJ 2 Replies Last reply
    0
    • H hbatalha

      @JonB said

      If using QTableWidget you should be saving its model(), not the widget itself. And be able to recreate its items from that.
      Something like this?

       QSettings qsettings(QSettings::IniFormat, QSettings::UserScope, "Company", "App");
      
       qsettings.setValue("TableModel", QVariant::fromValue(ui->tableWidget->model()));
      
      
      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #9

      No, no. QObjects are not serializable by themselves. You are not going to find a straightforward and simple solution to what you're looking for. You must write your own code, handling your own use case.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • H hbatalha

        @JonB said

        If using QTableWidget you should be saving its model(), not the widget itself. And be able to recreate its items from that.
        Something like this?

         QSettings qsettings(QSettings::IniFormat, QSettings::UserScope, "Company", "App");
        
         qsettings.setValue("TableModel", QVariant::fromValue(ui->tableWidget->model()));
        
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #10

        @hbatalha
        Unfortunately I think not :( But note that I have not tried this --- have you? I cannot imagine that any Qt model is serializable, so what does QVariant::fromValue(ui->tableWidget->model()) actually produce?

        So, unless that works (please tell me if it does!), I meant it is your job to iterate all the rows/columns saving whatever data role values you have stored in it, such that you can reconstruct the model when you deserialize that data. The issue of storing the serialization on a QSettings is a detail. One possibility would be to save the data as JSON in a single setting's key's value.

        H 1 Reply Last reply
        0
        • JonBJ JonB

          @hbatalha
          Unfortunately I think not :( But note that I have not tried this --- have you? I cannot imagine that any Qt model is serializable, so what does QVariant::fromValue(ui->tableWidget->model()) actually produce?

          So, unless that works (please tell me if it does!), I meant it is your job to iterate all the rows/columns saving whatever data role values you have stored in it, such that you can reconstruct the model when you deserialize that data. The issue of storing the serialization on a QSettings is a detail. One possibility would be to save the data as JSON in a single setting's key's value.

          H Offline
          H Offline
          hbatalha
          wrote on last edited by
          #11

          @JonB said

          So, unless that works (please tell me if it does!)

          Yes it "works", it does save some values in the ini file, but don't know what they mean nor how I can load from that as I can't convert QVariant to QAbstractItemModel*.

          JonBJ 1 Reply Last reply
          0
          • H hbatalha

            @JonB said

            So, unless that works (please tell me if it does!)

            Yes it "works", it does save some values in the ini file, but don't know what they mean nor how I can load from that as I can't convert QVariant to QAbstractItemModel*.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #12

            @hbatalha

            Yes it "works", it does save some values in the ini file

            I really don't think it does (i.e. save the table contents/info)! And nor does @kshegunov. What exactly does that line produce? If it's a few bytes long, it can't possibly be the necessary information to reconstruct :)

            H 1 Reply Last reply
            0
            • kshegunovK kshegunov

              You can't, not out of the box. You can store the window's geometry and such, but storing a (child) widget state makes no sense. Firstly the geometry of the children is controlled by the layouts, so it's not something one usually concerns oneself with. And secondly the data backing the widgets/views may not be available, loaded or even accessible on a second run, so you need to guarantee a consistent state yourself.

              H Offline
              H Offline
              hbatalha
              wrote on last edited by hbatalha
              #13

              @kshegunov said

              You can't, not out of the box

              So any idea on how I can achieve that?

              JonBJ 1 Reply Last reply
              0
              • H hbatalha

                @kshegunov said

                You can't, not out of the box

                So any idea on how I can achieve that?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #14

                @hbatalha
                The way I said! You have to save what you want from the model with your own code. There isn't a magic wand. Both of us are telling you that! But doing it this way is likely to be a lot easier than actually trying to serialize a model or a widget with all its properties/attributes etc. as an object.

                H 1 Reply Last reply
                2
                • JonBJ JonB

                  @hbatalha

                  Yes it "works", it does save some values in the ini file

                  I really don't think it does (i.e. save the table contents/info)! And nor does @kshegunov. What exactly does that line produce? If it's a few bytes long, it can't possibly be the necessary information to reconstruct :)

                  H Offline
                  H Offline
                  hbatalha
                  wrote on last edited by hbatalha
                  #15

                  @JonB
                  I don't think it does neither. That's why I had 'works' in quotes. What I meant is that it compiles and save some stuff in the file. And then I saw that the application informs that it can't save QAbstractItemModel*

                  This is what it saves to the file App.ini:

                  [General]
                  TableModel=@Variant(\0\0\0\x7f\0\0\0\x14QAbstractItemModel*\0)
                  
                  
                  JonBJ 1 Reply Last reply
                  0
                  • H hbatalha

                    @JonB
                    I don't think it does neither. That's why I had 'works' in quotes. What I meant is that it compiles and save some stuff in the file. And then I saw that the application informs that it can't save QAbstractItemModel*

                    This is what it saves to the file App.ini:

                    [General]
                    TableModel=@Variant(\0\0\0\x7f\0\0\0\x14QAbstractItemModel*\0)
                    
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #16

                    @hbatalha
                    Yes, that is what I/ @kshegunov expected. All it's serializing is the pointer value with its type name! Because that's all it knows how to do. And will never get further, you have to write the code to serialize/save the data in the model, and to restore it. Which I think is easier than trying to serialize/save the widgets/the visible table for restoration.

                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @hbatalha
                      The way I said! You have to save what you want from the model with your own code. There isn't a magic wand. Both of us are telling you that! But doing it this way is likely to be a lot easier than actually trying to serialize a model or a widget with all its properties/attributes etc. as an object.

                      H Offline
                      H Offline
                      hbatalha
                      wrote on last edited by
                      #17

                      @JonB said

                      The way I said! You have to save what you want from the model with your own code. There isn't a magic wand. Both of us are telling you that! But doing it this way is likely to be a lot easier than actually trying to serialize a model or a widget with all its properties/attributes etc. as an object.

                      yeah, I already thought about a way to do that, it requires me to change a lot of working code but that seems to be the way to do it. I will get back to you once I finish.

                      1 Reply Last reply
                      1
                      • H Offline
                        H Offline
                        hbatalha
                        wrote on last edited by hbatalha
                        #18

                        @JonB, @SGaist , @kshegunov

                        I am happy to inform that I have achieved the goal stated in the OP.

                        What I did was create a function that that stores all the instances info in the QSettings and another function that will will create all the rows in the QTableWIdget from the info saved.

                        Something like this:

                        
                        void saveSetting()
                        {
                            QSettings qsettings(QSettings::IniFormat, QSettings::UserScope, "Company", "APP");
                        
                            qsettings.beginWriteArray("Table");
                            qsettings.remove("");
                            qsettings.endArray();
                        
                            qsettings.beginWriteArray("Table");
                            for(qsizetype i = 0, len = vector_to_hold_FooClass_instances.size(); i < len; ++i)
                            {
                                qsettings.setArrayIndex(i);
                                qsettings.setValue("args", vector_to_hold_FooClass_instances.at(i)->getArgs());
                        
                                 // I replaced the statuses with enum class instead of using ``bool``
                                 // I couldn't find a better t save enum class to QSettings so I used this workaround
                                qsettings.setValue("status", static_cast<int>(vector_to_hold_FooClass_instances.at(i)->status));
                            }
                            qsettings.endArray();
                        
                        }
                        

                        and on loading:

                        void MainWindow::loadSettings()
                        {
                            QSettings qsettings(QSettings::IniFormat, QSettings::UserScope, "HBatalha", "Table");
                        
                           int size = qsettings.beginReadArray("Table");
                           
                           for(int i = 0; i < size; ++i)    
                           {        
                                QStringList args = qsettings.value("args").toString().split(" ");
                                
                                FooClass::Status status = static_cast<FooClass::Status>(qsettings.value("status").toInt());        
                               const int dest_row = ui->tableWidget->rowCount();
                        
                                ui->tableWidget->setRowCount(dest_row + 1);
                        
                                QPushButton* button1 = new QPushButton();
                                pause_button1->setIcon(QIcon(QPixmap(":/Icons/Icons/icon1.png")));
                        
                                QPushButton* button2= new QPushButton();
                                cancel_button->setIcon(QIcon(QPixmap(":/Icons/Icons/icon2.png")));
                        
                        
                                ui->tableWidget->setItem(dest_row, 0, new QTableWidgetItem("Text1");
                                ui->tableWidget->setItem(dest_row, 1, new QTableWidgetItem("Text2"));
                                ui->tableWidget->setCellWidget(dest_row, 2, button1 );
                                ui->tableWidget->setCellWidget(dest_row, 3, button2);
                        
                               FooClass *foo_class = new FooClass(args);
                                foo_class->status = status;
                         
                                connect(button1, SIGNAL(clicked(bool)), this, SLOT(someTask(bool))); 
                               
                                connect(button2, SIGNAL(clicked()), foo_class , SLOT(sometask()));
                        
                                 connect(cancel_button, &QPushButton::clicked, [this, button1, button2, args]()
                                
                                            // some code
                                
                                            });
                        
                                vector_to_hold_FooClass_instances.push_back(foo_class );
                        
                                QSring arg = args.join(" ");
                        
                                QMap_args_rows[dest_row] = arg;
                        
                                foo_class->start();
                        
                                args.clear();
                            }
                            qsettings.endArray();
                        }
                        
                        

                        What do you guys think?

                        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