Q按钮上的圆形图标
Rounded icon on Qpushbutton
我有一个QPushButton
,我想在其中添加一个带圆角的图标(通过使用QPushButton::setIcon()
添加到按钮)。然而,我有一个像素图,它是一个正方形的图像。有可能调整像素图使其变圆吗?
我在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));
当然,你还有第二种选择,可以使用一些图像编辑器预先准备圆角图像。
相关文章:
- 问:如何使用C++中的按钮从窗口打开窗口
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- QML按钮点击功能执行顺序
- 如何在 c++ 中将图标放在按钮上?
- 如何在 VC6 中的对话框中将图标设置为按钮
- GTK 3 C++按钮中带有标签的图标
- GTK 卷按钮图标显示初始值 c++
- 单击工具按钮后,在GTKMM工具栏中更改工具按钮图标
- 如何在Qt应用程序中使用样式表更改QPush按钮图标大小
- 使用 Win32 API 禁用消息框中右上角C++ X 按钮图标?
- CToolbar是否支持带有32位图标的按钮
- Q按钮上的圆形图标
- 在q按钮上设置图标
- Gtk::缩放按钮:构造函数中的图标
- 在文本上方为 MFC 按钮制作图标
- MS功能区:从代码中绘制按钮图标
- 如何创建桌面图标/按钮来启动用户Windows屏幕保护程序,并设置选项
- 如何在MFC对话框按钮上设置管理权限图标
- c++自定义按钮图标
- Qt复选框/工具按钮,带有自定义/不同的检查/未选中的图标