0

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);

}
abhiarora
  • 9,743
  • 5
  • 32
  • 57
  • As the widget-size (possibly) depends on the way you insert the widget into your layout, you should post the relevant code that does that. Currently we see only the initialization code of the button, but the layout code is missing. – markus-nm Aug 26 '19 at 13:46
  • Updated my question with required code. – abhiarora Aug 26 '19 at 14:49
  • Could you post a minimal example reproducing this issue? Not being cheeky... :) just investigating. There is definitely some inconsistent behavior surrounding fonts and what inherits from what and when, and I've ID'd at least one Qt bug so far with `Qt::AA_UseStyleSheetPropagationInWidgetStyles` flag even when not using CSS at all. And having `QStyleSheetStyle` involved makes "unexpected" differences. But... **I can't actually reproduce what you describe** when setting font explicitly on the `QToolButton`s. I think you may be setting a font somewhere else as well? I'd love to repro it. TIA. – Maxim Paperno Sep 18 '19 at 10:07

1 Answers1

1

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.

Maxim Paperno
  • 4,485
  • 2
  • 18
  • 22