[SOLVED] QSettings, accesing arrays when you don't know their name..



  • So i am making a wallpaper changer, and i want everytime a wallpaper changes, a qstring (of the the datetime + and te path of the image) to be saved. So i did something like this everytime a wallpaper changes:

    @QDateTime date_now = QDateTime::currentDateTime();
    QSettings history_settings("wallch", "History");
    int size = history_settings.beginReadArray(QString::number(date_now.date().year())+"/"+QDate::longMonthName(date_now.date().month())+"/"+QString::number(date_now.date().day()));
    history_settings.endArray();
    history_settings.beginWriteArray(QString::number(date_now.date().year())+"/"+QDate::longMonthName(date_now.date().month())+"/"+QString::number(date_now.date().day()));
    history_settings.setArrayIndex(size);
    history_settings.setValue(date_now.toString("dd")+QString::number(date_now.date().dayOfWeek()), date_now.toString("hh")+":"+date_now.toString("mm") +" | "+qimage);
    history_settings.endArray();@

    which works like a charm if u see regedit:
    !http://i.imgur.com/2QGoQAl.png(img)!

    The question is, how do i access this qstrings after? I don't know the array name... Is there another way i should go with on the saving procedure instead? begin group seems a bettery way, but setValue seems a little wrong since i just want to save a QString..



  • Hi,

    I don't like your solution so much.

    I sugget you to use groups with variable names (because you can call QSettings::childGroups() to retreive all group names) and array with fixed name inside.

    WRITE
    @
    QSettings settings;

    settings.beginGroup("History");
    for (int i = 0; i < 5; ++i) {
        settings.beginGroup(QString("group_%1").arg(i + 1));
        settings.beginWriteArray("images", 10);
        for (int j = 0; j < 10; ++j) {
            settings.setArrayIndex(j);
            settings.setValue("date", QString("DATE_%1:%2").arg(i).arg(j));
            settings.setValue("path", QString("PATH_%1:%2").arg(i).arg(j));
        }
        settings.endArray();
        settings.endGroup();
    }
    settings.endGroup();
    

    @

    READ
    @
    QSettings settings;
    settings.beginGroup("History");
    QStringList groups = settings.childGroups();
    Q_FOREACH (QString group, groups) {
    settings.beginGroup(group);
    int imageSize = settings.beginReadArray("images");
    for (int i = 0; i < imageSize; ++i) {
    settings.setArrayIndex(i);
    qDebug (qPrintable (settings.value("date").toString()));
    qDebug (qPrintable (settings.value("path").toString()));
    }
    settings.endArray();
    settings.endGroup();
    }
    settings.endGroup();
    @



  • Late answer but yeah :D
    Thanks! groups solved my hand

    WRITE

    @QDateTime date_now = QDateTime::currentDateTime();
    QSettings history_settings("wallch", "History");
    history_settings.beginGroup(QString::number(date_now.date().year()));
    history_settings.beginGroup(QString::number(date_now.date().month()));
    int size = history_settings.beginReadArray(date_now.toString("dd"));
    history_settings.endArray();
    history_settings.beginWriteArray(date_now.toString("dd"));
    history_settings.setArrayIndex(size);
    history_settings.setValue("time", date_now.toString("hh:mm"));
    history_settings.setValue("path", image);
    history_settings.setValue("type", type);
    history_settings.endArray();
    history_settings.endGroup();
    history_settings.endGroup();@

    READ
    @QSettings history_settings("wallch", "History");
    QStringList years = history_settings.childGroups();
    Q_FOREACH (QString year, years) {
    history_settings.beginGroup(year);
    QStringList months = history_settings.childGroups();
    Q_FOREACH (QString month, months) {
    //add top level item, eg. December (2013)
    QTreeWidgetItem *month_year_item = new QTreeWidgetItem;
    month_year_item->setText(0, QDate::longMonthName(month.toInt())+" ("+year+")");
    month_year_item->setData(0,12, year);
    month_year_item->setData(1,12, month);
    ui->treeWidget->addTopLevelItem(month_year_item);

            history_settings.beginGroup(month);
            QStringList days = history_settings.childGroups();
            int index=0;
            Q_FOREACH (QString day, days) {
                //add day of month child (1-31)
                QTreeWidgetItem *day_item = new QTreeWidgetItem;
                if(day.toInt()<10)
                    day_item->setText(0, day.right(1));
                else
                    day_item->setText(0, day);
                ui->treeWidget->topLevelItem(ui->treeWidget->topLevelItemCount()-1)->insertChild(index, day_item);
    
                index++;
                history_settings.endArray();
            }
            history_settings.endGroup();
        }
        history_settings.endGroup();
    }@

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.