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. How to save settings of a window that contains widgets with same object name?
Qt 6.11 is out! See what's new in the release blog

How to save settings of a window that contains widgets with same object name?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 305 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.
  • Y Offline
    Y Offline
    Ylvy
    wrote on last edited by Ylvy
    #1

    I have a QMainWindow where I create new instances of a specific .ui created on Qt Designer, each new instance the widgets contains the same object name, example:

    // test.h
    #pragma once
    #include "stdafx.h"
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class Form; };
    QT_END_NAMESPACE
    
    class Test : public QWidget 
    {
        Q_OBJECT
    public:
        Ui::Form* ui;
        Test(QWidget* parent = nullptr) : QWidget(parent), ui(new Ui::Form()) {
            ui->setupUi(this);
        }
    };
    
    
    // mainwindow
    Test test = new Test(...);   // <-
    Test test_2 = new Test(...); // <- all widgets from both `Test` contains same object name
    

    There's another way to differentiate the widgets other than using their object names when saving their values to a settings.ini file?

    If no, I'm looking for suggestions in how else I could achieve this, I mean, maybe appending a special character before each widget objectName?

    JonBJ 1 Reply Last reply
    0
    • Y Ylvy

      I have a QMainWindow where I create new instances of a specific .ui created on Qt Designer, each new instance the widgets contains the same object name, example:

      // test.h
      #pragma once
      #include "stdafx.h"
      
      QT_BEGIN_NAMESPACE
      namespace Ui { class Form; };
      QT_END_NAMESPACE
      
      class Test : public QWidget 
      {
          Q_OBJECT
      public:
          Ui::Form* ui;
          Test(QWidget* parent = nullptr) : QWidget(parent), ui(new Ui::Form()) {
              ui->setupUi(this);
          }
      };
      
      
      // mainwindow
      Test test = new Test(...);   // <-
      Test test_2 = new Test(...); // <- all widgets from both `Test` contains same object name
      

      There's another way to differentiate the widgets other than using their object names when saving their values to a settings.ini file?

      If no, I'm looking for suggestions in how else I could achieve this, I mean, maybe appending a special character before each widget objectName?

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

      @Ylvy
      Exactly that, you would have to e.g. append a sequence number or a number based on which new Test() you are in to each widget's objectName() if you wish to identify it uniquely across sessions.

      Y 1 Reply Last reply
      0
      • JonBJ JonB

        @Ylvy
        Exactly that, you would have to e.g. append a sequence number or a number based on which new Test() you are in to each widget's objectName() if you wish to identify it uniquely across sessions.

        Y Offline
        Y Offline
        Ylvy
        wrote on last edited by
        #3

        @JonB there's any 'easy' way to iterate every widget on a ui?

        JonBJ 1 Reply Last reply
        0
        • Y Ylvy

          @JonB there's any 'easy' way to iterate every widget on a ui?

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

          @Ylvy
          parentWidget->findChild<QWidget *>()

          If you really need to "save state" it's more usual to create and save some model than expect to find widgets and know what to save from them. Up to you.

          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