1. 程式人生 > >QT 將INI檔案中的選項寫入QComboBox中功能實現

QT 將INI檔案中的選項寫入QComboBox中功能實現

INI檔案如下 

;====================== 個人性別 ======================
[sex]
01=男
02=女

向QComboBox中寫入下拉選項的函式

extern void SetControlItemsFromIni(const QString &file_name, const QString &str_group, QComboBox *m_box);

void SetControlItemsFromIni(const QString &file_name, const QString &str_group, QComboBox *m_box)
{
    QSettings *setting = new QSettings(file_name, QSettings::IniFormat);
    setting->setIniCodec("UTF-8");

    setting->beginGroup(str_group);
    m_box->clear();

    QStringList data_list = setting->allKeys();
    for(int i = 0; i < data_list.count(); i++)
        m_box->addItem(setting->value(data_list.at(i)).toString());

    setting->endGroup();

    delete setting;
}

使用方法

SetControlItemsFromIni("person_ini", "sex", ui->sex);