【Qt】Qt组件设置背景图片

1. 方法1(paintEvent方式)

使用paintEvent()实现

1. .h文件中添加虚函数

c 复制代码
protected:
	void paintEvent(QPaintEvent *event) override;
  • 添加虚函数方法:
    • 选中父类,点击鼠标右键
    • 点击重构
    • 点击 Insert Virtual Funtion of Base Classes

2. .c文件实现

c 复制代码
void M_LoginDlg::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.drawPixmap(rect(), QPixmap(":/Img/Img/loginBg.jpg"));
}

3. 效果

4. 备注

  • 图片适应组件的尺寸

2. 方法2(.ui中实现)

1. 实现


2. 效果

3.问题

  • 此方式中显示的背景是图片原始大小,当组件尺寸大于图片时,背景图片是重复的

参考

  1. Qt中正确的设置窗体的背景图片的几种方式

  2. 设置QWidget背景图的几种方法

相关推荐
用户805533698033 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner3 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz8 天前
QML Hello World 入门示例
qt
xcyxiner11 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner12 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner12 天前
DicomViewer (添加模型类)3
qt
xcyxiner13 天前
DicomViewer (目录调整) 2
qt
xcyxiner13 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR00615 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术15 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript