0

I am new to QT and GUI related programming and looking to do a 2 tier selection menu in a project. I appreciate the time taken to help.

Example: Combo box 1 has options like: 1. Screen size - Medium 2. Screen size - large and depending on that I would like to display different options for screen resolution in combo box 2.

The user can change the combo box 1 selection any number of times and box 2 should show the appropriate options.

I have tried using QComboBox.setEnabled(False) and True as was suggested in Disabling QComboBox in pyqt but it has not worked for me and I am certainly missing something.

Snippet of my code:

void interface::changeFunctionx(int index)
{
    delete f;
    switch(index)
    {
    case 0:
        version = 1;
        functionSely->setVisible(1);
        break;

    case 1:
        version = 1;
        //some other function call still seeing how gui works
        break;
    }
}
Community
  • 1
  • 1
  • If you can post any code, it will help to answer your question. – Alper Akture Oct 15 '15 at 20:31
  • There are two options, having two comboboxes and calling `setVisible()` on both depending on the current selection or clear and repopulate the combobox on the selection changes. You have to implement this using a custom slot in your code, default slots will not be sufficient. But it would be better if you show some code of what you have tried so far. – Bowdzone Oct 15 '15 at 20:32
  • void interface::changeFunctionx(int index) { delete f; switch(index) { case 0: version = 1; functionSely->setVisible(1); break; case 1: version = 1; break; } } functionSely is the combo box which i am setting visible to 0 initially. The issue is when I make the right selection in functionSelx the program quits execution. – Starving Magician Oct 15 '15 at 21:06

1 Answers1

0

The Data of the QComboBox can be re-filled using below logic

QComboBox::clear()
QComboBox::insertItems(0,QStringList);

//Declare the variable needed in the class

class myclass : public QMainWindow {
    QList<QString> lst;
    QStringList ql1,ql2;
}

Two combo box used in this example. i.e, cbo1 (2 items) (user selection) & cbo2 (re-fill data)

//Combo box fill Data Preparation
//In this example, the below function called only one time (called from the end line of constructor) (to initialize)

void fnPrepareStaticData(){
    lst.push_back("Option-1");
    lst.push_back("Option-2");
    lst.push_back("Option-3");
    ql1 = (QStringList(lst));

    lst.clear();
    lst.push_back("New-1");
    lst.push_back("New-2");
    lst.push_back("New-3");
    ql2 = (QStringList(lst));
}

//slot added to the QComboBox (currentIndexChanged(int index))

void MainWindow::on_cbo1_currentIndexChanged(int index)
{
    ui->cbo2->clear();
    switch (index) {
    case 0:
        ui->cbo2->insertItems(0,ql1);
        break;
    case 1:
        ui->cbo2->insertItems(0,ql2);
        break;
    default:
        break;
    }
}
Jeet
  • 1,006
  • 1
  • 14
  • 25