1. QPalette的方法

基本步骤:

(1). 首先设置QWidget的autoFillBackground属性为真

(2). 然后定义一个QPalette对象,设置QPalette对象的背景属性(颜色或者图片);

(3). 最后设置QWidget对象的QPalette。

QWidget *widget=new QWidget;widget->autoFillBackground(true);QPalette palette;palette.setColor(QPalette::Background,QColor(192,253,123));  //设置背景色//palette.setBrush(QPalette::Background,QBrush(QPixmap(":/background.png")));  //设置背景图片widget->setPalette(palette);

或者:

#include <QApplication>
#include <QtGui>int main(int argc, char *argv[])
{QApplication app(argc,argv);QFrame *frame = new QFrame;frame->resize(400,700);QPixmap pixmap(":/images/frame.png");//设定图片QPalette palette;//创建一个调色板对象palette.setBrush(frame->backgroundRole(),QBrush(pixmap));//用调色板的画笔把映射到pixmap上的图片画到            frame.backgroundRole()这个背景上//palette.setColor(frame->backgroundRole(),QColor(192,253,123));frame->setPalette(palette);//设置窗口调色板为palette,窗口和画笔相关联frame->setMask(pixmap.mask()); //可以将图片中透明部分显示为透明的frame->setAutoFillBackground(true);//设置窗体自动填充背景frame->show();return app.exec();
}

存在问题:图片可以显示出来,但是图片大小不能和frame大小一致,显示效果不好,具体怎样调整大小,以后再补充;

  1. setStyleSheet方法(非常好用的方法)
 #include <QApplication>
#include <QtGui>int main(int argc, char *argv[])
{QApplication app(argc,argv);QFrame *frame = new QFrame;frame->setObjectName("myframe");frame->resize(400,700);frame->setStyleSheet("QFrame#myframe{border-image:url(images/frame.png)}" );frame->show();return app.exec();
}

注意:frame->setObjectName(“myframe”);,设置ObjectName后,才能保证setStyleSheet 只作用在我们的frame上,不影响其子控件的背景设置。之所以用border-image而不用background-image,还是上面的问题,用 background-image不能保证图片大小和控件大小一致,图片不能完全显示,这个以后再补充了,现在还没有找到方法。

  1. paintEvent事件方法
//myframe.h文件
#ifndef MYFRAME_H
#define MYFRAME_H#include <QWidget>
#include <QtGui>class MyFrame : public QWidget
{public:MyFrame();void paintEvent(QPaintEvent *event);
};#endif // MYFRAME_H//myframe.cpp文件
#include "myframe.h"MyFrame::MyFrame()
{}void MyFrame::paintEvent(QPaintEvent *event)
{QPainter painter(this);painter.drawPixmap(0,0,400,700,QPixmap("images/frame.png"));
}//main.cpp文件
#include <QApplication>
#include <QtGui>#include "myframe.h"int main(int argc, char *argv[])
{QApplication app(argc,argv);MyFrame *frame = new MyFrame;frame->resize(400,700);frame->show();return app.exec();
}

与前面的差别就是这个背景图片不随着窗口的大小而变化,因为它的固定大小被设置成(400,700)了。重写QWidget的paintEvent事件,当控件发生重绘事件,比如show()时,系统就会自动调用paintEvent函数。

好了,上面是三种设置背景图片的方法,下面我要说一个设置QPushButton的背景图片的方法,用的是setIcon方法(其实QPushButton设置背景图片也可以用前面三种方法的,不过现在这种Icon方法的看起来也不错)

#include <QApplication>
#include <QtGui>int main(int argc, char *argv[])
{QApplication app(argc,argv);QFrame *frame = new QFrame;QPushButton * button0 = new QPushButton(frame);QPushButton * button1 = new QPushButton(frame);QPushButton * button2 = new QPushButton(frame);QPushButton * button3 = new QPushButton(frame);QPushButton * button4 = new QPushButton(frame);QPushButton * button5 = new QPushButton(frame);frame->setObjectName("myframe");frame->resize(400,700);frame->setStyleSheet("QFrame#myframe{border-image:url(images/frame.png)}" );button0->setGeometry(60,150,68,68);button1->setGeometry(160,150,68,68);button2->setGeometry(260,150,68,68);button3->setGeometry(60,280,68,68);button4->setGeometry(160,280,68,68);button5->setGeometry(260,280,68,68);QIcon icon;QPixmap pixmap0("images/SMS.png");icon.addPixmap(pixmap0);button0->setIcon(icon);button0->setIconSize(QSize(68,68));button0->setFixedSize(pixmap0.size());button0->setMask(pixmap0.mask());QPixmap pixmap1("images/EMail.png");icon.addPixmap(pixmap1);button1->setIcon(icon);button1->setIconSize(QSize(68,68));button1->setFixedSize(pixmap1.size());button1->setMask(pixmap1.mask());QPixmap pixmap2("images/Contacts.png");icon.addPixmap(pixmap2);button2->setIcon(icon);button2->setIconSize(QSize(68,68));button2->setFixedSize(pixmap2.size());button2->setMask(pixmap2.mask());QPixmap pixmap3("images/Calendar.png");icon.addPixmap(pixmap3);button3->setIcon(icon);button3->setIconSize(QSize(68,68));button3->setFixedSize(pixmap3.size());button3->setMask(pixmap3.mask());QPixmap pixmap4("images/GoogleVoice.png");icon.addPixmap(pixmap4);button4->setIcon(icon);button4->setIconSize(QSize(68,68));button4->setFixedSize(pixmap4.size());button4->setMask(pixmap4.mask());QPixmap pixmap5("images/AndroidMarket.png");icon.addPixmap(pixmap5);button5->setIcon(icon);button5->setIconSize(QSize(68,68));button5->setFixedSize(pixmap5.size());button5->setMask(pixmap5.mask());frame->show();return app.exec();
}

补充:

这样就可以让图片跟窗口一样大小了

 int main(int argc, char *argv[])
{QApplication app(argc,argv);QFrame *frame = new QFrame;frame->resize(400,700);QImage image1;image1.load("images/frame1.jpg");QImage image2 = image1.scaled(400,700);QPalette palette;palette.setBrush(frame->backgroundRole(),QBrush(image2));frame->setPalette(palette);frame->setMask(pixmap.mask()); //可以将图片中透明部分显示为透明的frame->setAutoFillBackground(true);frame->show();return app.exec();
}

QT设置背景图片的三种方式相关推荐

  1. 设置背景图片的两种方式,并解决手机端背景图片高度自适应问题

    设置背景图片的两种方式,并解决手机端背景图片高度自适应问题 参考文章: (1)设置背景图片的两种方式,并解决手机端背景图片高度自适应问题 (2)https://www.cnblogs.com/Dark ...

  2. VSCode设置背景图片的两种方式

    1.可以通过直接修改VSCode的css代码来设置 2.可以通过下载插件来设置 第一种: 首先要找到css文件: 然后在css文件中加入如下代码: body {/*背景图片的路径*/backgroun ...

  3. QT设置背景图片的Qss实现方式

    代码如下: setStyleSheet("background-image:url(:/icon/Background.png); background-position: left top ...

  4. Qt设置背景图片方法

    本文主要介绍三种Qt设置背景图片的方法: 1.QPalette 2.重写paintEvent 3.设置Qss(Qt style sheet) 1.QPalette (1)示例 //.cppthis-& ...

  5. linux qt 背景图片,qt设置背景图片

    <一>http://blog.csdn.net/qq5823996/article/details/6851788 Qt的窗口背景图片有3种方式设置 1.QPalette p; p.set ...

  6. ImGui添加背景图片的两种方式

    给ImGui添加背景图片的两种方式 最近在使用ImGui做客户端程序,想给窗口添加背景图片,但是作者的文档里面好像并没有讲如何添加背景图片,研究了下找到了两种方式. 第一种 创建一个和窗口一样大的Im ...

  7. QT读写Sqlite数据库的三种方式

    QT对一些基本的数据库的访问封装,可谓是极大的方便的我们开发人员,现在我们就来说下QT对Sqlite这个数据库的读写,Sqlite是一个比较小型的本地数据库,对于保存一些软件配置参数或量不是很大的数据 ...

  8. QT设置背景图片以及设置按钮

    1.Qt设置背景图片 ①.设置UI窗口背景图片,需要在构造函数中添加以下代码:          this-> setAutoFillBackground(true);          QPa ...

  9. 微信小程序设置背景图的几种方式

    微信小程序设置背景图的几种方式 原本在html中可以通过background-image来设置背景图片 .page {width: 100%;height: 100%;background-size: ...

最新文章

  1. mysql sql 1到10_(1.10)SQL优化——mysql 常见SQL优化
  2. 生命周期结束,Spring Boot 1.x退役
  3. 怎么用pycharm更新python_利用PyCharm操作Github(仓库新建、更新,代码回滚)
  4. 裸眼3D全攻略3:拍摄3D—瞳距、镜距、视角偏转与空间感
  5. Java加密与解密的艺术~MD算法实现
  6. openvas 配置遇到的问题
  7. 99%程序员不知道的编程必备工具,人工智能助你编程更轻松
  8. .net core在Linux下获取AD域信息
  9. 虚拟化技术发展编年史
  10. bootstrap table 合并行_ElementUI Table组件如何使用合并行或列功能深入解析
  11. 乐檬:我们和fiil主题词相同是撞车!汪峰信吗?
  12. 接收邮件的服务器称为,接收邮件服务器
  13. 电子元器件的识别与换算
  14. flash读取程序 msp430_MSP430 flash的操作
  15. 枕头里面加进这些小宝贝能治高血压!快告知你父母~
  16. python爬虫练习之爬取豆瓣读书所有标签下的书籍信息
  17. ESP32 LVGL8.1 ——event 事件 (event 17)
  18. 【洛谷 P3191】 [HNOI2007]紧急疏散EVACUATE(二分答案,最大流)
  19. Android加速度传感器测位移,一种校核加速度传感器测位移的方法与流程
  20. 【运维面试】面试官:你觉得网站访问慢的原因有哪些?

热门文章

  1. 美国办公室里怎样着装
  2. softmax,softmax loss和cross entropy
  3. c语言中执行cl exe时出错,VC6.0执行cl.exe时出错
  4. Kubernetes联合创始人Brendan Burns:K8s让企业上云更容易
  5. 从SEO优化角度打造移动端网站的移动建站指南
  6. 人生是一场独自的修行_产品负责人经理–独自一人
  7. DAY29(DAY30拓展):Vulnhub--靶机实战
  8. Ubuntu下Logi MX Ergo自定义按键
  9. android kotlin 上传文件 上传图片
  10. java sql 工资管理,企业工资管理系统(Java+MySQL)Word版