Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to change just one item of QCombobox editable by the user



  • i have 4 items in my QCombobox,

    'Bryce king'
    'James White'
    'Russo W'
    'Custom Manager'
    

    so when i click on

    'Custom Manager',
    

    it should change to editable and i must be able to enter my own desired name.

    i have tried to achieve this behavior using Qtcreator, and in the properties, i can set it to editable but that would make all items editable instead of just 1.


  • Lifetime Qt Champion

    Hi
    Editable is for all items then.
    It cant handle only one being editable as its not the design.

    The normal use case for this would be to have it nonedible and then have

    'Add Custom Manager'

    Then when you select that, it shows a QInputDialog allowing users to enter a name and press ok to have that added to the listed and selected as the current one.

    That said something like this does work ok-ish

    void MainWindow::on_comboBox_currentIndexChanged(int index)
    {
        if (index == ui->comboBox->count()-1)
            ui->comboBox->setEditable(true);
        else
            ui->comboBox->setEditable(false);
    }
    


  • @mrjj Thanks So much !!


  • Lifetime Qt Champion

    @learnist
    Np :)
    However, for a real app, you should also consider the use case where users spell the new name
    wrong and want to delete it/edit it later to correct it.


Log in to reply