【问题标题】:Rounded icon on QpushbuttonQpushbutton 上的圆形图标
【发布时间】:2016-04-13 11:34:48
【问题描述】:

我有一个QPushButton,我想在其中添加一个带圆角的图标(我使用QPushButton::setIcon() 添加到按钮中)。但是我有一个像素图,它是一个方形图像。是否可以调整像素图使其变圆?

我在QPixmap 上找到了setMask() 函数,我也许可以使用它。但是如何制作一个位图来掩盖我的QPixmap 的边缘?

或者有更好的方法吗?

【问题讨论】:

  • 你的图标有透明度吗?您是否尝试使用 setStyleSheet(); ?它允许您在 QPushButton 上使用 CSS :)
  • 图标是一个动态颜色的正方形。每个按钮的不透明度是可变的。不确定样式表在这里有什么帮助。我正在使用 QPushButton::setIcon() 设置图标。是否可以通过样式表来操作这个图标?
  • 我不太明白问题出在哪里...如果您的图标具有透明值,则可以使用它们。但是,如果您只想拥有一个“圆形”图标,一种方法可能是使用 QLabel(例如使用 StyleSheet 设置,边框半径等)并将此 QLabel 放在 QPushButton 的顶部?

标签: c++ qt qpixmap qpushbutton


【解决方案1】:

这就是你如何准备一个带圆角的QPixmap

const QPixmap orig = QPixmap("path to your image");

// getting size if the original picture is not square
int size = qMax(orig.width(), orig.height());

// creating a new transparent pixmap with equal sides
QPixmap rounded = QPixmap(size, size);
rounded.fill(Qt::transparent);

// creating circle clip area
QPainterPath path;
path.addEllipse(rounded.rect());

QPainter painter(&rounded);
painter.setClipPath(path);

// filling rounded area if needed
painter.fillRect(rounded.rect(), Qt::black);

// getting offsets if the original picture is not square
int x = qAbs(orig.width() - size) / 2;
int y = qAbs(orig.height() - size) / 2;
painter.drawPixmap(x, y, orig.width(), orig.height(), orig);

然后您可以使用生成的像素图设置QPushButton 图标:

QPushButton *button = new QPushButton(this);
button->setText("My button");
button->setIcon(QIcon(rounded));

当然,您还有第二个选择,即使用一些图像编辑器预先准备带有圆角的图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 2019-08-31
    相关资源
    最近更新 更多