Important: Please read the Qt Code of Conduct -

Dynamic changed text of sub menu is cutting off

  • I have a QMenuBar with QMenu "Main Menu" which has sub menu as a QWidgetAction which consist of Qlabel with text "Menu". First time it displayed perfect but if we change the text dynamically lets say "Changed Menu Text" then text is cutting off. It looks like sub menu resizing is not happening dynamically.


    Text change of sub menu happen after 3000 ms by calling slot so before that open the sub menu "Menu" and its looks fine but after text change sub menu not resizes and text not displayed properly.

  • What happens if you also would adjust the size of the QLabel (just for testing purposes)?

  • QLabel size is growing dynamically after changing to larger text automatically because of layout but size of sub menu is still same as initial.

  • Yes, I know. But as you correctly mentioned, that has no effect on the size of the submenu.

    Looking at your code I suspect the problem might be your changing of the text in the constructor of your MenuItem.
    Perhaps you could try to create a button in your application and connect this to MenuItem::textChange() and see if that works.

  • I am setting a small text for sub menu in constructor as "Menu" and changing of sub-menu text is happening in textChange slot which is called through single-shot timer.
    So its the same case whether I change the text of sub-menu through timer or by QPushbutton click.

  • I'm afraid not. You are still doing it in the constructor which might be the problem. My suggestion, aside from a button or a QTimer::singleShot, is taking it out of the constructor. So it is not the same case whether you change the text of the submenu through a timer in the constructor or by QPushbutton click after the constructor is finished.

  • OK, I verified it through QPushButton too and problem is still with this approach.

  • This post is deleted!

  • Problem is not with this m_iconLabel QLabel (I want this QLabel of fixed size). Text change is done on m_textLabel QLabel which is not set as fixed size.

  • Okay, silly me. Your code is quit clear on that.
    I will run your program when I'm home and see if I can find the problem.
    I will let you know this evening.

  • Hi,

    I just tried your code an made an adjustment.
    The idea with the button didn't work, this seems to work.

    I'm not sure why this works and with the button it doesn't.
    But part of the solution seems to be indeed the constructor part.


    class MenuItem : public QWidget
        explicit MenuItem(QWidget *parent = 0);
        QLabel *m_iconLabel;
        QLabel *m_textLabel;
    public slots:
    //private slots:
        void textChange();


    MenuItem::MenuItem(QWidget *parent) : QWidget(parent)
        QHBoxLayout *hlayout = new QHBoxLayout(this);
        m_iconLabel = new QLabel(this);
        m_textLabel = new QLabel("Menu",this);
    void MenuItem::textChange()
        qDebug()<<"Menu text changed";
        m_textLabel->setText("Changed Menu Text");


    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
        m_menuItem = new MenuItem(this);
        QWidgetAction *listAction = new QWidgetAction(this);
        QMenu *menu = new QMenu("main Menu",this);
        delete ui;

  • I had some similar problem. My QMenu wasn't adjusting to a dynamically changed size of QWidgetAction. I have found this thread and suggested workaround worked for me. Qt 5.9.5.

    QResizeEvent re (QSize(1, 1), size());
    qApp->sendEvent(this, &re);

    Where this is a pointer to QMenu object.
    Fake resize event forces all menu items size recalculating and adjustSize() recalculates it.
    I hope it will help to anyone else.

  • I helped myself to set a minimum size ->setMinimumSize(300)
    I also had to problem that at first pop up, my text was cut off. Nothing of the workarrounds mentioned here helped.