当前位置:网站首页>Qt中为工程添加资源文件、给按钮添加图片
Qt中为工程添加资源文件、给按钮添加图片
2022-07-26 21:28:00 【天天进步2015】
上一次布局好之后做了小小的改动,就是在左边放按钮的上面添加了一个放logo的label(虽然还没有想好要放什么logo—— )
现在的布局就是这样子了:

下面要做的是给按钮添加图标,还有在logo和右边的大label上放上初始的图片,这里就需要为工程添加资源文件了,步骤如下:
1、右击项目文件夹选择添加新文件——选择Qt——Qt resource file


2、填写好name后点下一步、完成,然后双击项目中生成的.qrc文件,点击添加,选择添加前缀

2、添加好前缀之后就可以添加文件了,选择需要添加的文件,这里需要把需要添加的文件先放到项目目录下的一个文件夹下,添加好之后保存,就能在资源浏览器中看到了,也就能在代码中引用了。

明天就可以把图标和logo添加上去了
——————————————————————————————————————————
/******
这里有个小插曲,我发现一开始我的movie都写成了moive ,以为要一个个改感觉好麻烦,后来发现可以将光标移动到需要更改的变量上,按Ctrl + Shift + R,这样就能把项目中的这个变量名称一起改掉了

********/
回到正题——给按钮添加图标,一共分为以下几步:
1、申明一个QIcon对象用来存放图标
[cpp] view plain copy
- QIcon button_ico(":/new/icon/srcs/movieclicked.png");
2、按钮对象调用setIcon()函数把图片加载到button上
[cpp] view plain copy
- button_movie->setIcon(button_ico);
运行一下发现按钮的大小和图标的大小都感觉很别扭:

我想最好能图标变大一点,然后按钮和图标一样大。
但是发现无论是在将button放到layout之前还是之后调用QPushButton的resize()函数都没有效果。
我想因为是放在layout里就算是resize后也会根据窗口的大小又自动改掉,所以就直接把按钮的最大最小值设置为一样就行了:
[cpp] view plain copy
- QIcon button_ico(":/new/icon/srcs/movieclicked.png");
- button_movie = new QPushButton;
- button_movie->setMinimumSize(33,33);
- button_movie->setMaximumSize(33,33);
- button_movie->setIcon(button_ico);
- button_movie->setIconSize(QSize(28,28));
效果就是这样的:(下面图案边框可以去掉,button_movie ->setFlat(ture) http://jingyan.baidu.com/article/cd4c29791c3e16756f6e6043.html)

图标添加完了,接着就把logo和右边的label上添加上图片

添加图片部分的代码如下:
[cpp] view plain copy
- logo_label = new QLabel("LOGO"); //添加图片
- QImage *logo_img = new QImage(":/new/label/srcs/logo.png");
- QImage *scaled_logo_img = new QImage();
- *scaled_logo_img=logo_img->scaled(150,120,Qt::KeepAspectRatio);
- logo_label->setPixmap(QPixmap::fromImage(*scaled_logo_img));
右边图片的添加方法和这个logo是一样的。
网址:http://blog.csdn.net/lbb2016/article/details/52675773
边栏推荐
- 【Try to Hack】防火墙(一)
- Keyword maintenance of dream weaving document doesn't work
- 什么是 Base64 ?
- Altium Designer 22 中文字符乱码
- Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering
- Knowledge base tools | wechat, document center, image display page can be generated by dragging (with template, directly used)
- [audio and video] ijkplayer player parameter description document
- [RequireComponent(typeof(....))]
- Flash source code startup phase
- 现货黄金操作指南与建议(上)
猜你喜欢

Matlab pitch period estimation post-processing

Let me show you the MySQL isolation level. What happens when two transactions operate on the same row of data at the same time?

mysql推荐书

1 - "pytorch deep learning practice" - linear model

2018 arXiv preprint | MolGAN: An implicit generative model for small molecular graphs

Understanding and practice of the trend of Bank of London foreign exchange

06 cp 命令

Jd.com: how does redis realize inventory deduction? How to prevent goods from being oversold?

Just one dependency to give swagger a new skin, which is simple and cool~

进程的概念和分类
随机推荐
正规方程法(Normal Equation)原理以及与梯度下降法的区别
Join method in JS
【Try to Hack】防火墙(一)
Li Kou daily question - day 43 -168. Name of Excel table column
Flink 在讯飞 AI 营销业务的实时数据分析实践
I successfully landed the automatic testing post, with a maximum monthly salary of 15.4k. I'm great~
Actual authority comes from information superiority
Flash source code startup phase
08 du 命令
Pytoch squeeze() unsqueeze() usage
Leetcode exercise - Sword finger offer II 005. maximum product of word length
Unity installation failed: operation not allowed, MKDIR
Matlab draws short-term average amplitude spectrum
Highlight the secondary and tertiary columns under the primary column of pbootcms
Software Testing Technology: cross platform mobile UI automated testing (Part 2)
MPLS基础知识概述
Isilon's onefs common operation commands (I)
[MySql]substr用法-查询表的某个字段的具体位数的值
A friend with a monthly salary of 50000 told me that you were just doing chores
Xshell7 personal free download, use