1
我有一个QPushButton
,我想用圆角添加一个图标(通过使用QPushButton::setIcon()
添加到按钮)。不过,我有一个方形图像的像素图。是否有可能使像素图变得圆润?Qpushbutton上的圆形图标
我在QPixmap
上找到了setMask()
函数,我可以使用它。但是,我将如何制作一个掩盖我的QPixmap
边缘的位图?
或者还有更好的方法吗?
我有一个QPushButton
,我想用圆角添加一个图标(通过使用QPushButton::setIcon()
添加到按钮)。不过,我有一个方形图像的像素图。是否有可能使像素图变得圆润?Qpushbutton上的圆形图标
我在QPixmap
上找到了setMask()
函数,我可以使用它。但是,我将如何制作一个掩盖我的QPixmap
边缘的位图?
或者还有更好的方法吗?
这是你怎么能带圆角准备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));
当然你有第二可以选择使用一些图像编辑器预先准备带圆角的图像。
你的图标有任何透明度? 你尝试过使用setStyleSheet(); ?它允许你在你的QPushButton上使用CSS :) –
这个图标是一个动态颜色的正方形。每个按钮的不透明度是可变的。不确定样式表将如何帮助这里。我使用QPushButton :: setIcon()来设置图标。是否有可能通过样式表来操纵这个图标? – Frank
我真的不明白问题所在......如果您的图标具有透明价值,您可以使用它们。 但是,如果你只是想要一个“圆形”的图标,一种方法可能是使用QLabel(用StyleSheet设置例如边框半径等),并将这个QLabel放在你的QPushButton之上? –