Solved 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?
-
Hi,
- I have global
QSetting *iniSetting =new QSetting(filename,QSettings::IniFormat,this);
, reads the.ini
file and creates groups.
2)I have globalQTreeWidget
object, it is getting populated byQTreeWidgetItems
objects , data from theiniSettings
object.
3)Now while user double-clicks a group value onQTreeWidget
object and edits the column 2 value ,QTreeWidget
generates the signalitemChanged(QTreeWidgetItem*,int);
this I have connected to the slotitemChanged(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 ?
- I have global
-
@thippu
to write a value into a group you need either set the key of the value togroup/key
or you callQSettings::beginGroup()
/QSettings::endGroup()
before callingQSettings::setValue()
-
@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
onQTreeWidget
object, I want update this values to existing group key/value in theQSetting
and the.ini
file , -
@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?!
-
@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-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
-
@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 groupmygroup
iniSettings->beginGroup("mygroup"); QVariant val = iniSettings->value("mykey"); iniSettings->endGroup();
QVariant val = iniSettings->value("mygroup/mykey");
-
@raven-worx , I think I got it.
my current key/value stored inQTreeWidgetItem
and this is attached root node, root node contains the group name.
how to get parent node text using currentQTreeWidgetItem
? -
@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?
-
okay, but my group name is dynamic.
iniSettings->beginGroup("mygroup"); QVariant val = iniSettings->value("mykey"); iniSettings->endGroup();
QVariant val = iniSettings->value("mygroup/mykey");
-
@raven-worx
I didvoid 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?
-
@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. -
@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
onQTreeWidget
object, I want update this values to existing group key/value in theQSetting
and the.ini
file , -
@thippu Again: a root node does NOT have a parent.
-
@jsulm I'm stuck here if I find the group name I can complete this, don't know how to fix it help me.
-
@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) }
-
@raven-worx it is working perfectly, Awesome.
Thanks a lot.