Results 1 to 2 of 2

Thread: QSignalMapper, QMenu and custom context menu

  1. #1
    Join Date
    Dec 2016
    Location
    New England, US
    Posts
    31
    Thanks
    6
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSignalMapper, QMenu and custom context menu

    Thanks to this thread, I discovered QSignalMapper, and found this nice example in Qt's doc:
    Qt Code:
    1. ButtonWidget::ButtonWidget(const QStringList &texts, QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. signalMapper = new QSignalMapper(this);
    5.  
    6. QGridLayout *gridLayout = new QGridLayout;
    7. for (int i = 0; i < texts.size(); ++i) {
    8. QPushButton *button = new QPushButton(texts[i]);
    9. connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
    10. signalMapper->setMapping(button, texts[i]);
    11. gridLayout->addWidget(button, i / 3, i % 3);
    12. }
    13.  
    14. connect(signalMapper, SIGNAL(mapped(QString)),
    15. this, SIGNAL(clicked(QString)));
    16.  
    17. setLayout(gridLayout);
    18. }
    To copy to clipboard, switch view to plain text mode 

    My question is: can I use the above approach with custom context menus, thus?
    Qt Code:
    1. class MyCustomMenu : QMenu {
    2. /* do as above except map (QAction*, actionText) through signalMapper */
    3. };
    To copy to clipboard, switch view to plain text mode 
    Would this work? If not this, what do most people do when you have a long list (say, more than 5-8) of context menu actions?

    Thanks for your insightful comments.


    Added after 1 40 minutes:


    As it happens, my Google search before my posting was not useful. It's funny how juxtaposing a few words in Google search fetches a different set of hits ... this link clearly illustrates what I want to do, that too with the function pointer idiom, which is better, except if you have overloading. Even with overloading it is not at all bad.

    Still I would like the opinion of Qt experts on the choice of idioms. Thanks.
    Last edited by NameRakes; 3rd January 2017 at 06:23.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSignalMapper, QMenu and custom context menu

    Quote Originally Posted by NameRakes View Post
    Would this work? If not this, what do most people do when you have a long list (say, more than 5-8) of context menu actions?
    Yes, basically the same code, just using the action's "triggered()" signal instead of a button's "clicked()" signal.

    Cheers,
    _

Similar Threads

  1. QMenu context menu on QTextEdit
    By johnmauer in forum Qt Programming
    Replies: 0
    Last Post: 10th January 2011, 22:48
  2. Custom QTreeWidgetItem context menu?
    By AaronMK in forum Qt Programming
    Replies: 4
    Last Post: 1st February 2010, 05:42
  3. Custom context menu in QTreeView
    By ttvo in forum Qt Programming
    Replies: 5
    Last Post: 3rd April 2009, 23:29
  4. Replies: 3
    Last Post: 5th March 2009, 08:27
  5. Q3TextEdit custom context menu
    By bcteh_98 in forum Qt Programming
    Replies: 1
    Last Post: 15th February 2006, 22:00

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.