Results 1 to 7 of 7

Thread: QListWidget right-mouse click

  1. #1
    Join Date
    Jun 2006
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QListWidget right-mouse click

    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 ??

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QListWidget right-mouse click

    Reimplement QWidget::contextMenuEvent() and use QListWidget::itemAt() to get item at given position.
    J-P Nurmi

  3. #3
    Join Date
    Jun 2006
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: QListWidget right-mouse click

    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

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QListWidget right-mouse click

    That's the default behaviour. There are a few different ways for implementing a context menu. See QWidget::contextMenuPolicy for other possibilities. Also, this topic has been discussed several times on the forums.
    J-P Nurmi

  5. #5
    Join Date
    Mar 2007
    Posts
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QListWidget right-mouse click

    create the signal / slot for the QListWidget like this..
    Assuming the QListWidget is called lbUserList...
    Qt Code:
    1. connect(lbUserList, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(userListMenuRequested(const QPoint &)));
    To copy to clipboard, switch view to plain text mode 

    Then in the slot place some code like this

    Qt Code:
    1. void MainWindowImpl::userListMenuRequested(const QPoint & pos)
    2. {
    3. QPoint globalPos = lbUserList->mapToGlobal(pos); // Map the global position to the userlist
    4. QModelIndex t = lbUserList->indexAt(pos);
    5. lbUserList->item(t.row())->setSelected(true); // even a right click will select the item
    6. userListMenu->exec(globalPos);
    7. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jun 2006
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget right-mouse click

    clive : Thanx. .. that looks better

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

  7. #7
    Join Date
    Jun 2007
    Posts
    52
    Thanks
    6

    Default Re: QListWidget right-mouse click

    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

Similar Threads

  1. The event fired by the mouse click on the frame
    By Placido Currò in forum Qt Programming
    Replies: 8
    Last Post: 3rd March 2007, 09:05
  2. Mouse Drag selction Issue in QListWidget
    By vishal.chauhan in forum Qt Programming
    Replies: 5
    Last Post: 21st February 2007, 08:55
  3. mouse click in QGprahicsScene
    By Morea in forum Qt Programming
    Replies: 11
    Last Post: 21st December 2006, 08:21
  4. QListWidget selection behavior
    By Arthur in forum Qt Programming
    Replies: 1
    Last Post: 30th May 2006, 14:10
  5. mouse click event
    By vijay anandh in forum Qt Programming
    Replies: 1
    Last Post: 1st May 2006, 09:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.