Simpliest way for creating contextMenu for QTableWidget cells
-
In QTableWidget so many SLOTs, but only one for right click, and even that won't help with info about on which cell user rightclicked... Would be great to have same SLOT, but which would simply tell in params row&column.
Like SLOT cellClicked(int, int) should be also cellRightClicked(int, int). Why there is no such thing exist yet?
-
-
@SGaist said in Simpliest way for creating contextMenu for QTbaleWidget cells:
Hi,
You can get the model index using indexAt.
How can i use this function? I can't use it directly because it's not static. Create variable of this QAbstractItemView i also can't because alot of errors and in google it seems like a pretty complicated stuff(advanced Qt).
-
Why would you need it as static ?
You have QTableWidget instance (e.g.
_myTableWidget
), then connect itscustomContextMenuRequested
signal to some slots and in that slot callindexAt
.Something along the lines:
MainWindow(QWidget *parent) QWidget(parent) { _myTableWidget = new QTableWidget; // layout stuff etc. connect(_myTableWidget, &QTableWidget::customContextMenuRequested, this, &handleContextMenu); } void MainWindow::handleContextMenu(const QPoint& pos) { QTableWidgetItem *item = _myTableWidget->itemAt(pos); if (item) { // do what you want with the item. } }
-
@SGaist said in Simpliest way for creating contextMenu for QTableWidget cells:
QTableWidgetItem *item = _myTableWidget->itemAt(pos);
Imo there is a small mapping needed:
The exception to this rule is QAbstractScrollArea and its subclasses that map the context menu event to coordinates of the viewport(). -
@SGaist Ah now i get it. You misswrite in first post. Not indexAt but itemAt.
P.S. there is no need to making connect, i just use all stuff inside customContextMenuRequested SLOT.
P.P.S. everything seems to be well, compiles well, but not working i rly can't understand why:
void MWindow::on_tableWidMemory_customContextMenuRequested(const QPoint &pos) { QTableWidgetItem *tempItem = ui->tableWidMemory->itemAt(pos); ui->tableWidMemory->item(tempItem->row(), tempItem->column())->setBackgroundColor(Qt::green); }
Also tried:
ui->tableWidMemory->itemAt(pos.x(), pos.y())->setBackgroundColor(Qt::green);
Still nothing....
-
@Engelard I don't have a solution, but want to say that you're writing dangerous code: you're not checking whether tempItem is a valid pointer! If it is not your app will crash.
Also, shouldn't it be http://doc.qt.io/qt-5/qtablewidgetitem.html#setBackground? -
You just need to set the menu policy: http://doc.qt.io/qt-5/qwidget.html#contextMenuPolicy-prop
In
MWindow
constructor callsetContextMenuPolicy(Qt::CustomContextMenu);
Also note what @Christian-Ehrlicher said.
Instead ofui->tableWidMemory->itemAt(pos);
you'll probably needui->tableWidMemory->itemAt(ui->tableWidMemory->mapFromGlobal(mapToGlobal(pos)));
-
@jsulm said in Simpliest way for creating contextMenu for QTableWidget cells:
Also, shouldn't it be http://doc.qt.io/qt-5/qtablewidgetitem.html#setBackground?
Nope. setBackgroundColor is wroked well with leftClick slot.
-
@Engelard said in Simpliest way for creating contextMenu for QTableWidget cells:
nothing changed, still not working
Doeson_tableWidMemory_customContextMenuRequested
get executed at all?Actually realised my mistake. You are asking the context menu only for the table widget so instead of
setContextMenuPolicy(Qt::CustomContextMenu);
you should haveui->tableWidMemory->setContextMenuPolicy(Qt::CustomContextMenu);
and forget about themapFromGlobal
/mapToGlobal
part -
@VRonin said in Simpliest way for creating contextMenu for QTableWidget cells:
@Engelard said in Simpliest way for creating contextMenu for QTableWidget cells:
nothing changed, still not working
Doeson_tableWidMemory_customContextMenuRequested
get executed at all?Lol, no it does'nt. But after your:
Actually realised my mistake. You are asking the context menu only for the table widget so instead of
setContextMenuPolicy(Qt::CustomContextMenu);
you should haveui->tableWidMemory->setContextMenuPolicy(Qt::CustomContextMenu);
and forget about themapFromGlobal
/mapToGlobal
partIt called, but whole app crashes)
-
Now it's woring, i removed whole that stuff
QTableWidgetItem *tempItem = ui->tableWidMemory->itemAt(pos);
The only thing is left now:
ui->tableWidMemory->itemAt(pos)->setBackgroundColor(Qt::green);
And
ui->tableWidMemory->setContextMenuPolicy(Qt::CustomContextMenu);
in constructor.
Tnx VRonin for help. Still can't get why no slot for rightclick for QTableWidget cells, would be way efficient and simplier for newcomers. But anyway, at least it working with two extra-lines of code.
-
@Engelard said in Simpliest way for creating contextMenu for QTableWidget cells:
It called, but whole app crashes)
Where do you execute this code? It should be after ui->setupUI
-