Dynamic changed text of sub menu is cutting off
-
wrote on 22 Aug 2016, 06:43 last edited by
What happens if you also would adjust the size of the QLabel (just for testing purposes)?
-
wrote on 22 Aug 2016, 07:40 last edited by
QLabel size is growing dynamically after changing to larger text automatically because of layout but size of sub menu is still same as initial.
-
wrote on 22 Aug 2016, 08:06 last edited by
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. -
wrote on 22 Aug 2016, 08:32 last edited by
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. -
wrote on 22 Aug 2016, 08:45 last edited by Jan-Willem
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.
-
wrote on 22 Aug 2016, 08:50 last edited by
OK, I verified it through QPushButton too and problem is still with this approach.
-
wrote on 22 Aug 2016, 08:58 last edited by Jan-WillemThis post is deleted!
-
wrote on 22 Aug 2016, 09:12 last edited by
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.
-
wrote on 22 Aug 2016, 09:21 last edited by
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. -
wrote on 22 Aug 2016, 17:02 last edited by
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; }
-
wrote on 14 May 2018, 10:24 last edited by Allactaga
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. -
wrote on 30 Jun 2020, 09:17 last edited by
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.