当前位置:网站首页>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
边栏推荐
- Go ---- variable usage in go language
- [RequireComponent(typeof(....))]
- What you need to know about mobile video compatibility
- Resume in 2022 is dead in the sea. Don't vote. Software testing positions are saturated
- MPLS基础知识概述
- Just one dependency to give swagger a new skin, which is simple and cool~
- Overview of MPLS Basics
- AFNetworking了解
- [MySql]substr用法-查询表的某个字段的具体位数的值
- 软件测试技术之跨平台的移动端UI自动化测试(下)
猜你喜欢

Altium designer 22 Chinese character garbled

matlab 激励模型 三角波频谱

Flash source code startup phase

Triangular wave spectrum of MATLAB excitation model

深入源码剖析String类为什么不可变?(还不明白就来打我)

调试stc8a8k64d4单片机485通信总结

开发转测试:从零开始的6年自动化之路

Knowledge base tools | wechat, document center, image display page can be generated by dragging (with template, directly used)

07 df 命令

MOS 管示意图
随机推荐
[RequireComponent(typeof(....))]
Technology sharing | do you know the functions of the server interface automated testing and requests library?
I successfully landed the automatic testing post, with a maximum monthly salary of 15.4k. I'm great~
ansible安装及使用
Circular progress bar animation based on cashapelayer and Bezier curve
Pytoch -- used by visdom
Also on Data Governance
npm, npm中文文档, npm学习使用
也谈数据治理
SQL注入 Less26(过滤空格和注释符,使用不带空格的报错注入)
MOS 管示意图
知识库工具 | 微网站、文档中心、形象展示页拖拽即可生成(附模板,直接用)
Actual authority comes from information superiority
Concept and classification of processes
mysql推荐书
Altium Designer 22 中文字符乱码
调试stc8a8k64d4单片机485通信总结
Altium designer 22 Chinese character garbled
Xshell7 personal free download, use
08 Du command