How to sort Qt combobox item after removing item from combobox?



  • Hi,

    I have a Comobox in my DialogWindow. I add 3 items in it.

        // TEST DATA LOAD
        ui->comboBoxSQLData->addItem("AAA");
        ui->comboBoxSQLData->addItem("BBB");
        ui->comboBoxSQLData->addItem("CCC");
        QCoreApplication::processEvents();
    

    I also have my connect(SIGNAL/SLOT) for combobox item changed

       // COMBOBOX SELECTION CHANGED
        QObject::connect(ui->comboBoxSQLData, SIGNAL(activated(int)), this, SLOT(handleSelectionChanged(int)));
    

    My combobox item changed function:

    void DialogWRITE_CARD::handleSelectionChanged(int index)
    {
        // Combobox item text changed
        if(ui->comboBoxSQLData->itemText(index) == "AAA")
        {
            ui->lineEditWrite->setText("AAA");   
        }
        else if(ui->comboBoxSQLData->itemText(index) == "BBB")
        {
            ui->lineEditWrite->setText("BBB);
        }
        else if(ui->comboBoxSQLData->itemText(index) == "CCC")
        {
            ui->lineEditWrite->setText("CCC");   
        }
    }
    

    Now I need to remove item first from combobox and than I need to sort item in combobox. How can I do that?

    In below function I can remove item from combobox but I dont know how to sort the item?

    void DialogWRITE_CARD::on_pushButtonKartYaz_clicked()
    {
        // Do something with text
        ………
    
        int cItem = ui->comboBoxSQLData->count();
    
        // Remove Current Combobox Index
        if(cItem != 0)
        {
            // Remove Current ComoboBox Item
            ui->comboBoxSQLData->removeItem(ui->comboBoxSQLData->currentIndex());
    
            // Now Sort the combo box after removing item
            ????????????????
        }
    }
    

    Kind Regards


  • Lifetime Qt Champion

    Hi,

    I haven't tested but you can use the sort function of the QComboBox's model.

    Note that handleSelectionChanged's implementation isn't really optimal. Why not connect comboBoxSQLData's activated(QString)signal on lineEditWrite's setText slot ?


  • Moderators

    If you remove an element from a sorted list then the list is still sorted. So, do you really need to sort?


Log in to reply
 

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