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)?
    Thank you for advance.

    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 set 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);
        view->setSelectionBehavior(QAbstractItemView::SelectRows); // I said about the the line in problem description
    
        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;
    }
    


  • selectionModel()->clearSelection() for no selection or
    if you prefer to have a default first row selection, do the following:
    use selectionModel() of QTableView to send a signal currentRowChanged() or selectionChanged() with first row info?



  • @JoeCFD , I didn't get it. You mean to emit the signals or connect and handle them in my own way? Could you give an example?



  • QTableView *cbxView = new QTableView(this);
    
    // I set 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);
    
    // add code here
    cbxView->selectionModel()->clearSelection();


  • I tried to print this:

    qDebug() << selectionModel->hasSelection();
    

    It says false in apllication output. And I think that's why cbxView->selectionModel()->clearSelection(); has no effect.



  • try subclassing QComboBox like this:

    class SelectRowCombo : public QComboBox{
    Q_OBJECT
    Q_DISABLE_COPY(SelectRowCombo)
    public:
    explicit SelectRowCombo(QWidget *parent = Q_NULLPTR) : QComboBox(parent){}
    void showPopup() Q_DECL_OVERRIDE{
    QComboBox::showPopup();
    const QItemSelection rowSelection(model()->index(currentIndex(),0),model()->index(currentIndex(),model()->columnCount());
    view()->selectionModel()->select(rowSelection,QItemSelectionModel::Select);
    }
    };


  • I solved the problem reimplementing the showEvent method of QTableView. This post (link) helped me.

    I did something like this:

    class MyTableView : public QTableView
    {
        Q_OBJECT
    
    public:
        explicit MyTableView(QWidget *parent=0) : QTableView(parent) {}
    
    protected:
        void showEvent(QShowEvent *e)
        {
            if (e->type() == QShowEvent::Show)
                this->selectRow(this->currentIndex().row());
        }
    };
    

    It worked for me.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.