Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to sort Qt combobox item after removing item from combobox?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
3 Posts 3 Posters 2.6k Views 1 Watching
  • 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.
  • N Offline
    N Offline
    NTCYP
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        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