How to use Qt/C++ to create/read/write files and store settings local with the program

后端 未结 1 1070
野的像风
野的像风 2021-01-27 10:14

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 thi

相关标签:
1条回答
  • 2021-01-27 10:55

    You can use QSettings with any file, with constructor QSettings::QSettings ( const QString & fileName, Format format, QObject * parent = 0 ).

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

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

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

    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.

    0 讨论(0)
提交回复
热议问题