PDA

View Full Version : Show a context menu when the a column in a QTableView is selected



roov_raven
3rd November 2015, 11:19
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();
QModelIndexList ls = selectedIndexes();

foreach(QModelIndex i, ls)
qDebug() << i.column() << "," << i.row();

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. The first condition never evaluates to true. Using the qDebug() I realised that the first time I select the first column of the table the output is just


Inside mod.column= 0
But when I select the next column I get


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

meaning that mod.column() evaluates to 1 and selectedIndexes()[0].column() evaluates to 0 while they should be the same. Why is that? And how can I fix this?