【发布时间】:2016-08-23 10:44:54
【问题描述】:
我想创建QPushButton 与QIcon 左对齐(默认不居中)和文本居中对齐。我不想使用样式表。我知道这可能会使用QPainter,但我无法做到。我几乎没有任何线索并尝试了这段代码:
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);
}
产生此错误消息
没有匹配函数调用'QStylePainter::drawItemPixmap(QRect&, Qt::AlignmentFlag, QIcon&)'painter.drawItemPixmap(opt.rect, Qt::AlignCenter, opt.icon);
上面的代码有什么问题?
【问题讨论】:
-
我们还需要知道您在运行代码时实际发生了什么以及您不喜欢它的哪些地方。
-
this error code error: no matching function for call to 'QStylePainter::drawItemPixmap(QRect&, Qt::AlignmentFlag, QIcon&)'painter.drawItemPixmap(opt.rect, Qt::AlignCenter, opt.图标);
-
@mumulala 答案属于对问题的编辑,而不是评论:)
-
显示的代码不足,无法了解问题所在。据我们所知,您在尺寸管理等方面错误地设置了您的小部件。如果没有完整的代码来编译、运行和演示问题,这个问题是无法回答的。
-
一旦一个问题有了答案,你就不能从根本上改变它,以至于它会使那个答案无效。如果需要,请提出另一个问题。
标签: c++ qt qpainter qpushbutton