Unable to increase size of QToolButton (with Button style set to Qt::ToolButtonTextBesideIcon)

877 Views Asked by At

I have a small qt application written in c++. I want the same application to run in small embedded device with a touchscreen but the size of few QToolButton was too small to be visible comfortably. I have tired to increase the size by modifying following function (adding setfont member call):

QToolButton* ColorToolBar::setupToolButton(QString name, QString iconPath, bool isCheckable)
{
    QToolButton *p_btn = new QToolButton(this);
    p_btn->setCheckable(isCheckable);
    p_btn->setIcon(QIcon(iconPath));
    p_btn->setIconSize(QSize(ICON_WIDTH, ICON_HEIGHT));
    p_btn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

    QFont font = p_btn->font();
    font.setPointSize(10);
    p_btn->setFont(font);

    if (!name.isEmpty())
        p_btn->setText(name);

    p_btn->setMinimumWidth(MINIMUM_WIDTH);
    p_btn->setMinimumHeight(MINIMUM_HEIGHT);
    return p_btn;
}

I am able to change the Icon size using setIconSize member function but setPointSize and setFont doesn't do anything. I want to have larger text and icon.

I have a class AdvancePlotToolBar which has all the QToolButton.My qss file looks like :

AdvancePlotToolBar QToolButton {
  border: none;
  padding-right: 10px;
  padding-bottom: 5px; 
}
AdvancePlotToolBar QToolButton:hover {
  border-bottom: 3px solid #52ce90;
  padding-bottom: 5px; 
}
AdvancePlotToolBar QToolButton:pressed {
  border-bottom: 3px solid #52ce90; 
}
AdvancePlotToolBar QToolButton:checked {
  border-bottom: 3px solid #52ce90;
  margin: 0px; 
}

EDIT:

The code below shows how I add QToolButton to the layout of AdvancePlotToolbar.

AdvancePlotToolBar::AdvancePlotToolBar(QWidget *parent) : QWidget(parent)
{
    transformationButton_ =  setupToolButton("Transformations", "://images//summation.png", true);

    QHBoxLayout *layout = new QHBoxLayout;

    layout->addWidget(transformationButton_);
    setLayout(layout);

}
1

There are 1 best solutions below

0
Maxim Paperno On

Setting a style sheet is overriding the font size you're setting in the C++ code. Try it w/out any style sheet applied (even to QApplication) and check. (Also How to set the font size of the label on pushbutton in Qt? )

I have the same issue with a completely custom QToolButton which overrides initStyleOption() and sets own font details in there (so it happens each time just before the button is painted, unrelated to setFont() at all). This works until an app-wide style-sheet is applied (even if it doesn't affect QToolButton directly! *), or a style sheet is applied specifically to the button instance or to its parent. I haven't tracked down where exactly this is happening (somewhere in QStyleSheetStyle I imagine), so I do not have a workaround yet **. In your case since you're using CSS for the tool button anyway, that would probably be best place to set the font size as well.

* The reason setting a style sheet may affect elements which are not styled specifically in the CSS rules is that the whole underlying QStyle is replaced with QStyleSheetStyle, and the original style (Fusion, WindowsVista, Macintosh, etc) becomes a proxy/fallback for QStyleSheetStyle. Or IOW all painting is filtered through the CSS style first. This includes setting style sheet on individual elements as well as globally on QApplication.

** I suspect this is a Qt bug.