qtmenuqmenuqtoolbutton

I created a QToolButton and the label would include "..." but it looks like Qt removes them, is that right?


I use the following code, the first part, creating the QToolButton, is from Designer/moc, the rest I added.

// Moc part
QToolButton * f_tool_button = new QToolButton(bottom_grid);
f_tool_button->setObjectName(QStringLiteral("f_tool_button"));
f_tool_button->setText(QApplication::translate("MainWindow",
                                               "Tool Button...", 0));

// What I added
f_action = new QAction(this);
f_action->setObjectName(QStringLiteral("f_action"));
f_action->setText(QApplication::translate("MainWindow",
                                          "&Click...", 0));

f_menu.reset(new QMenu("Tool Button Menu ...", this));
f_menu->addAction(f_action);

f_tool_button->setDefaultAction(f_menu->menuAction());

If I don't call setDefaultAction(), the title appears as expected.

When I add the default action, the label seems to be replaced by the f_menu title, "Tool Button Menu ..." (I put a somewhat different label on purpose). But somehow the "..." gets removed from the name.

Any idea about this problem? Is that a special Qt feature?


Solution

  • The QToolButton displays text from QAction's iconText property (not text). text is meant to be used in menu entries, whereas iconText is meant to be displayed in tool bars. When not set, iconText is a stripped version of text.

    To override the default behavior of stipping text to generate iconText, you can set the desired iconText for your QAction using QAction::setIconText() (i.e. f_action->setIconText("Click..."); right after your f_action->setText call).