Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Save QRadioButton Selection



  • Hi All,

    I am building an application that has a function list connected devices i.e. COM Ports. Now, I have a list of some QRadioButton and they are selected by the user and then I would like to save the user selection state for the next run time.

    Could you help me with this issue, as I am not getting the part how can I save users selection for an application?

    The nearest functioning example can also be seen in Mouse properties in control panel as user selects the state and apply it. When next time user open the setting it is the last changed state, which user made.

    Regards,
    Neeraj Singhal



  • @Neeraj2020
    And to complete @jsulm's answer, to read back & restore the setting, something like (I assume you have your buttons in a QButtonGroup()):

    QSettings settings("MySoft", "My App");
    QString val = settings.value("ComPorts/selected", "").toString());
    if (!val.isEmpty())
    {
        QList<QRadioButton *> buttons = theRadioButtonGroup()->buttons();
        for (QRadioButton * button : buttons)
            if (button->text() == val)
                button->setChecked(true);
    }
    

  • Moderators

    Use QSettings to save the selection when user changes it (or when application is closing), and then to load it when application starts up.


  • Lifetime Qt Champion

    @Neeraj2020 Take a look at https://doc.qt.io/qt-5/qsettings.html
    You can store settings in a file and on Windows alternatively in Registry.



  • This post is deleted!


  • Hi @sierdzio and @jsulm I read about QSettings but still dont know how to save the the button selection for the user?

    I have a list or ports
    o COM1
    o COM2
    o COM3

    by default, it is on COM1 and now the user open the window and change to COM2 and click OK. Now, next time user opens the window it should be COM2 not COM1.



  • @Neeraj2020

    I read about QSettings but still dont know how to save the the button selection for the user

    Have you tried any example for QSettings?


  • Banned

    Okay there are many ways to save these settings -- as stated QSettings is one, a Config file is another, and a database can be used as well.

    All that said you have to, for you situation save the state you wish to retain and then re-implement that state via code which is to say -- when your user selects the COM2 radio button you capture this and translate that into a storeable value such as 1, 2, 3, etc...
    and store that and then when you init the GUI you check to see what the last stored state was and use that to instantiate the radio buttons.


  • Lifetime Qt Champion

    @Neeraj2020 said in Save QRadioButton Selection:

    I read about QSettings but still dont know how to save the the button selection for the user?

    Did you try anything already?
    There are even examples.
    Could be like this:

    QSettings settings("MySoft", "My App");
    settings.setValue("ComPorts/selected", selectedRadioButton->text());
    


  • @Neeraj2020
    And to complete @jsulm's answer, to read back & restore the setting, something like (I assume you have your buttons in a QButtonGroup()):

    QSettings settings("MySoft", "My App");
    QString val = settings.value("ComPorts/selected", "").toString());
    if (!val.isEmpty())
    {
        QList<QRadioButton *> buttons = theRadioButtonGroup()->buttons();
        for (QRadioButton * button : buttons)
            if (button->text() == val)
                button->setChecked(true);
    }
    



Log in to reply