Search code examples
c++qtqcombobox

QComboBox with QTableView, selection or focus problem when combobox items pop up


I changed the standart view of combobox items using QTableView.

The problem is with the setting of selection. I set QAbstractItemView::SelectRows for the view and it works fine when I hover cursor on the rows of QTableView (see setup_table_view and Pic. 2).

But when combobox opens and I don't move cursor to the combobox items then I see selection on the cell of one column (which is defined using setModelColumn, see Pic. 1).

I tried to play with selection mode options but unsuccessfully.

So, how can I set up my program to select full row when I clicked and combobox pops up (to get result of Pic. 2 with cursor position of Pic. 1)?

P.S. I made the cursor brown for the better contrast.


Pic. 1: The mouse cursor on the same place where click was happened.

0_1540454500496_11.PNG

Pic. 2: The mouse cursor was moved to the 1st row.

1_1540454500496_22.PNG


Here is a simplified code:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QStandardItemModel *model = get_model(); // here I get the model (see below).

    QTableView *cbxView = new QTableView(this);

    // I define the model for the view to set static column width   
    cbxView->setModel(model); 

    setup_table_view(cbxView); // set view prorerties (see below)

    // set up my combobox with model and view
    ui->comboBox->setModelColumn(1);
    ui->comboBox->setView(cbxView);
    ui->comboBox->setModel(model);
}

/*
 *  Here I set up the view properties
 */
void setup_table_view(QTableView * view)
{
    view->setSelectionMode(QAbstractItemView::SingleSelection);

    // I said about the the line below in problem description
    view->setSelectionBehavior(QAbstractItemView::SelectRows);     

    view->setColumnWidth(0, 30);
    view->horizontalHeader()->setStretchLastSection(true);
    view->verticalHeader()->setStretchLastSection(true);

    view->verticalHeader()->hide();
    view->horizontalHeader()->hide();
}

/*
 *  My model stub (for example). You can skip it. 
 *  It is not important. But maybe it can be useful for someone.
 */
QStandardItemModel* get_model()
{
    QString names[] = {"Alex", "Tim", "Mary", "Ben", "Nicole", "Max"};
    QString indexes[] = {"1", "3", "6", "5", "4", "2"};
    QString addresses[] = {
        "Alex address", "Tim address", "Mary address",
        "Ben address", "Nicole address", "Max address" 
    };

    QStandardItemModel *model = new QStandardItemModel;

    for(int i = 0; i < 6; i++)
    {
        model->setItem(i, 0, new QStandardItem(indexes[i]));
        model->setItem(i, 1, new QStandardItem(names[i]));
        model->setItem(i, 2, new QStandardItem(addresses[i]));
    }

    return model;
}

Solution

  • You need to reimplement QTableView and override showEvent

    void MyTable::showEvent(QShowEvent *e)
    {
        if (e->type() == QShowEvent::Show)
        {
           this->selectRow(0);
        }
    }
    

    And set this class as view for combobox