在Qt中解析ini文件 [英] Parsing ini-File in Qt

查看:285
本文介绍了在Qt中解析ini文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用定制的INI解析器,其参数为表单名称,父窗口小部件.语法如下:

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();

我可能在语法上有些错误,因为我现在没有代码.我要添加一些在INI文件的第2页中提到的标签.这些图像的属性在ini文件本身中已提及.甚至绝对路径,等等.

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.

我尝试了多种组合,但是没有用.谁能给我相同的语法?这里的返回值应该是多少?一个标签,我已经在Qt设计器中创建了标签.假设label1.但是有很多标签.请让我知道.

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.

推荐答案

QSettings是一个很好的类,可以很好地处理INI文件.我会检查出来.它已经过优化,并且非常健壮.它还以非常智能的方式使用QVariant.它还可以很好地处理组".

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

稍后要访问或设置设置

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

或者您可以通过这种方式指定您的ini文件,以指向特定的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(); 

如果您想处理嵌套元素以及gui布局和设计,我将研究XML或JSON.两者均受Qt支持.

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是Qt Creator存储由Qt Designer创建的UI文件的本机方式.

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

希望有帮助.

这篇关于在Qt中解析ini文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆