QT QPushButton带有图标和重叠的居中文本

时间:2011-04-20 19:12:37

标签: image qt button centering

我最近参加了Qt编程,并希望将我的应用程序移植到Qt。

我正在摆弄QPushButtons并且我设法在按钮中显示图像并将一些文本设置到按钮,但无论我做什么,无论有没有设计师, 我有同样的问题,文本与图标对齐,而不是覆盖在图标上。

addButton = new QPushButton();
addButton->setIcon(QIcon(":/AddButton.png"));
addButton->setText(tr("+"));
addButton->setFlat(true);
addButton->setIconSize(QSize(100,90));

我错过了什么?

我知道有toolButton但它似乎没有“flat”属性。

请问好吗?

非常感谢,

麦克

1 个答案:

答案 0 :(得分:7)

如果您尝试将图像用作背景图像,则可以使用样式表:

addButton->setStyleSheet("background-image: url(:/AddButton.png);"
                         "background-repeat: no-repeat;"
                         "background-position: center center");

您只需要确保按钮的大小至少与图像一样大。