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. QSettings object not overwriting the existing group's key value, creates [General] and stores data, how to make it write on the actual location in the .ini file?
Qt 6.11 is out! See what's new in the release blog

QSettings object not overwriting the existing group's key value, creates [General] and stores data, how to make it write on the actual location in the .ini file?

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 3 Posters 3.1k 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.
  • T Offline
    T Offline
    thippu
    wrote on last edited by
    #8

    @raven-worx , I think I got it.
    my current key/value stored in QTreeWidgetItem and this is attached root node, root node contains the group name.
    how to get parent node text using current QTreeWidgetItem?

    raven-worxR 1 Reply Last reply
    0
    • T thippu

      @raven-worx , I think I got it.
      my current key/value stored in QTreeWidgetItem and this is attached root node, root node contains the group name.
      how to get parent node text using current QTreeWidgetItem?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #9

      @thippu said in QSettings object not overwriting the existing group's key value, creates [General] and stores data, how to make it write on the actual location in the .ini file?:

      how to get parent node text using current QTreeWidgetItem?

      QTreeWidgetItem::parent()

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      T 1 Reply Last reply
      4
      • raven-worxR raven-worx

        @thippu said in QSettings object not overwriting the existing group's key value, creates [General] and stores data, how to make it write on the actual location in the .ini file?:

        So why to call again QSettings::beginGroup.

        because this sets the group to read/write from (if you do not specify the group in the key path already).

        There are the 2 following ways to read the key mykey from the group mygroup

        iniSettings->beginGroup("mygroup");
        QVariant val = iniSettings->value("mykey");
        iniSettings->endGroup();
        
        QVariant val = iniSettings->value("mygroup/mykey");
        
        T Offline
        T Offline
        thippu
        wrote on last edited by
        #10

        okay, but my group name is dynamic.

        iniSettings->beginGroup("mygroup");
        QVariant val = iniSettings->value("mykey");
        iniSettings->endGroup();
        
        QVariant val = iniSettings->value("mygroup/mykey");
        
        1 Reply Last reply
        0
        • raven-worxR raven-worx

          @thippu said in QSettings object not overwriting the existing group's key value, creates [General] and stores data, how to make it write on the actual location in the .ini file?:

          how to get parent node text using current QTreeWidgetItem?

          QTreeWidgetItem::parent()

          T Offline
          T Offline
          thippu
          wrote on last edited by
          #11

          @raven-worx
          I did

           void Widget::itemChanged(QTreeWidgetItem *item,int col)
          {
          QTreeWidgetItem *parent=item->parent();
          qDebug()<<parent->text(0);//App is getting crashed here, do not know why? how to get text from this root?
          raven-worxR 1 Reply Last reply
          0
          • T thippu

            @raven-worx
            I did

             void Widget::itemChanged(QTreeWidgetItem *item,int col)
            {
            QTreeWidgetItem *parent=item->parent();
            qDebug()<<parent->text(0);//App is getting crashed here, do not know why? how to get text from this root?
            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #12

            @thippu
            probably because parent is a NULL pointer when you call this piece of code for all your items.
            Your root items of course do not have a parent item.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            T 1 Reply Last reply
            4
            • raven-worxR raven-worx

              @thippu
              probably because parent is a NULL pointer when you call this piece of code for all your items.
              Your root items of course do not have a parent item.

              T Offline
              T Offline
              thippu
              wrote on last edited by
              #13

              @raven-worx
              like below I'm doing to create nodes and children.

               QTreeWidgetItem& Widget::createRoot(const QString &groupname)
              {
                 QTreeWidgetItem *root=new QTreeWidgetItem(displayTreeWidget);//here parameter is QTreeWidget
                 root->setText(0,groupname);
                displayTreeWidget->addTopLevelItem(root);
              return *root;
              }
              
              //method to create children and attach them to toplevelitem
              void Widget::createChild(QTreeWidgetItem &rootaddress,const QString &key,const QVariant &value)
              {
                QTreeWidgetItem *child=new QTreeWidgetItem;
               child->setText(0,key);
              child->setTextAlignment(0,Qt::AlignCenter);
              child->setData(1,2,value);
              rootaddress.addChild(child);
              }
              //these two methods are called while initial group creation
              //mentioned with bold text in the code below
              

              Groups have been created initially using code:

              {
                      iniSettings->beginGroup(group);
                      QTreeWidgetItem &rootaddress=**createRoot(group);** //it creates group text as node on QTreeWidget object.
                       foreach( QString key, iniSettings->childKeys() )
                       {
                              QVariant val = iniSettings->value(key);
                              ***createChild(rootaddress,key,val); *** /adds key and value of the group as the child nodes to the QTreeWidget using 
                              rootaddress.
                       }
                   iniSettings->endGroup();
              } 
              

              Now if a user changes(or edits) any value on the QTreeWidgetItem on QTreeWidget object, I want update this values to existing group key/value in the QSetting and the .ini file ,

              jsulmJ 1 Reply Last reply
              0
              • T thippu

                @raven-worx
                like below I'm doing to create nodes and children.

                 QTreeWidgetItem& Widget::createRoot(const QString &groupname)
                {
                   QTreeWidgetItem *root=new QTreeWidgetItem(displayTreeWidget);//here parameter is QTreeWidget
                   root->setText(0,groupname);
                  displayTreeWidget->addTopLevelItem(root);
                return *root;
                }
                
                //method to create children and attach them to toplevelitem
                void Widget::createChild(QTreeWidgetItem &rootaddress,const QString &key,const QVariant &value)
                {
                  QTreeWidgetItem *child=new QTreeWidgetItem;
                 child->setText(0,key);
                child->setTextAlignment(0,Qt::AlignCenter);
                child->setData(1,2,value);
                rootaddress.addChild(child);
                }
                //these two methods are called while initial group creation
                //mentioned with bold text in the code below
                

                Groups have been created initially using code:

                {
                        iniSettings->beginGroup(group);
                        QTreeWidgetItem &rootaddress=**createRoot(group);** //it creates group text as node on QTreeWidget object.
                         foreach( QString key, iniSettings->childKeys() )
                         {
                                QVariant val = iniSettings->value(key);
                                ***createChild(rootaddress,key,val); *** /adds key and value of the group as the child nodes to the QTreeWidget using 
                                rootaddress.
                         }
                     iniSettings->endGroup();
                } 
                

                Now if a user changes(or edits) any value on the QTreeWidgetItem on QTreeWidget object, I want update this values to existing group key/value in the QSetting and the .ini file ,

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #14

                @thippu Again: a root node does NOT have a parent.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                T 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @thippu Again: a root node does NOT have a parent.

                  T Offline
                  T Offline
                  thippu
                  wrote on last edited by thippu
                  #15

                  @jsulm I'm stuck here if I find the group name I can complete this, don't know how to fix it help me.

                  raven-worxR 1 Reply Last reply
                  0
                  • T thippu

                    @jsulm I'm stuck here if I find the group name I can complete this, don't know how to fix it help me.

                    raven-worxR Offline
                    raven-worxR Offline
                    raven-worx
                    Moderators
                    wrote on last edited by raven-worx
                    #16

                    @thippu
                    assuming your tree widget only has 1 sub-level a simple check can be:

                    QTreeWidgetItem* item = ...;
                    if( QTreeWidgetItem* parent = item->parent() )
                    {
                         // item is a child (= key)
                         QString keyPath = QString("%1/%2").arg(parent->text(0)).arg(item->text(0));
                         QVariant val = iniSettings->value( keyPath ); // reading
                         iniSettings->setValue( keyPath, val ); // writing
                    }
                    else
                    {
                         // item is a root item (= group)
                    }
                    

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    T 1 Reply Last reply
                    3
                    • raven-worxR raven-worx

                      @thippu
                      assuming your tree widget only has 1 sub-level a simple check can be:

                      QTreeWidgetItem* item = ...;
                      if( QTreeWidgetItem* parent = item->parent() )
                      {
                           // item is a child (= key)
                           QString keyPath = QString("%1/%2").arg(parent->text(0)).arg(item->text(0));
                           QVariant val = iniSettings->value( keyPath ); // reading
                           iniSettings->setValue( keyPath, val ); // writing
                      }
                      else
                      {
                           // item is a root item (= group)
                      }
                      
                      T Offline
                      T Offline
                      thippu
                      wrote on last edited by thippu
                      #17

                      @raven-worx it is working perfectly, Awesome.
                      Thanks a lot.

                      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