Results 1 to 8 of 8

Thread: QWidgetAction resize in QMenu

  1. #1
    Join Date
    May 2008
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default QWidgetAction resize in QMenu

    I have a QButton with an associated QMenu. The QMenu contains a single QWidgetAction with an associated widget.

    Now I want to change the content of the widget contained in the menu at some point, and have the QMenu resize itself when shown so that the QMenu is the appropriate size for the content in the widget.
    For instance:
    Qt Code:
    1. QPushButton* button = new QPushButton("Button",parent);
    2. QMenu* menu = new QMenu(button);
    3. button->setMenu(menu);
    4. MyWidget* customWidget = new MyWidget(menu);
    5. QWidgetAction* menuWidget = new QWidgetAction(menu);
    6. menuWidget->setDefaultWidget(customWidget);
    7. menu->addAction(menuWidget);
    8. //Attach a signal that would allow the customWidget to update its contents prior to being shown
    9. QObject::connect(menu,SIGNAL(aboutToShow()),customWidget,SLOT(updateContent()));
    10. //The 'updateContent' slot may add or remove child widgets to 'customWidget' causing its size to change
    To copy to clipboard, switch view to plain text mode 
    What I want to happen is that each time the menu is about to be shown the widget will update its content and the QMenu will be shown with the appropriate size to display the content of the widget.

    What currently happens is that the QMenu takes its size from whatever the initial size of the customWidget object is the first time it is shown. Updating the content of the customWidget object does not have any effect on the size of the containing menu on later showings of the menu. That is, the QMenu will always be the same size as the first time it was shown regardless of what modifications I make to the customWidget object.

    This was working at one point and I can't for the life of me figure out what has changed.

  2. #2
    Join Date
    Feb 2007
    Location
    Italy
    Posts
    69
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QWidgetAction resize in QMenu

    I know this is an old post, but I've this same problem... Can anyone help, please?

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QWidgetAction resize in QMenu

    I don't think it is directly possible with in a slot connected to QMenu's aboutToShow() signal.

    One workaroud is to show(), hide(), and then show() to that will trigger a resize of QMenu(), somthing like
    Qt Code:
    1. void updateContent()
    2. {
    3. QMenu * menu = dynamic_cast<QMenu *>(sender());
    4. if(menu != 0)
    5. {
    6. menu->blockSignals(true);
    7. // Add/update widgets
    8.  
    9. menu->show();
    10. qApp->processEvents();
    11. menu->hide();
    12. qApp->processEvents();
    13. menu->show();
    14. menu->blockSignals(false);
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    This may cause a small noise / glich in ui during the menu popup, due to quick show(), hide() and show()
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  4. #4
    Join Date
    Jun 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QWidgetAction resize in QMenu

    Hello,

    instead of menu->hide()/show(), I found the workaround
    QPixmap::grabWidget(menu)
    So it doesn't flicker ;-)

  5. #5
    Join Date
    Mar 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QWidgetAction resize in QMenu

    Hi,

    Is there any news about this issue? I met the same problem.

  6. #6
    Join Date
    Feb 2017
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QWidgetAction resize in QMenu

    This is quite an old thread, but the problem remains … I posted a question about this at http://stackoverflow.com/questions/42122985/ recently. My workaround was to clear the menu and re-fill it with the respective actions after changing the content of the QWidgetAction.

    Is there any non-workaround solution for this?

  7. #7
    Join Date
    Dec 2017
    Posts
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QWidgetAction resize in QMenu

    The solution is to send a resize event instead:

    Qt Code:
    1. QResizeEvent re(newsize, menu->size());
    2. qApp->sendEvent(menu, &re);
    To copy to clipboard, switch view to plain text mode 

    This will set the QMenu's internal itemsDirty flag and will force geometry recalculation when the menu is next shown.
    Last edited by mnaydenov; 9th December 2017 at 13:29.

  8. #8
    Join Date
    Jan 2018
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QWidgetAction resize in QMenu

    Quote Originally Posted by mnaydenov View Post
    The solution is to send a resize event instead:

    Qt Code:
    1. QResizeEvent re(newsize, menu->size());
    2. qApp->sendEvent(menu, &re);
    To copy to clipboard, switch view to plain text mode 

    This will set the QMenu's internal itemsDirty flag and will force geometry recalculation when the menu is next shown.
    I have exactly the same problem with a QWidgetAction and a popup menu, and tried your solution.
    I send the resize event in the aboutToShow signal of my menu, but the menu is correctly sized only the second time it is shown (I have to popup the mnu, close it and popup it again).
    Am I missing something ?

Similar Threads

  1. QWidgetAction: A QTreeWidget in a QMenu
    By chezifresh in forum Qt Programming
    Replies: 1
    Last Post: 16th October 2009, 04:49
  2. QMenu popup: how close when clicked outside
    By powermax in forum Qt Programming
    Replies: 5
    Last Post: 4th March 2009, 04:18
  3. Replies: 2
    Last Post: 22nd January 2008, 17:10
  4. Custom Shape Widget (resize)
    By PiXeL16 in forum Qt Programming
    Replies: 7
    Last Post: 12th February 2007, 08:00
  5. postponing resize event
    By Honestmath in forum Qt Programming
    Replies: 11
    Last Post: 26th February 2006, 01:32

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.