[英]Construct QPushButton with QIcon alignleft and text center align
I would like to create QPushButton
with QIcon
left align (not to the center as default) and text center align.我想创建
QPushButton
与QIcon
左对齐(默认不居中)和文本居中对齐。 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.我知道这可能是使用
QPainter
但我无法做到。 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);
没有匹配的函数调用'QStylePainter::drawItemPixmap(QRect&, Qt::AlignmentFlag, QIcon&)'painter.drawItemPixmap(opt.rect, Qt::AlignCenter, opt.icon);
What's wrong with code above?上面的代码有什么问题?
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);
此错误代码错误:没有匹配的函数调用'QStylePainter::drawItemPixmap(QRect&, Qt::AlignmentFlag, QIcon&)'painter.drawItemPixmap(opt.rect, Qt::AlignCenter, opt.icon);
because drawItemPixmap
draws... a pixmap.因为
drawItemPixmap
绘制......一个像素图。 Not an icon.不是图标。 So all you need to do is get the icons pixmap using the
pixmap()
accessor.所以你需要做的就是使用
pixmap()
访问器获取图标像素pixmap()
。
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)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.