0

I am using a custom made INI parser, for which the parameters are the form name, parent widget. The syntax looks like this:

int width = w->findchild("form_name","section_to_be_accessed_name").toInt();

I maybe a little bit wrong with the syntax, because I don't have the code with me right now. I want to add a few labels which are mentioned in page 2 of the INI file. The properties of those images are mentioned in the ini file itself. Even the absolute path,etc.

I tried multiple combinations, but it doesn't work. Can anyone give me a syntax for the same? What should be the return value here? A label, I have already created labels in Qt designer. Let's say label1. But there are many labels. Kindly let me know.

Swordfish
  • 12,971
  • 3
  • 21
  • 43
We are Borg
  • 5,117
  • 17
  • 102
  • 225

1 Answers1

8

QSettings is a great class that works well for handling INI files. I would check it out. It has been optimized well, and is very robust. It also uses QVariant in a very intelligent way. It also handles "Groups" well.

http://qt-project.org/doc/qt-4.8/qsettings.html

// in your main, or somewhere very early in your program
qApp->setApplicationName("Star Runner");
qApp->setOrganizationName("MySoft");

QSettings::setDefaultFormat(QSettings::IniFormat);

Later when you want to access or set a setting

QSettings s;  // in windows this would be
// C:/Users/<username>/AppData/Roaming/MySoft/Star Runner.ini

Or you could specify your ini file this way to point at a specific ini file

QSettings::QSettings ( const QString & fileName, Format format, QObject * parent = 0 )

QSettings s("path/to/my/inifile.ini", QSettings::IniFormat);

And now an example of using the settings variable:

// set the setting
s.setValue("group_name/setting_name", new_value); 

// retrieve the setting
// Note: Change "toType" to whatever type that this setting is supposed to be
current_value = s.value("group_name/setting_name", default_value).toType(); 

If you want to handle nested elements and gui layouts and design, I would look at XML or JSON. Both are supported by Qt.

XML is the native way that Qt Creator stores the UI files that are created by Qt Designer.

http://qt-project.org/doc/qt-5.0/qtxml/qtxml-module.html

http://qt-project.org/doc/qt-5.0/qtcore/json.html

Hope that helps.

phyatt
  • 18,472
  • 5
  • 61
  • 80
  • This code will not compile (because of 2 `s` variables) and can confuse someone. You should separate it to several code blocks. – Pavel Strakhov Jul 09 '13 at 17:17
  • 3
    I am not sure if I like how it looks now, but the point of most of my answers are to point the user to the correct part of the documentation. Shrug. – phyatt Jul 09 '13 at 17:33
  • Hello Phyatt. Thank you for the information, but i can't use QSettings, i am supposed to use the inbuilt parser. The syntax as i remember is : int width = parser_object_name->findchild(same as above).toInt(); Kindly let me know how to do it for multiple image labels. Thank you very much. – We are Borg Jul 09 '13 at 18:10
  • 1
    I think more example code of using the parser + what the ini file looks like is necessary to help you with your question. It is difficult to assume how the parser would work or handle variables, etc. For example: What doesn't work? What is working? What is in your ini file?, etc. – phyatt Jul 09 '13 at 18:13
  • I will post more details tomorrow. Thank you very much Phyatt. – We are Borg Jul 09 '13 at 18:30