Results 1 to 10 of 10

Thread: open context menu unde the items of a QListWidget

  1. #1
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up open context menu unde the items of a QListWidget

    Hi all, I have a QListWidget where I show as icons the images in a specified directory. I want that when the user presses the right button of the mouse under an item of the list, it selects it and also opens a context menu with actions to work with the item or just opens the context menu if the selection (of one or more items) already exists. Anybody knows what's the simplest way to implement this behaviour? Thanks.
    Last edited by Dark_Tower; 9th April 2006 at 11:12.

  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: open context menu unde the items of a QListWidget

    Check the second post in this thread:
    http://www.qtcentre.org/forum/showthread.php?t=1710
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    Dark_Tower (9th April 2006)

  4. #3
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: open context menu unde the items of a QListWidget

    Should I have to re-implement "mousePressEvent" in the list to catch when the user presses the right button or there's another way to make it automaticly?

    Edit: I answer myself, I have to use QContextMenuEvent...
    Last edited by Dark_Tower; 9th April 2006 at 11:21.

  5. #4
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: open context menu unde the items of a QListWidget

    Hi again. I still have a question about the context menu: is it strictly necesary to subclass a widget to handle context menus with it? I think that a context menu should be something that you can manage from "outside" the class... Can anybody explain me if it's possible and how to handle the context menus of a widget without having to subclass it to catch the QContextMenuEvent?
    Last edited by Dark_Tower; 12th April 2006 at 18:01.

  6. #5
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: open context menu unde the items of a QListWidget

    Any comments, please?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: open context menu unde the items of a QListWidget

    Quote Originally Posted by Dark_Tower
    Can anybody explain me if it's possible and how to handle the context menus of a widget without having to subclass it to catch the QContextMenuEvent?
    Open Qt Assistant, select the "Index" tab, enter "QListWidget" in the "Look for" text edit, select QListWidget from the list, click "List of all members, including inherited members" link, press Ctrl+F, enter "contextMenu" in the dialog and press return 5 times. I hope you will know what to do next.

  8. The following user says thank you to jacek for this useful post:

    Dark_Tower (14th April 2006)

  9. #7
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: open context menu unde the items of a QListWidget

    Quote Originally Posted by jacek
    Open Qt Assistant, select the "Index" tab, enter "QListWidget" in the "Look for" text edit, select QListWidget from the list, click "List of all members, including inherited members" link, press Ctrl+F, enter "contextMenu" in the dialog and press return 5 times. I hope you will know what to do next.
    I think that I could find the treasure in the island following this instructions but all I've found is the "contextMenuEvent" that's a protected method so I should need to subclass QListWidget to use it and that's exactly what I want to avoid, if it's possible...

  10. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: open context menu unde the items of a QListWidget

    Quote Originally Posted by Dark_Tower
    but all I've found is the "contextMenuEvent" that's a protected method
    Then you should look harder next time.

    void QWidget::customContextMenuRequested ( const QPoint & pos ) [signal]
    This signal is emitted when the widget's contextMenuPolicy is Qt::CustomContextMenu, and the user has requested a context menu on the widget. The position pos is the position of the context menu event that the widget receives. Normally this is in widget coordinates. The exception to this rule is QAbstractScrollArea and its subclasses that map the context menu event to coordinates of the viewport() .
    See also mapToGlobal(), QMenu, and contextMenuPolicy.
    And there are event filters too.

  11. The following user says thank you to jacek for this useful post:

    Dark_Tower (14th April 2006)

  12. #9
    Join Date
    Jan 2006
    Posts
    105
    Thanks
    38
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: open context menu unde the items of a QListWidget

    Sorry, I've repeated the steps and finally I've found the treasure: customContextMenuRequested Thanks and sorry jacek

  13. #10
    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: open context menu unde the items of a QListWidget

    QWidget::contextMenuPolicy

    Qt:efaultContextMenu -> override/catch contextMenuEvent
    Qt::ActionsContextMenu -> a context menu containing all actions added to a widget is constructed and shown automatically
    Qt::CustomContextMenu -> widget emits signal: customContextMenuRequested()

    The latter 2 policies allow you to play with widget's context menus without subclassing or filtering events. For the first one, subclassing or an event filter is needed.
    J-P Nurmi

Similar Threads

  1. QListWidget context menu using Designer
    By palsa in forum Qt Programming
    Replies: 1
    Last Post: 31st August 2008, 13:57

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.