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. QComboBox with QTableView, selection or focus problem when combobox items pop up
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.7k Views
  • 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.
  • B Offline
    B Offline
    bduminyuk
    wrote on last edited by bduminyuk
    #1

    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;
    }
    
    1 Reply Last reply
    0
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #2

      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?

      B 1 Reply Last reply
      1
      • JoeCFDJ JoeCFD

        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?

        B Offline
        B Offline
        bduminyuk
        wrote on last edited by
        #3

        @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?

        1 Reply Last reply
        0
        • JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by JoeCFD
          #4
          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();
          
          1 Reply Last reply
          0
          • B Offline
            B Offline
            bduminyuk
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              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);
              }
              };

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0
              • B Offline
                B Offline
                bduminyuk
                wrote on last edited by bduminyuk
                #7

                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.

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved