Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get to Completer Popup on Down Arrow
Qt 6.11 is out! See what's new in the release blog

How to get to Completer Popup on Down Arrow

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 424 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

    I've this subclass of QComboBox:

    CustomCombo::CustomCombo(QStandardItemModel * model){
        completer = new QCompleter(this);
        proxy = new QSortFilterProxyModel(this);
        table = new QTableView(this);
        proxy->setSourceModel(model);
        proxy->setFilterKeyColumn(0);
        proxy->setFilterCaseSensitivity(Qt::CaseInsensitive);
        completer->setModel(proxy);
        completer->setPopup(table);
        completer->setWidget(this);
        completer->setCaseSensitivity(Qt::CaseInsensitive);
    
        //completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
        table->horizontalHeader()->hide();
        table->verticalHeader()->hide();
        table->horizontalHeader()->setStretchLastSection(true);
        setModel(model);
        setContextMenuPolicy(Qt::NoContextMenu);
        setPlaceholderText("Select an Item");
        connect(this, &CustomCombo::currentTextChanged, [=]{proxy->setFilterFixedString(currentText());});
        connect(this, &CustomCombo::currentIndexChanged, [=]{setEditable(false);});
    }
    void CustomCombo::mousePressEvent(QMouseEvent *e){
        if(e->button() == Qt::RightButton){
            setEditable(true);
            setCompleter(completer);
            setCurrentText("");
        }
        QComboBox::mousePressEvent(e);
    }
    void CustomCombo::keyPressEvent(QKeyEvent *e){
        if(completer->popup()->isVisible() && e->key() == Qt::Key_Down)
            completer->popup()->setFocus(Qt::OtherFocusReason);
        else QComboBox::keyPressEvent(e);
    }
    

    and in the main window, I give it a model and add in layout like this:

    Window::Window(QWidget *parent) : QWidget(parent){
        comboModel = new QStandardItemModel();
        comboModel->setColumnCount(2);
        for (int i = 0; i < 3; i++) {
            auto col1 = new QStandardItem("Left " + QString::number(i + 1));
            auto col2 = new QStandardItem("Right " + QString::number(i + 1));
            col2->setTextAlignment(Qt::AlignRight);
            comboModel->appendRow(QList<QStandardItem*>() << col1 << col2);
    
            col1 = new QStandardItem("Right " + QString::number(i + 1));
            col2 = new QStandardItem("Left " + QString::number(i + 1));
            col2->setTextAlignment(Qt::AlignRight);
            comboModel->appendRow(QList<QStandardItem*>() << col1 << col2);
        }
        auto lay = new QVBoxLayout(this);
        auto combo = new CustomCombo(comboModel);
        lay->addWidget(combo);
        lay->addStretch(1);
        setLayout(lay);
    }
    

    When I right click it clears the text and as I type in, it shows the matches. When I press down arrow, I want it to go to the popup and select items there instead of changing text in the box. Here's what it does on down arrow press:

    x1.gif

    One more thing, how to show both columns of the selected item in the combo box. Right now it shows only one column in the box. I can see other column only when popup opens. Something like this:

    x2.gif

    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