QString QSettings::readEntry(const QString & key, const QString & def = QString::null, bool * ok = 0)
透過key讀取特定的值,回傳一個QString,或是一個default value,若找不到此值對應的value,則以default value為相對應此key的值,並且回傳回來。若不能被讀取且ok又不是一個null pointer則會回傳TRUE,否則為FALSE
bool QSettings::writeEntry ( const QString & key, const QString & value )
把value的值寫入相對應的key中,若失敗的話,會回傳false,若成功的話,會回傳true
QStringList QSettings::entryList ( const QString & key ) const
列出所有目錄下的成員,例:
/MyCompany/MyApplication/background color
/MyCompany/MyApplication/foreground color
/MyCompany/MyApplication/geometry/x
/MyCompany/MyApplication/geometry/y
/MyCompany/MyApplication/geometry/width
/MyCompany/MyApplication/geometry/height
若key為"/MyCompany/MyApplication"
則
/MyCompany/MyApplication/background color
/MyCompany/MyApplication/foreground color
會被收集起來,而子目錄下的並不會被收集起來,換句話說,只會收集當前目錄下的成員。
main.cpp
#include <qsettings.h>
#include <qapplication.h>
#include <qstringlist.h>
int main(int argc,char **argv)
{
QApplication app(argc,argv);
bool gui=false;
bool ret=false;
int width = 88;
QString bgColor;
QStringList keys;
QValueList <QSettings>::iterator it; // 用來放QSettings的容器
QSettings settings;
// settings.insertSearchPath(QSettings::Windows,"MyCompany");
// No search path needed for Unix; see notes further on.
// Use default values if the keys don't exist
// 可以把後面的數值寫到settings的這一個key項目中
// 1. 設定第一個項目"/MyApplication/geometry/width"
settings.writeEntry("/MyApplication/geometry/width",width);
// 讀取相對應的key,且讀出來的是字串型態
// 2. 因之前沒有設定過,所以目前是設定第二個項目"/MyApplication/background color"
// 字串的部分一直轉換!!失敗!!,所以在search時,也不會找到這一個key
bgColor = settings.readEntry("/MyApplication/background color","white",&ret);
if (!ret)
qDebug("1. transfer error");
// 若沒有找到則以後面的default value設定"MyApplication/geometry/width"為相對應的值,並回傳回來
// 以下的例子,若之前沒有設定"MyApplication/geometry/width"的相對應的值
// 則"MyApplication/geoetry/width"會被設定為640
width = settings.readNumEntry("/MyApplication/geometry/width",640,&ret);
qDebug("%d",settings.readNumEntry("/MyApplication/geometry/width",55,&ret));
keys = settings.entryList("/MyApplication/geometry");
qDebug("%d",keys.count());
return gui?app.exec():0;
}
沒有留言:
張貼留言