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.

    testapp: https://www.dropbox.com/s/mnr22bgss80d4pr/QMenuTesting.zip?dl=0

    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.
    QTimer::singleShot(6000,this,SLOT(textChange()));
    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.

    menuitem.h

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

    menuitem.cpp

    MenuItem::MenuItem(QWidget *parent) : QWidget(parent)
    {
        QHBoxLayout *hlayout = new QHBoxLayout(this);
    
        m_iconLabel = new QLabel(this);
        m_iconLabel->setObjectName("ActionIconLabel");
        m_iconLabel->setFixedSize(15,15);
        hlayout->addWidget(m_iconLabel);
    
        m_textLabel = new QLabel("Menu",this);
        hlayout->addWidget(m_textLabel);
        hlayout->setContentsMargins(6,7,0,2);
    
        setLayout(hlayout);
        //QTimer::singleShot(3000,this,SLOT(textChange()));
    }
    
    void MenuItem::textChange()
    {
        qDebug()<<"Menu text changed";
        m_textLabel->setText("Changed Menu Text");
    }
    

    mainwindow.cpp

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        m_menuItem = new MenuItem(this);
    
        QWidgetAction *listAction = new QWidgetAction(this);
        listAction->setDefaultWidget(m_menuItem);
    
        QMenu *menu = new QMenu("main Menu",this);
    
        ui->m_menuBar->addMenu(menu);
    
        menu->addAction(listAction);
    
        m_menuItem->textChange();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    


Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.