Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Dynamic changed text of sub menu is cutting off
QtWS25 Last Chance

Dynamic changed text of sub menu is cutting off

Scheduled Pinned Locked Moved Unsolved General and Desktop
qwidgetqmenubarqmenuqwidgetactionqlabel
13 Posts 4 Posters 5.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Zee_07Z Offline
    Zee_07Z Offline
    Zee_07
    wrote on last edited by
    #1

    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.

    Thanks and Kind Regards,
    Zee_07

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jan-Willem
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • Zee_07Z Offline
        Zee_07Z Offline
        Zee_07
        wrote on last edited by
        #3

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

        Thanks and Kind Regards,
        Zee_07

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jan-Willem
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • Zee_07Z Offline
            Zee_07Z Offline
            Zee_07
            wrote on last edited by
            #5

            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.

            Thanks and Kind Regards,
            Zee_07

            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jan-Willem
              wrote on last edited by Jan-Willem
              #6

              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.

              1 Reply Last reply
              0
              • Zee_07Z Offline
                Zee_07Z Offline
                Zee_07
                wrote on last edited by
                #7

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

                Thanks and Kind Regards,
                Zee_07

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jan-Willem
                  wrote on last edited by Jan-Willem
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • Zee_07Z Offline
                    Zee_07Z Offline
                    Zee_07
                    wrote on last edited by
                    #9

                    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.

                    Thanks and Kind Regards,
                    Zee_07

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      Jan-Willem
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        Jan-Willem
                        wrote on last edited by
                        #11

                        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;
                        }
                        
                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          Allactaga
                          wrote on last edited by Allactaga
                          #12

                          I had some similar problem. My QMenu wasn't adjusting to a dynamically changed size of QWidgetAction. I have found this thread http://www.qtcentre.org/threads/26291-QWidgetAction-resize-in-QMenu and suggested workaround worked for me. Qt 5.9.5.

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

                          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.

                          1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            TimB
                            wrote on last edited by
                            #13

                            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.

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved