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. Show a context menu when the a column in a QTableView is selected
Forum Updated to NodeBB v4.3 + New Features

Show a context menu when the a column in a QTableView is selected

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 2.4k 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.
  • R Offline
    R Offline
    RDiGuida
    wrote on last edited by
    #1

    Hello, as the title says I am trying to implement a context menu that appears on left click if a whole column is selected in a TableView.

    In my MainWindow I have these attributes

    private:
        QString mFilePath;
        DatasetModel model;
        myTabView* view;
        DataFrame data;
        TabSelModel* selModel;
    

    where myTabView is just a reimplemented QTableView. I installed an event filter in the constructor of the MainWindow like this view->installEventFilter(this); and I implemented di eventfilter in the Main Window like this

    bool MainWindow::eventFilter(QObject *watched, QEvent *e)
    {
        if(watched==view && e->type()==QEvent::MouseButtonPress)
        {
            QModelIndexList idxl = view->selectedIndexes();
            QMouseEvent *mou = static_cast<QMouseEvent*>(e);
            if(mou->button()==Qt::RightButton && selModel->ColSelected.first)
            {
                ShowContextMenu(mou->pos());
                return true;
            }
        }
        return QWidget::eventFilter(watched,e);
    }
    

    Where ColSelected is a QPair implemented in a slot of my TabSelModel (a reimplemented QItemSelectionModel). The slot looks like this

    void TabSelModel::CheckSelection(const QModelIndex& mod,)
    {
        qDebug() << "Inside mod.column=" << mod.column();
    
        if(isColumnSelected(mod.column(),mod))                 //never evaluates to true
            ColSelected = qMakePair(true,mod);
        else
            ColSelected.first = false;
    }
    

    which is connected to a simple SIGNAL(currentChanged()) from the selection model. I manage to get into the slot (the qDebug outputs the correct column) but the first condition never evaluates to true. How is that possible?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      isColumnSelected checks for selection in the parent given in second param, so this should rather be
      if(isColumnSelected(mod.column(),mod->parent())).

      1 Reply Last reply
      0
      • R Offline
        R Offline
        RDiGuida
        wrote on last edited by
        #3

        I changed the code but still I never reach ColSelected = qMakePair(true,mod);.

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Maybe a silly question, but is the whole column actually selected ? If you get selected items in that slot does that contain all indices in the column?

          1 Reply Last reply
          0
          • R Offline
            R Offline
            RDiGuida
            wrote on last edited by
            #5

            Actually I get all the rows but I get the previously selected column. The code is

            void TabSelModel::CheckSelection(const QModelIndex& mod)
            {
                qDebug() << "Inside mod.column=" << mod.column();
                QModelIndexList ls = selectedIndexes();
            
                foreach(QModelIndex i, ls)
                    qDebug() << i.column() << "," << i.row();
            

            And the first time I select the first column of the table the output is just

            Inside mod.column= 0
            

            When I select the next column I get

            Inside mod.column= 1
            0 , 0
            0 , 1
            0 , 2
            0 , 3
            ...
            0, 66
            

            How is that possible?

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Most things in c++ are indexed from 0, so first column has index 0, second 1 and so on.

              1 Reply Last reply
              0
              • R Offline
                R Offline
                RDiGuida
                wrote on last edited by
                #7

                I know, what is strange is that mod.column() evaluates to 1 and selectedIndexes()[0].column() evaluates to 0 while they should be the same.

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  RDiGuida
                  wrote on last edited by
                  #8

                  May it be related to some sort of dragging of the previous selection?

                  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