Parsing ini-File in Qt [closed]

安稳与你 提交于 2019-12-23 07:04:44

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/17553686/parsing-ini-file-in-qt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!