No icon with QStyleOptionButton
Asked Answered
F

1

6

I have got a weird problem.

I need to have some buttons in my QTableView. I used to use QAbstractItemView::setIndexWidget() method, but it is not very responsible when working with larger models. Therefore I decided to switch to QStyledItemDelegate. My buttons have icons (and icons only, no text). When working with setIndexWidget, I used the following code:

ClientDetailsButton::ClientDetailsButton(const Client* _client,
                                         QWidget* _parent) :
    QPushButton("", _parent),
    __current(_client) {

  setIcon(QIcon(":/uiIcons/button-details.png"));
}

And it worked perfectly. But when I switch to delegate, I use it like that:

QStyleOptionButton button;
button.rect = _option.rect;
button.text.clear();
button.icon = QIcon(":/uiIcons/button-details.png");
button.state = _option.state | QStyle::State_Enabled;

if (_index == __button)
  button.state |= QStyle::State_Sunken;

QApplication::style()->drawControl(QStyle::CE_PushButton, &button, _painter);

The button itself is fine, but its empty. There is no icon visible. Suprisingly, when I use, for example:

button.icon = QIcon::fromTheme("dialog-information", QIcon(":/uiIcons/button-details.png"));

the theme icon is visible. But if Qt cannot find the theme icon, the replacement is still blank. I tried everything I could think of and have no idea why it doesn't work. Anyone has any ideas?

Friction answered 2/3, 2014 at 15:49 Comment(9)
Why you can't draw an icon directly?Incisive
Its soo complicated and I can never be sure if it looks OK on every machine...Friction
What you mean by "complicated"? Making call painter->drawPixmap instead of all your code?Incisive
No, I want button there. By complicated I mean making the pixmap looks like a standard image on button.Friction
Draw button - then draw image on it. Still complex? :)Incisive
Yes, its not that simple. Whats more, its only workaround, not a solution.Friction
Using of delegates is not a workaround.Incisive
Rendering an icon separately is not a solution.Friction
It's your opinion. If you want to talk instead of using Qt-ways - you should wait for other answers.Incisive
B
9

I solved this problem by setting button.iconSize=QSize(16,16); as the default iconsize is (-1,-1) so the icon is invisible.

Bohlin answered 6/1, 2015 at 4:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.