1

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?

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
  • Unrelated to your issue, but are you directy editing the h-file generated by moc? Since those are automatically generated all changes will be lost the next time you run moc. – Bowdzone Jun 22 '18 at 08:58
  • No, I'm adding my code in my MainWindow constructor implementation. It's to show all the pertinent code in one place. – Alexis Wilke Jun 22 '18 at 09:28

1 Answers1

3

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).

Mike
  • 8,055
  • 1
  • 30
  • 44
  • The problem I have is in the `f_menu`, I guess I wasn't clear in my question which one has missing the `...`. The `QAction` text works as expected. – Alexis Wilke Jun 22 '18 at 11:27
  • However, doing a `f_tool_button->setText("Click...");` just after the `setDefaultAction()` works! – Alexis Wilke Jun 22 '18 at 11:29
  • @AlexisWilke, I think that you just meant to write `f_tool_button->setDefaultAction(f_menu->actions().at(0));` instead of the last line in your code snippet. `menuAction` is the action for the whole menu, and you seem to be looking for the `action` that you have just added using `addAction`... – Mike Jun 22 '18 at 12:27
  • No I really want the extra little button to the right with the drop down extra selection. That gives me two options on the same button. – Alexis Wilke Jun 23 '18 at 08:57
  • 1
    @AlexisWilke, Oh, I misunderstood you because I don't usually add `QMenu`s to `QToolBar`s the way you are doing it. I usually do something like [this](https://stackoverflow.com/a/5365184/2666212). Anyway, If your concern is the 3 dots at the end of "Tool Button Menu...", You just need to set `iconText` (as indicated in my answer) on your `menuAction()` before setting it as the default action for the `QToolButton` (i.e. `f_menu->menuAction()->setIconText(f_menu->menuAction()->text());`). Hope this works for you :) – Mike Jun 23 '18 at 10:08
  • Yes! That works! And it's certainly cleaner than my previous `setText()` after the `setDefaultAction()`. – Alexis Wilke Jun 23 '18 at 21:38