how to align text (alone) of a qtoolbutton

  • Last Update :
  • Techknowledgy :

You can try to sub-class QStyle and re-implement QStyle::drawControl() to align the text to the right. Check the file qt/src/gui/styles/qcommonstyle.cpp to see how it's done. (Sorry I'm using C++ not Python)

case CE_ToolButtonLabel:
    if (const QStyleOptionToolButton *toolbutton
            = qstyleoption_cast<const QStyleOptionToolButton *>(opt)) {
        QRect rect = toolbutton->rect;
        int shiftX = 0;
        int shiftY = 0;
        if (toolbutton->state & (State_Sunken | State_On)) {
            shiftX = proxy()->pixelMetric(PM_ButtonShiftHorizontal, toolbutton, widget);
            shiftY = proxy()->pixelMetric(PM_ButtonShiftVertical, toolbutton, widget);
        }
        // Arrow type always overrules and is always shown
        bool hasArrow = toolbutton->features & QStyleOptionToolButton::Arrow;
        if (((!hasArrow && toolbutton->icon.isNull()) && !toolbutton->text.isEmpty())
            || toolbutton->toolButtonStyle == Qt::ToolButtonTextOnly) {
            int alignment = Qt::AlignCenter | Qt::TextShowMnemonic;

This example of align button content (icon & text) to center, but you can adopt this example to your demands (align to right). Override QToolButoon::paintEvent as next:

void CMyToolButton::paintEvent(QPaintEvent * ) {
   QStylePainter sp(this);
   QStyleOptionToolButton opt;
   initStyleOption( & opt);
   const QString strText = opt.text;
   const QIcon icn = opt.icon;
   //draw background
   opt.text.clear();
   opt.icon = QIcon();
   sp.drawComplexControl(QStyle::CC_ToolButton, opt);
   //draw content
   const int nSizeHintWidth = minimumSizeHint().width();
   const int nDiff = qMax(0, (opt.rect.width() - nSizeHintWidth) / 2);
   opt.text = strText;
   opt.icon = icn;
   opt.rect.setWidth(nSizeHintWidth); //reduce paint area to minimum
   opt.rect.translate(nDiff, 0); //offset paint area to center
   sp.drawComplexControl(QStyle::CC_ToolButton, opt);
}

Suggestion : 2

You can try to sub-class QStyle and re-implement QStyle::drawControl() to align the text to the right. Check the file qt/src/gui/styles/qcommonstyle.cpp to see how it's done. (Sorry I'm using C++ not Python),This example of align button content (icon & text) to center, but you can adopt this example to your demands (align to right). Override QToolButoon::paintEvent as next:,Boost.Log - how to configure a text sink backend to append to rotated files,How to access directly and efficiently on very large text file?

You can try to sub-class QStyle and re-implement QStyle::drawControl() to align the text to the right. Check the file qt/src/gui/styles/qcommonstyle.cpp to see how it's done. (Sorry I'm using C++ not Python)

case CE_ToolButtonLabel:
    if (const QStyleOptionToolButton *toolbutton
            = qstyleoption_cast<const QStyleOptionToolButton *>(opt)) {
        QRect rect = toolbutton->rect;
        int shiftX = 0;
        int shiftY = 0;
        if (toolbutton->state & (State_Sunken | State_On)) {
            shiftX = proxy()->pixelMetric(PM_ButtonShiftHorizontal, toolbutton, widget);
            shiftY = proxy()->pixelMetric(PM_ButtonShiftVertical, toolbutton, widget);
        }
        // Arrow type always overrules and is always shown
        bool hasArrow = toolbutton->features & QStyleOptionToolButton::Arrow;
        if (((!hasArrow && toolbutton->icon.isNull()) && !toolbutton->text.isEmpty())
            || toolbutton->toolButtonStyle == Qt::ToolButtonTextOnly) {
            int alignment = Qt::AlignCenter | Qt::TextShowMnemonic;

This example of align button content (icon & text) to center, but you can adopt this example to your demands (align to right). Override QToolButoon::paintEvent as next:

void CMyToolButton::paintEvent(QPaintEvent * ) {
   QStylePainter sp(this);
   QStyleOptionToolButton opt;
   initStyleOption( & opt);
   const QString strText = opt.text;
   const QIcon icn = opt.icon;
   //draw background
   opt.text.clear();
   opt.icon = QIcon();
   sp.drawComplexControl(QStyle::CC_ToolButton, opt);
   //draw content
   const int nSizeHintWidth = minimumSizeHint().width();
   const int nDiff = qMax(0, (opt.rect.width() - nSizeHintWidth) / 2);
   opt.text = strText;
   opt.icon = icn;
   opt.rect.setWidth(nSizeHintWidth); //reduce paint area to minimum
   opt.rect.translate(nDiff, 0); //offset paint area to center
   sp.drawComplexControl(QStyle::CC_ToolButton, opt);
}

Suggestion : 3

2022-02-27 , 2022-04-27 , 2022-03-19 , 2022-05-15

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566