0
votes

Now, this is a one which is baffling me,

Giving a short example of my sample GUI,

i. I have four labels in the mainwindow, label_1, label_2, label_3, label_4

ii. I have a spinBox, comboBox and a pushButton as well.

iii. The values in comboBox are - ITEM1, ITEM2, ITEM3, ITEM4.

iv. When user runs the program, he selects the value from spinBox (1-4) and chooses the value from comboBox. And on every press of pushButton the labels text is changed,the logic for label text display

spinBox value = 1 and comboBox = ITEM1, label_1->setText("Item 1 in label1")

spinBox value = 1 and comboBox = ITEM2, label_1->setText("Item 2 in label1")

spinBox value = 1 and comboBox = ITEM3, label_1->setText("Item 3 in label1")

spinBox value = 1 and comboBox = ITEM4, label_1->setText("Item 4 in label1")

spinBox value = 2 and comboBox = ITEM1, label_2->setText("Item 1 in label2")

spinBox value = 2 and comboBox = ITEM2, label_2->setText("Item 2 in label2")

spinBox value = 2 and comboBox = ITEM3, label_2->setText("Item 3 in label2")

spinBox value = 2 and comboBox = ITEM4, label_2->setText("Item 4 in label2")

and so on.. .. .. for all the labels (i.e. label_2,label_3,label_4).

sample snippet,

void MainWindow::on_pushButton_clicked()
{
    int spinValue;
    QString comboText;
    spinValue=ui->spinBox->value();
    comboText=ui->comboBox->currentText();
    if(spinValue==1)
    {
        if(comboText=="LABEL 1")
        {
            ui->label->setText("ITEM 1 in field 1");
        }
        else if(comboText=="LABEL 2")
        {
            ui->label->setText("ITEM 2 in field 1");
        }.. .. .. .. .. .. ..

EDIT for better clarification, the user selects a value from spinbox (e.g. 1) selects a value from combobox (e.g. TEXT1) presses button; again selects a second value from spinbox (e.g. 2) selects a value from combobox (e.g. TEXT2) presses button etc... to populate data in all 4 labels.

i.e. in theory, the value in SpinBox is the label number.

Now, I want to ensure if the application restarts the state of the application should restore,

i.e. How to save the combination of SpinBox values and ComboBox values ???

2
your question is unclear. It seems like you want to periodically save state. It will be costly to save each time the user act.UmNyobe
i have put some more points for better clarification.. thank you for suggestion..RicoRicochet
actually i have space constraint in my UI so, i am using a spinbox and combobox to set the values in the labels. Cant afford 6 comboxes as i do not have much space in the form).RicoRicochet

2 Answers

2
votes

This can be done using QSettings. You can save the settings when the application is terminating. It could be done in the destructor of main window :

QSettings settings("organizationName","applicationName");
settings.setValue("settings/spinValue",ui->spinBox->value());
settings.setValue("settings/comboText",ui->comboBox->currentText());

In main window constructor you can read and set them accordingly :

QSettings settings("organizationName","applicationName");
spinValue = settings.value("settings/spinValue","").toInt();
comboText = settings.value("settings/comboText","").toString();
ui->spinBox->setValue(spinValue);
ui->comboBox->setCurrentText(comboText);
1
votes

You need to save:

  • The mapping spinValue <-> comboText <-> LabelText. In its simplest form, This is a table with three columns .
  • The last comboText, and the last spinValue.
  • Possibly all possible spin values, or all possible combo text. But this can be deduced after reading the table above

Because your logic is a table I would not use QSettings to save, but xml instead (or a table text file). There are resources to read\write xml using Qt

A xml like

  <?xml version="1.0" encoding="UTF-8" ?>
  <default spin="1" combo="LABEL 2"/>
  <mappings>
  <mapping spin="1" combo="LABEL 1">"VALUE A"</mapping>
  <mapping spin="1" combo="LABEL 2">"VALUE B"</mapping>
  <mapping spin="2" combo="LABEL 1">"VALUE C"</mapping>
  <mapping spin="2" combo="LABEL 2">"VALUE D"</mapping>
  </mappings>

Which can be loaded later to build a map

std::map<std::pair<int, QString>, QString> mapLabeltextfromSpinAndCombo;