-8

I would like to create QPushButton with QIcon left align (not to the center as default) and text center align. I don't want to use a style sheet. I know that might be possibly using QPainter but I wasn't able to do it. I had little clue and tried this code:

void MyPushButton::paintEvent(QPaintEvent *)
{
    QStylePainter painter(this);
    QStyleOptionButton opt;
    initStyleOption(&opt);
    painter.drawItemPixmap(opt.rect, Qt::AlignLeft, opt.icon);
    painter.drawItemText(opt.rect, Qt::AlignCenter, palette(), 1, opt.text);
    painter.drawPrimitive(QStyle::PE_PanelButtonCommand, opt);
}

which produces this error message

no matching function for call to 'QStylePainter::drawItemPixmap(QRect&, Qt::AlignmentFlag, QIcon&)' painter.drawItemPixmap(opt.rect, Qt::AlignCenter, opt.icon);

What's wrong with code above?

MSDN.WhiteKnight
  • 664
  • 7
  • 30
mumulala
  • 1
  • 5
  • 1
    We also need to know what actually happened when you ran the code and what you didn't like about it. – Nicolas Holthaus Apr 28 '16 at 15:10
  • this error code error: no matching function for call to 'QStylePainter::drawItemPixmap(QRect&, Qt::AlignmentFlag, QIcon&)' painter.drawItemPixmap(opt.rect, Qt::AlignCenter, opt.icon); – mumulala Apr 28 '16 at 15:14
  • 1
    @mumulala The answer belongs in an edit to the question, not in a comment :) – Kuba hasn't forgotten Monica Apr 28 '16 at 15:44
  • 1
    There isn't enough code shown to know what's wrong. For all we know you've set up your widget incorrectly with regards to size management, etc. This question is unanswerable without a complete code that compiles, runs, and demonstrates the problem. – Kuba hasn't forgotten Monica Apr 28 '16 at 15:45
  • 8
    Once a question has an answer you can't radically change it such that it invalidates that answer. If you need to, ask another question. – Robert Longson Apr 28 '16 at 21:16
  • @rodolk: [Your review](http://stackoverflow.com/review/suggested-edits/12182748) is being [discussed on meta](http://meta.stackoverflow.com/questions/322010/why-was-my-edit-rejected-when-attempting-to-revert-a-question) – Pokechu22 Apr 30 '16 at 00:39
  • @wogsland: [Your review](http://stackoverflow.com/review/suggested-edits/12182748) is being [discussed on meta](http://meta.stackoverflow.com/questions/322010/why-was-my-edit-rejected-when-attempting-to-revert-a-question) – Pokechu22 Apr 30 '16 at 00:39
  • @River: [Your review](http://stackoverflow.com/review/suggested-edits/12182748) is being [discussed on meta](http://meta.stackoverflow.com/questions/322010/why-was-my-edit-rejected-when-attempting-to-revert-a-question) – Pokechu22 Apr 30 '16 at 00:39

1 Answers1

6

you're getting

this error code error: no matching function for call to 'QStylePainter::drawItemPixmap(QRect&, Qt::AlignmentFlag, QIcon&)' painter.drawItemPixmap(opt.rect, Qt::AlignCenter, opt.icon);

because drawItemPixmap draws... a pixmap. Not an icon. So all you need to do is get the icons pixmap using the pixmap() accessor.

change

painter.drawItemPixmap(opt.rect, Qt::AlignLeft, opt.icon);

to

// or whaever size you want
painter.drawItemPixmap(opt.rect, Qt::AlignLeft, opt.icon.pixmap(QSize(16,16))); 
Nicolas Holthaus
  • 7,763
  • 4
  • 42
  • 97
  • thank you @Nicholas for respons, i changed the code and it didn't produce any error but icon and text are invisible. – mumulala Apr 28 '16 at 15:39
  • @mumulala it's like Kuba said, without specific code, images of the output, and error messages, we can't replicate the problem, and there's not a lot we can do. – Nicolas Holthaus Apr 28 '16 at 16:57
  • if i remove this line painter.drawPrimitive(QStyle::PE_PanelButtonCommand, opt); QIcon is on the left and text is on the center but no button behaviour. – mumulala Apr 28 '16 at 17:27
  • @mumulala to get the button behavior, you'd have to paint each permutation of `opt.state` and all the button features individually. Honestly you are _way_ better off if you just use stylesheets. It's much simpler, much more flexible, and much less code to write. – Nicolas Holthaus Apr 28 '16 at 18:10