如何在 QPushButton 上设置图像? [英] How to set image on QPushButton?

查看:82
本文介绍了如何在 QPushButton 上设置图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在QPushButton上设置一个图片,QPushButton的大小应该取决于图片的大小.我可以在使用 QLabel 时做到这一点,但不能使用 QPushButton.

I want to set an image on QPushButton, and the size of QPushButton should depend on the size of the image. I am able to do this when using QLabel, but not with QPushButton.

所以,如果有人有解决方案,请帮助我.

So, if anyone has a solution, then please help me out.

推荐答案

你可以做的是使用一个像素图作为一个图标,然后把这个图标放到按钮上.

What you can do is use a pixmap as an icon and then put this icon onto the button.

为了确保按钮的大小正确,您必须根据像素图大小调整图标大小.

To make sure the size of the button will be correct, you have to reisze the icon according to the pixmap size.

这样的事情应该可以工作:

Something like this should work :

QPixmap pixmap("image_path");
QIcon ButtonIcon(pixmap);
button->setIcon(ButtonIcon);
button->setIconSize(pixmap.rect().size());

这篇关于如何在 QPushButton 上设置图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆