How to get to Completer Popup on Down Arrow
Unsolved
General and Desktop
-
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:
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: