Show a context menu when the a column in a QTableView is selected
-
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
myTabViewis just a reimplementedQTableView. I installed an event filter in the constructor of the MainWindow like thisview->installEventFilter(this);and I implemented dieventfilterin the Main Window like thisbool 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
ColSelectedis aQPairimplemented in a slot of myTabSelModel(a reimplementedQItemSelectionModel). The slot looks like thisvoid 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 (theqDebugoutputs the correct column) but the first condition never evaluates to true. How is that possible? -
isColumnSelectedchecks for selection in the parent given in second param, so this should rather be
if(isColumnSelected(mod.column(),mod->parent())). -
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?
-
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= 0When I select the next column I get
Inside mod.column= 1 0 , 0 0 , 1 0 , 2 0 , 3 ... 0, 66How is that possible?
-
Most things in c++ are indexed from 0, so first column has index 0, second 1 and so on.