Simpliest way for creating contextMenu for QTableWidget cells
-
@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
-
J JonB referenced this topic on