在Qt中设置窗体背景颜色及透明度的方法

本文详细介绍了如何在面向Windows操作系统的QtCreator7.0.0中,使用样式表(StyleSheet)来改变Qt应用的窗体背景颜色和透明度。通过设置rgba参数,可以实现从不透明到半透明的效果,例如将背景设为50%透明的红色或10%透明的红色,并展示了相应的界面效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文主要介绍在Qt中设置窗体背景颜色及透明度的方法。

说明:

  • 本文中的应用程序是面向Windows操作系统的;
  • 本文中使用的Qt Creator版本号为:7.0.0;
  • 本文中使用的Qt版本号为:5.14.2。

1 利用样式表(Style Sheet)

在Qt Creator的“Design”界面中,右键点击待设置的窗体(组件),在弹出的菜单中,选择“Change styleSheet...”,如下图所示:

点击上图中的“Change styleSheet...”后,会弹出一个编辑样式表“Edit Style Sheet”窗口,如下图所示:

在使用Qt样式表设置窗体背景颜色及透明度时,需要使用如下参数:

background:rgba(r,g,b,opacity);

其中,“rgb”是颜色值,“opacity”是透明度(有效范围从0.0至1.0,1.0为不透明),我们直接在上面的“Edit Style Sheet”窗口中输入待设置的背景颜色及透明度参数即可。

例如,如果想设置红色背景,50%透明度,则将下列参数填入“Edit Style Sheet”窗口中,然后点击“OK”确认,如下图所示:

background-color: rgba(255,0,0,0.5);

此时,程序呈现的界面效果,如下图所示(红色标注部分):

如果改为10%的透明度(将透明度参数为0.1),则程序界面效果,如下图所示(红色标注部分):

华为OD华为公司用于招聘软件开发工程师的一项重要考核环节。2024华为OD题库中包含了多种类型的编程题目,主要以C语言为主。以下是一些常见的题型和示例: 1. **基础算法题**: - 题目:实现一个函数,计算两个整数的最大公约数(GCD)。 - 示例代码: ```c #include <stdio.h> int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { int a, b; printf("Enter two integers: "); scanf("%d %d", &a, &b); printf("GCD of %d and %d is %d\n", a, b, gcd(a, b)); return 0; } ``` 2. **数据结构题**: - 题目:实现一个简单的链表,并提供插入和删除操作。 - 示例代码: ```c #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; void insert(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } void deleteNode(struct Node** head_ref, int key) { struct Node *temp = *head_ref, *prev; if (temp != NULL && temp->data == key) { *head_ref = temp->next; free(temp); return; } while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } if (temp == NULL) return; prev->next = temp->next; free(temp); } void printList(struct Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } } int main() { struct Node* head = NULL; insert(&head, 3); insert(&head, 2); insert(&head, 1); printf("Created Linked List: "); printList(head); deleteNode(&head, 2); printf("\nLinked List after Deletion of 2: "); printList(head); return 0; } ``` 3. **逻辑题**: - 题目:给定一个整数数组,找出其中没有出现的最小正整数。 - 示例代码: ```c #include <stdio.h> int firstMissingPositive(int* nums, int numsSize) { for (int i = 0; i < numsSize; i++) { while (nums[i] > 0 && nums[i] <= numsSize && nums[nums[i] - 1] != nums[i]) { int temp = nums[nums[i] - 1]; nums[nums[i] - 1] = nums[i]; nums[i] = temp; } } for (int i = 0; i < numsSize; i++) { if (nums[i] != i + 1) { return i + 1; } } return numsSize + 1; } int main() { int nums[] = {3, 4, -1, 1}; int size = sizeof(nums)/sizeof(nums[0]); printf("The smallest missing positive integer is %d\n", firstMissingPositive(nums, size)); return 0; } ``` 这些题目涵盖了C语言编程中的基础算法、数据结构和逻辑思维等方面。准备华为OD时,建议多练习类似题型,提升编程能力和解题技巧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liitdar

赠人玫瑰,手有余香,君与吾共勉

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值