Results 1 to 2 of 2

Thread: QListWidget context menu using Designer

  1. #1
    Join Date
    Aug 2008
    Location
    Espoo, Finland
    Posts
    1
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy QListWidget context menu using Designer

    I would like to create a context menu for a QListWidget, while continuing to use Designer for creating the user interface. At the moment it looks like I need to subclass QListWidget and implement void QWidget::contextMenuEvent ( QContextMenuEvent * event ).

    The problem is, does this mean I can no longer use Designer generated code and have to implement the UI handcoding?

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QListWidget context menu using Designer

    for using context menu you should do next steps:
    1. set for your widget next property
    Qt Code:
    1. ...
    2. widget->setContextMenuPolicy(Qt::CustomContextMenu);
    3. ...
    To copy to clipboard, switch view to plain text mode 
    2. then create slot which will create and show context menu and connect it with signal customContextMenuRequested(const QPoint &)
    Qt Code:
    1. ...
    2. connect(widget SIGNAL(customContextMenuRequested(const QPoint &)),
    3. SLOT(showContextMenuForWidget(const QPoint &)));
    4. ...
    To copy to clipboard, switch view to plain text mode 
    3. example of showContextMenuForWidget slot
    Qt Code:
    1. void MainWindow::showContextMenuForWidget(const QPoint &pos)
    2. {
    3. QMenu contextMenu(tr("Context menu"), this);
    4. contextMenu.addAction(new QAction(tr("Hello"), this));
    5. contextMenu.exec(mapToGlobal(pos));
    6. }
    To copy to clipboard, switch view to plain text mode 

  3. The following 4 users say thank you to spirit for this useful post:

    jiveaxe (25th March 2012), palsa (31st August 2008), reasra (28th June 2013), sreerams (3rd April 2013)

Similar Threads

  1. Context Menu on QTableWidget
    By ankurjain in forum Qt Programming
    Replies: 9
    Last Post: 17th December 2009, 10:52
  2. QTable context menu
    By grabnerj in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2008, 06:20
  3. context menu problem
    By dreamer in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2008, 14:18
  4. Replies: 4
    Last Post: 25th June 2007, 21:40
  5. Replies: 13
    Last Post: 15th December 2006, 12:52

Tags for this Thread

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.