Saving index of QcomboBox[SOLVED]



  • I am trying to save the save the index of my QComboBox so that whatever index it had when the application was closed, the same index would show when I restart the application.
    So far I have the index number being saved to an .ini file:
    @
    [def]
    def=3@
    In this case "3" is the current index of the QcomboBox. The number changes everytime I change the index using this code:

    @QSettings settings("/home/test/Documents/Wave/signalgenerator.ini", QSettings::IniFormat);
    settings.beginGroup("def");
    QHash<QString, QString> def;
    foreach (const QString &childKey, childKeys) {
    values.insert(childKey, settings.value(childKey).toString());
    if (childKey.toInt() == index+1) {
    def.insert(childKey, settings.value(childKey).toString());
    settings.setValue("def",childKey);

                }@
    

    However, every time, I close the application and open it up again, the index shown is always index 0 i.e. the first option in the QComboBox.
    Any help would be greatly appreciated



  • You should read the settings somewhere and call setCurrentIndex() on the combobox.

    And PLEASE stop that damn crappy reading of all entries of the QSettings into a hash map. It does not save you anything, in fact it just bloats your code for nothing.

    You have been told how to do it right so many times in this forum now, there are so many examples out there and you still do it completely wrong. Did you ever understand what you've been told here?

    For a really last time:

    @
    // to save the index:
    QSettings settings("...path...");
    settings.setValue("def/comboBoxIndex", ui->comboBox->currentIndex());

    // to load the old index:
    QSettings settings("...path...");
    // the second argument to value is the default
    // in case there hasn't been set a value in the settings yet
    int index = settings.value("def/comboBoxIndex", 0).toInt();
    ui->comboBox->setCurrentIndex(index);
    @

    There is no need to loop over the child keys. SO STOP DOING THIS!



  • thanks volker. That saves the index, but it doesn't succeed in loading the old index. it still just loads index 0 when i run it again.



  • When using @settings.value("def/comboBoxIndex", 0);@ you define 0 as the default value when the loader could not have the information.

    For more information, you may always check "QSettings::value":http://doc.qt.nokia.com/4.7/qsettings.html#value.


  • Moderators

    [quote author="ogopa" date="1314726204"]thanks volker. That saves the index, but it doesn't succeed in loading the old index. it still just loads index 0 when i run it again.[/quote]

    Please post your snippet of code where it's trying to set the value. How does it differ from Volker's?



  • I got it working. thanks guys. i had set the current index before the code which populated the combo box. silly me. but i placed it after that so its working now. :)


  • Moderators

    You do know that you don't have to put save and read code together, right? Volker's examples (settings and getting) were meant to be put in separate places in the code, not called one right before the other...


Log in to reply
 

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