Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    Mobile and Embedded
    3
    3
    2262
    Loading More Posts
    • 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
      NTCYP last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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 Reply Quote 0
        • jsulm
          jsulm Lifetime Qt Champion last edited by

          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 Reply Quote 0
          • First post
            Last post