Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Saving index of QcomboBox[SOLVED]
QtWS25 Last Chance

Saving index of QcomboBox[SOLVED]

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 5.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    ogopa
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      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!

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • O Offline
        O Offline
        ogopa
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Cayan
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mlong
            wrote on last edited by
            #5

            [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?

            Software Engineer
            My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

            1 Reply Last reply
            0
            • O Offline
              O Offline
              ogopa
              wrote on last edited by
              #6

              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. :)

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mlong
                wrote on last edited by
                #7

                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...

                Software Engineer
                My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved