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