Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reversing the layout of a QToolButton

Tags:

qt

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.

like image 697
Ryan Patterson Avatar asked Nov 30 '25 16:11

Ryan Patterson


2 Answers

You can use QWidget::setLayoutDirection:

toolbar->setLayoutDirection(Qt::RightToLeft);
like image 174
alexisdm Avatar answered Dec 02 '25 05:12

alexisdm


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);
    }
};
like image 44
Anthony Avatar answered Dec 02 '25 07:12

Anthony