Results 1 to 4 of 4

Thread: QMenu clear items

  1. #1
    Join Date
    Jan 2007
    Posts
    92
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default QMenu clear items

    I am not able to clean action items from the QMenu. It is right click context menu. Every item when I right click it will go through following code. I want to delete any existing items on the menu before I add actions again.
    I tried to call clear() on menu. It does not work.

    Qt Code:
    1. QString strAg = "ITEM %1";
    2. QMenu * pMenu = new QMenu;
    3.  
    4. if(pMenu)
    5. {
    6. pMenu->clear();
    7. disconnect(pMenu,0,0,0);
    8.  
    9. for ( int item = 0; item < m_strItemList.count(); ++item )
    10. {
    11. QAction * pAction = new QAction(pMenu);
    12. pAction = pMenu->addAction(strArg.arg(m_strItemList[item] ));
    13.  
    14. QSignalMapper* mapper = new QSignalMapper(pMenu);
    15. mapper->setMapping(pAction, 1)
    16. connect(pAction, SIGNAL(triggered()), mapper, SLOT(map()));
    17. connect(mapper, SIGNAL(mapped(int)), this, SLOT(RunItem(int)));
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

    Following is Implemnation of Clear() in QMenu.cpp class in Qt Source. I tried to step into it and it never goes through forloop because actions list is empty.
    Qt Code:
    1. void QMenu::clear()
    2. {
    3. QList<QAction*> acts = actions();
    4.  
    5. for(int i = 0; i < acts.size(); i++) {
    6. #ifdef QT_SOFTKEYS_ENABLED
    7. Q_D(QMenu);
    8. // Lets not touch to our internal softkey actions
    9. if(acts[i] == d->selectAction || acts[i] == d->cancelAction)
    10. continue;
    11. #endif
    12. removeAction(acts[i]);
    13. if (acts[i]->parent() == this && acts[i]->d_func()->widgets.isEmpty())
    14. delete acts[i];
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    Thanks.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QMenu clear items

    I guess the list is empty then.

    why do this,
    QAction * pAction = new QAction(pMenu);
    then this,
    pAction = pMenu->addAction(strArg.arg(m_strItemList[item] ));

    ?
    The first pAction is not used for anything...

    Do you have some code that shows the menu not clearing? If so, please post (and see my sig).
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Jan 2007
    Posts
    92
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default Re: QMenu clear items

    I have posted code above. Where I am calling pMenu->clear();

    Qt Code:
    1. QString strAg = "ITEM %1";
    2. QMenu * pMenu = new QMenu;
    3.  
    4. if(pMenu)
    5. {
    6. pMenu->clear(); //CLEAR DOES NOT WORK
    7. disconnect(pMenu,0,0,0);
    8.  
    9. for ( int item = 0; item < m_strItemList.count(); ++item )
    10. {
    11.  
    12. QAcrion * pAction = pMenu->addAction(strArg.arg(m_strItemList[item] )); //Adding Action to the Menu
    13.  
    14. QSignalMapper* mapper = new QSignalMapper(pMenu);
    15. mapper->setMapping(pAction, 1)
    16. connect(pAction, SIGNAL(triggered()), mapper, SLOT(map()));
    17. connect(mapper, SIGNAL(mapped(int)), this, SLOT(RunItem(int)));
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,311
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMenu clear items

    In line 2 you create a new QMenu. It's EMPTY. Why do you think calling clear() on an empty menu will do anything? Of course the for() loop in QMenu::clear() is never entered. The menu is empty - there is nothing in it to clear!

Similar Threads

  1. Replies: 5
    Last Post: 7th September 2011, 12:59
  2. Replies: 2
    Last Post: 20th August 2010, 05:18
  3. Does clear()/clearContents() delete items?
    By vycke in forum Qt Programming
    Replies: 1
    Last Post: 31st July 2008, 17:49
  4. Replies: 1
    Last Post: 14th May 2008, 19:35
  5. How to clear All items on QTreeView ?
    By qintm in forum Qt Programming
    Replies: 1
    Last Post: 26th March 2006, 11:39

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
  •  
Qt is a trademark of The Qt Company.