Ryan Patterson
Ryan Patterson

Reputation: 78

Reversing the layout of a QToolButton

I would like to make a QToolButton where the text is the left of its icon. I've tried to find related information, and tried things with:

button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

but this sets the text to the right side of the button. I've looked into trying stylesheets, but text-alignment is only for push buttons. Is there any way to do this?

My highest-level goal is to make an interface bar which looks like this:

goal

with a text label and image right beside it. Currently I'm using a toolbar with checkable toolbuttons because of the style (no border unless moused-over, still has the indent with checkable, contains text and an icon...). It's entirely possible that I'm using the wrong type widgets, so if I can't change the layout of this, is there any way to emulate this style?

This is what I currently have:

current.

Upvotes: 3

Views: 2195

Answers (2)

alexisdm
alexisdm

Reputation: 29896

You can use QWidget::setLayoutDirection:

toolbar->setLayoutDirection(Qt::RightToLeft);

Upvotes: 4

Anthony
Anthony

Reputation: 8788

Your best bet is probably to create this functionality yourself in a QWidget. It should have a QLabel and a QToolButton (or QPushButton) arranged in a QHBoxLayout. Then you can add this custom widget to the toolbar using QToolBar::addWidget() or QToolBar::insertWidget().

Here's an example of a widget you could use:

#include <QtGui>

class CoolButton : public QWidget
{
public:
    CoolButton(QWidget *parent = 0) : QWidget(parent)
    {
        QLabel *label = new QLabel("Hi!", this);
        QToolButton *button = new QToolButton(this);
        QHBoxLayout *layout = new QHBoxLayout;
        layout->addWidget(label);
        layout->addWidget(button);
        setLayout(layout);
    }
};

Upvotes: 1

Related Questions