tableContextMenu sender not work
-
Have a tableContextMenu in a tableWidget but depends of the cell clicked, the actions displayed on the context menu will be different, for identifying de cell clicked the sender is not work for me
void MainWindow::tableContextMenu(QPoint pos) { QMenu * menu = new QMenu(this); QWidget *widget = qobject_cast<QWidget*>(sender()); QAction * editColor = new QAction(("Change Color"), this); QAction * editChannel = new QAction(); QWidget *it= ui->channelsTable->cellWidget(32,1); if(widget == it) { editChannel->setText("Descombine channels"); } else { editChannel->setText("Combine channels"); } editChannel->setParent(this); connect(editColor, SIGNAL(triggered()), this, SLOT(slotEditColor())); connect(editChannel, SIGNAL(triggered()), this, SLOT(combineChannelManager())); menu->addAction(editColor); menu->addAction(editChannel); menu->popup(ui->channelsTable->viewport()->mapToGlobal(pos)); }
Never obtain the Descombine channel text in context menu. What is wrong?
-
How do you connect this slot? Hopefully from the cellWidget - otherwise sender() can never be the cell widget.
-
In MainWindow constructor
connect(ui->channelsTable, SIGNAL(customContextMenuRequested(QPoint)), this,SLOT(tableContextMenu(QPoint)));
This already works, but later I need to add a matter of show a different menu option only when the cell (32,1) is right cliked in the tablewidget
-
So how should sender ever be the cell widget then?
You can find the cell under the mouse cursor with QTableWidget::itemAt(). -
@Christian-Ehrlicher
I assume that sender is anything that trigger something including the context menu, but whatever, the QTableWidget::itemAt(). works fine and is easier to implement and use.
Thanks. Problem resolved. -
@LCorona said in tableContextMenu sender not work:
I assume that sender is anything that trigger something
Correct, and as you see in your connect() statement the sender is the QTableWidget ...