PDA

View Full Version : Get last event in QTreeWidget



danilodsp
14th August 2011, 18:56
I wonder if the last event was Qt::RightButton. For QTreeWiget.

My code:

void vf_rfid::on_treeWidget_itemClicked(QTreeWidgetItem * item, int column)
{
if(item->childCount()==0){
QMenu menu(ui->treeWidget);
QAction *action = menu.addAction("Remover");
QObject::connect(action,SIGNAL(triggered()),this,S LOT(teste()));
menu.exec(ui->treeWidget->mapToParent(QCursor::pos()));
}
}

But I want it up only occurs with right click event.

I found:
http://developer.qt.nokia.com/faq/answer/how_to_prevent_right_mouse_click_selection_for_a_q treewidget
great tutorial,
But I do not want to create another class TreeWidget.

NullPointer
14th August 2011, 22:13
Hi,

Try using QTreeWidget::customContextMenuRequested(QPoint pos), and use the QTreeWidget::itemAt(QPoint&) to find the item. If that returns 0, just skip the menu...
You will need to change the contextMenuPolicy of the tree widget...

Hth.

danilodsp
14th August 2011, 23:58
Ok, it worked.

Thank you.

Was inserted in the main: ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);


void vf_rfid::on_treeWidget_customContextMenuRequested( QPoint pos)
{
QMenu menu(ui->treeWidget);
selT = ui->treeWidget->currentIndex().row();

...
QAction *desativar = menu.addAction("Desativar");
QObject::connect(desativar,SIGNAL(triggered()),thi s,SLOT(desativarT()));
...

menu.addSeparator();
QAction *remover = menu.addAction("Remover");
QObject::connect(remover,SIGNAL(triggered()),this, SLOT(removerT()));

menu.exec(ui->treeWidget->mapToParent(QCursor::pos()));
}

NullPointer
15th August 2011, 00:22
Mano,

Use the pos you receive by parameter... QCursor::pos() may be other, if you have a queued connection.
And more, why don't you leave the menu created, and just QMenu::popup(pos)?
This way you will not need to create the actions and connect them every time the user right-click the item...

;)

danilodsp
15th August 2011, 01:44
Hi,

I'm not using the "pos" because it's not located where the cursor is positioned. :confused:
I did not know of the popup. I will use it.
Thanks for the tip.