0

When having a long text applied to a QToolbutton it overlaps the menu button for the right click menu like the following:

enter image description here

The code for the Button

TestWindow::TestWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::TestWindow)
{
    ui->setupUi(this);

    QMenu *menu = new QMenu();
    QAction *testAction = new QAction(QLatin1String("testAction"), this);
    menu->addAction(testAction);

    ui->toolButton->setMenu(menu);
    ui->toolButton->setText(QLatin1String("This is a long Text that should not overlap"));
    ui->toolButton->setPopupMode(QToolButton::InstantPopup);
}

The toolbutton is just part of a horizontal layout of the central widget.

Is there an easy way to prevent this by e.g. cropping the text?

easysaesch
  • 159
  • 1
  • 14
  • Since it's in a layout, it really shouldn't overlap, unless you (accidentally?) set a maximum width for it. – p-a-o-l-o Oct 18 '19 at 10:32
  • You created a menu, but didn't add it to the menu bar. I don't even see a menu on the screenshot. So what kind of overlapping is that? – vahancho Oct 18 '19 at 12:51
  • @vahancho I added the menu to the toolbutton, so the small arrow menu button appears next to the toolbutton label. You see it in the screenshot overlapping the last character 'p' of the String. So that is the problem. – easysaesch Oct 18 '19 at 14:02
  • My workaround was now, to adjust the rect for the toolbutton label in the application style – easysaesch Oct 18 '19 at 14:03
  • I would solve this by setting some padding: `ui->toolButton->setStyleSheet("padding: .5em");` – VVV Oct 18 '19 at 14:39
  • 1
    By default `QToolButton`s try to minimize used space. The final result will also vary by the style being used, and in some cases may be dictated somewhat by the OS settings. CSS should work, as suggested, but another alternative is to use `QPushButton` which doesn't have this issue to begin with (I just tried your code, replacing the tool button with `QPushButton` and confirmed it). They're going to be better for long text anyway, even w/out a menu, basically because they typically have more padding by default. `QToolButton`s are good for icons with maybe some short text like "Edit". – Maxim Paperno Oct 19 '19 at 02:36

0 Answers0