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

How to get the text of a complete in a combobox



  • Hi Can someone help me with my problem, I have a combobox that I fill with a QList and now I have generated a search engine with the help of the QCompleter, so far everything is fine, but I have edited it to make it visually more attractive, and it worked correctly until I have trouble selecting with the mouse one of the options that the QCompleter threw at me and the selection does not respect me, it only respects me if I give an Enter with the keyboard, but only for a moment, someone could help me solve my problem.

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
     qApp->setStyleSheet("QAbstractItemView#completerPopup {font: 15pt ;color: rgb(255, 255, 255);}");
    }
    
        QCompleter *completer = new QCompleter(list, this);
        completer->setFilterMode(Qt::MatchContains);
        completer->setCaseSensitivity(Qt::CaseInsensitive);
        completer->popup()->setObjectName("completerPopup");
        ui->cobobox->setCompleter(completer);
    

    I manage to make it work but I have to remove some lines of code that are the ones that help me edit the QCompleter as Pupop.

        QCompleter *completer = new QCompleter(list, this);
        completer->setFilterMode(Qt::MatchContains);
        completer->setCaseSensitivity(Qt::CaseInsensitive);
        ui->cobobox->setCompleter(completer);
    

    The error it throws when I select is the following
    Setting a QCompleter on non-editable QComboBox is not allowed.



  • @Morado said in How to get the text of a complete in a combobox:

    Setting a QCompleter on non-editable QComboBox is not allowed.

    https://doc.qt.io/qt-5/qcombobox.html#editable-prop

    This property holds whether the combo box can be edited by the user

    By default, this property is false. The effect of editing depends on the insert policy.



  • Thanks for responding so quickly,I try this

    ui->combobox->setInsertPolicy(QComboBox::InsertAtCurrent);
    

    And with all its possible combinations:

    QComboBox::InsertAtTop
    QComboBox::InsertAtBottom
    QComboBox::InsertAfterCurrent
    QComboBox::InsertBeforeCurrent
    QComboBox::InsertAlphabetically
    

    But it did not give me any favorable results.
    Or is it that I'm wrong about something?



  • @Morado
    Did you do the setEditable(true) per the link? That is what makes the QComboBox editable. setInsertPolicy() is irrelevant.

    The default QComboBox is just a dropdown non-editable list. It does not accept text input, so a QCompleter makes no sense. You have to set it editable to get an edit widget and a completer.



  • @JonB
    I had just done it before you answered me, and it doesn't affect it in the same way.

    ui->combobox->setEditable(true);
    ui->combobox->setCompleter(completer);
    

    or I am doing something wrong again?



  • @JonB

    I was reviewing the documentation and enabling setEditable only allows me to write to the Combobox and I have already achieved this from the GUI, my problem is that it does not allow me to select from the popup with the mouse. But with keyboard yes.



  • @Morado
    It was you who wrote

    The error it throws when I select is the following

    Setting a QCompleter on non-editable QComboBox is not allowed.

    That's all I know.


Log in to reply