PDA

View Full Version : QtableWidget custom menu - information from cell



migel
1st June 2011, 13:43
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.


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());
}

}

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

migel
1st June 2011, 14:02
Solved:

the problem that the app crashed on column 0 was that I set

this->setCellWidget(row, col, status);
so there wasn't any item there

I just put an empty item and it worked


QTableWidgetItem *statusItem = new QTableWidgetItem;
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.


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());
}
menu->exec(QCursor::pos()); // HERE , STUPID ...
}



also this works either

selectedItem = this->itemAt(position);
instead

selectedItem = this->itemAt(position.x(), position.y());