如何使用Qt/C ++创建/读取/写入文件以及在程序本地存储设置 [英] How to use Qt/C++ to create/read/write files and store settings local with the program

查看:56
本文介绍了如何使用Qt/C ++创建/读取/写入文件以及在程序本地存储设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的不幸初学者,使用Qt GUI设计器程序似乎很满足我的需求,但我在尝试为此编写必要的代码时遇到了问题.我可以使用QSettings字符串将本地设置存储在硬盘驱动器上,但是当程序执行某些命令执行的%HOME_LOCAL%\ APPS_SETTINGS公牛时,我个人讨厌它.我需要在程序目录中为设置和本地\主机数据库保存一个文本文件,以记住以后要读取的字符串.

I'm an unfortunate beginner at C++ and using the Qt GUI designer program seemed perfect for my needs, except I'm having problems trying to write out the code necessary for this. I could use the QSettings string to store local settings on the hard drive, but I personally hate it when programs do the %HOME_LOCAL%\APPS_SETTINGS bull that some do. I need to save a text file for both settings and a local\host database, within the program directory, to remember strings to read from later.

我需要使用本地主机文本数据库的代码行是什么,或者有更好的选择吗?以及如何将其与本地程序一起存储在其目录中?

What is the line of code I need to make use of a local host text database or is there a better option? And how can I store that with the local program inside its directory?

推荐答案

您可以对要获取程序目录,可以使用 QCoreApplication::applicationDirPath() .

To get the program directory, you can use QCoreApplication::applicationDirPath().

因此,回答您的问题,创建QApplication实例后要添加的语句:

So, answer to your question, statement to put after creation of QApplication instance:

QSettings *settings = new QSettings(
     QCoreApplication::applicationDirPath() + "/settings.ini",
     QSettings::IniFormat,
     qApp);

但是,正如所讨论的评论中所指出的那样,如果要使程序用于一般发行,则应使用操作系统默认值.检查 QSettings 的所有构造函数看看它能做什么.用户通常在应用程序目录中没有写许可权.请注意,您还可以使用 QSettings :: NativeFormat将设置存储到Windows注册表中.

But, as noted in the comments under question, if you're making your program for general distribution, you should use the OS default. Examine all the constructors of QSettings to see what it can do. User does not often have write permission in the application directory. Note that you can also store settings to Windows registry with QSettings::NativeFormat.

这篇关于如何使用Qt/C ++创建/读取/写入文件以及在程序本地存储设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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