Re: QtableWidget custom menu - information from cell
so I made connection to create a popup menu
this->setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(provideContexMenu(const QPoint&)));
and the slot to print the menu:
void ListView::provideContexMenu(const QPoint &position) {
QMenu menu(this);
QAction* open = new QAction("Open", this);
connect(open, SIGNAL(triggered()), this, SLOT(open()));
menu.addAction(open);
menu.exec(QCursor::pos());
}
void ListView::open() {
// action here depends on info
}
how to actually have the information from the cell or the row ??? if the provideContextMenu passing just the const QPoint &position ?
Please advise, super thank you :)
Added after 1 32 minutes:
I am trying to get the item using itemAt but the app crashes when I right click on different column then I want to.
when I click on column 1 working fine, anything else the app crashes.
Code:
void ListView
::provideContexMenu(const QPoint &position
) { selectedItem = this->itemAt(position.x(), position.y());
if (selectedItem->column() == 1) {
menu->addAction(shareAct);
menu->addAction(openAct);
menu->addAction(viewAct);
}
}
what I do wrong here ? is there another way to handle right click on specific column and receive the item info ??
thanks for help
It actually crashes on column 0 but on column 1 and 2 still prints the menu
Re: QtableWidget custom menu - information from cell
Solved:
the problem that the app crashed on column 0 was that I set
Code:
this->setCellWidget(row, col, status);
so there wasn't any item there
I just put an empty item and it worked
Code:
this->setItem(row, col, statusItem);
this->setCellWidget(row, col, status);
The reason that it shows menu on column 2 was that I simply printed it anyway, I just missed that.
Code:
void ListView
::provideContexMenu(const QPoint &position
) { selectedItem = this->itemAt(position.x(), position.y());
if (selectedItem->column() == 1) {
menu->addAction(shareAct);
menu->addAction(openAct);
menu->addAction(viewAct);
}
menu
->exec
(QCursor::pos());
// HERE , STUPID ...}
also this works either
Code:
selectedItem = this->itemAt(position);
instead
Code:
selectedItem = this->itemAt(position.x(), position.y());