Dynamic changed text of sub menu is cutting off
-
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.
-
This post is deleted!
-
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; }
-
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.