PDA

View Full Version : QListWidget right-mouse click



Skizmo
27th March 2007, 09:48
Hi,

I'm a bit puzzled. I'm trying to create a popupmenu when I right-click a QListWidget, but I can only find the itemClicked () function, and this function doesn't tell me which button is used to click. Anyone here who knows how this 'normally' works ??

jpn
27th March 2007, 10:07
Reimplement QWidget::contextMenuEvent() and use QListWidget::itemAt() to get item at given position.

Skizmo
27th March 2007, 10:14
Reimplement ?? You mean to say that I have to subclass it so I can create a popupmenu ?? That would be totally stupid. I thought QT tried to make it easy for us ;)

jpn
27th March 2007, 10:19
That's the default behaviour. There are a few different ways for implementing a context menu. See QWidget::contextMenuPolicy (http://doc.trolltech.com/4.2/qwidget.html#contextMenuPolicy-prop) for other possibilities. Also, this topic has been discussed several times on the forums.

clive
27th March 2007, 10:19
create the signal / slot for the QListWidget like this..
Assuming the QListWidget is called lbUserList...


connect(lbUserList, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(userListMenuRequested(const QPoint &)));


Then in the slot place some code like this



void MainWindowImpl::userListMenuRequested(const QPoint & pos)
{
QPoint globalPos = lbUserList->mapToGlobal(pos); // Map the global position to the userlist
QModelIndex t = lbUserList->indexAt(pos);
lbUserList->item(t.row())->setSelected(true); // even a right click will select the item
userListMenu->exec(globalPos);
}

Skizmo
27th March 2007, 10:48
clive : Thanx. .. that looks better :)

jpn : I used the search a few times, but could not find anything. . .

abbapatris
20th June 2007, 13:13
I just wanted to thank JPN for his posts. I have been looking for some of this information and what he posted really helped out. I know this post is a few months old, but thanks. HUGE help