これは、あなたが丸みを帯びた角と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(たとえば、境界線の半径などのスタイルシートで設定)を使用して、このQLabelをQPushButtonの上に配置する方法があります。 –