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?

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.0k 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 thippu
    #1

    Hi,

    1. I have global QSetting *iniSetting =new QSetting(filename,QSettings::IniFormat,this);, reads the .ini file and creates groups.
      2)I have global QTreeWidgetobject, it is getting populated by QTreeWidgetItems objects , data from the iniSettings object.
      3)Now while user double-clicks a group value on QTreeWidget object and edits the column 2 value ,QTreeWidgetgenerates the signal itemChanged(QTreeWidgetItem*,int); this I have connected to the slot itemChanged(QTreeWidgetItem *item,int col);of the Mainwindow object. Now I want to get the edited value and update the value in the .ini file but it is not happening properly, please help me.

    code:

    
     void Widget::itemChanged(QTreeWidgetItem *item,int col)
     {
        iniSettings->setValue(item->text(0),item->text(col));//here item->text(0) is key of a group in iniSettings object and item->text(col) new value edit by user corresponds to key and it has to be updated on the .ini file, but not working properly.
    // this will create contents in .ini file like this:
    [General]
    key1=value
    //output not overwritting the existing group using key with new value but creates the above contents in .ini file how to make this to update on the correct group ?
    
    raven-worxR 1 Reply Last reply
    0
    • T thippu

      Hi,

      1. I have global QSetting *iniSetting =new QSetting(filename,QSettings::IniFormat,this);, reads the .ini file and creates groups.
        2)I have global QTreeWidgetobject, it is getting populated by QTreeWidgetItems objects , data from the iniSettings object.
        3)Now while user double-clicks a group value on QTreeWidget object and edits the column 2 value ,QTreeWidgetgenerates the signal itemChanged(QTreeWidgetItem*,int); this I have connected to the slot itemChanged(QTreeWidgetItem *item,int col);of the Mainwindow object. Now I want to get the edited value and update the value in the .ini file but it is not happening properly, please help me.

      code:

      
       void Widget::itemChanged(QTreeWidgetItem *item,int col)
       {
          iniSettings->setValue(item->text(0),item->text(col));//here item->text(0) is key of a group in iniSettings object and item->text(col) new value edit by user corresponds to key and it has to be updated on the .ini file, but not working properly.
      // this will create contents in .ini file like this:
      [General]
      key1=value
      //output not overwritting the existing group using key with new value but creates the above contents in .ini file how to make this to update on the correct group ?
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @thippu
      to write a value into a group you need either set the key of the value to group/key
      or you call QSettings::beginGroup() / QSettings::endGroup() before calling QSettings::setValue()

      --- 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
        to write a value into a group you need either set the key of the value to group/key
        or you call QSettings::beginGroup() / QSettings::endGroup() before calling QSettings::setValue()

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

        @raven-worx
        you are not got what my problem is,
        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 ,

        raven-worxR 1 Reply Last reply
        0
        • T thippu

          @raven-worx
          you are not got what my problem is,
          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 ,

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

          @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?:

          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

          yes, and why is my advise wrong here?!

          --- 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 2 Replies 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?:

            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

            yes, and why is my advise wrong here?!

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

            @raven-worx iniSettings has been populated by group names using .ini file already, So why to call again QSettings::beginGroup.
            Or maybe I did not get what you are saying.

            raven-worxR 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?:

              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

              yes, and why is my advise wrong here?!

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

              @raven-worx , I did not get this

              to write a value into a group you need either set the key of the value to group/key

              1 Reply Last reply
              0
              • T thippu

                @raven-worx iniSettings has been populated by group names using .ini file already, So why to call again QSettings::beginGroup.
                Or maybe I did not get what you are saying.

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

                @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");
                

                --- 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
                • 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