当前位置: 首页 > news >正文

Qt圆角窗口

Qt圆角窗口

问题:自己重写了一个窗口,发现用qss设置圆角了,但是都不生效,不过子窗口圆角都生效了。

无边框移动窗口

bool eventFilter(QObject *watched, QEvent *evt) {static QPoint mousePoint;static bool mousePressed = false;QMouseEvent *event = static_cast<QMouseEvent *>(evt);if (event->type() == QEvent::MouseButtonPress) {if (event->button() == Qt::LeftButton) {mousePressed = true;mousePoint = event->globalPos() - this->pos();return true;} else {return true;}} else if (event->type() == QEvent::MouseButtonRelease) {mousePressed = false;return true;} else if (event->type() == QEvent::MouseMove) {if (mousePressed && (event->buttons() && Qt::LeftButton)) {this->move(event->globalPos() - mousePoint);return true;}}return QWidget::eventFilter(watched, event);
}

1.当前窗口背景隐藏,使用子窗口圆角

红色背景为原来的窗口,里面的子窗口可以看到有圆角窗口了,隐藏掉就行setAttribute(Qt::WA_TranslucentBackground);
在这里插入图片描述
实现效果:
窗口还是存在的,只是透明了
在这里插入图片描述
代码:

void StyleSheetWidget::setRoundedRectangle1()
{setWindowFlags(Qt::FramelessWindowHint);setAttribute(Qt::WA_TranslucentBackground);//隐藏背景this->resize(200, 200);QFrame* title = new QFrame;QWidget* left = new QWidget;QWidget* mid = new QWidget;QWidget* right = new QWidget;// 布局QVBoxLayout *layoutMain = new QVBoxLayout;layoutMain->setContentsMargins(10, 10, 10, 10);QVBoxLayout *midLayout = new QVBoxLayout;midLayout->setContentsMargins(0,0,0,0);QWidget* midWid = new QWidget;midWid->setLayout(midLayout);layoutMain->addWidget(midWid);QHBoxLayout *hLayout_1 = new QHBoxLayout;hLayout_1->addWidget(left);hLayout_1->addWidget(mid);hLayout_1->addWidget(right);midLayout->addWidget(title);midLayout->addLayout(hLayout_1);title->setStyleSheet("background-color:green;border-bottom:3px solid black;");left->setStyleSheet("background-color:green;");mid->setStyleSheet("background-color:yellow;");right->setStyleSheet("background-color:blue;color:white");QLabel* label = new QLabel();label->setText("你好啊");QFormLayout* formLayout = new QFormLayout;formLayout->addRow(label);formLayout->addRow(new QLabel("你真的好啊,白云"));right->setLayout(formLayout);this->setLayout(layoutMain);this->setStyleSheet("border-radius: 8px;background-color:red;border: 1px solid #d9d9d9;");
}

2.使用paintEvent()绘制圆角

注意:使用paintEvent绘制圆角时候,布局之间要留一点距离,不然上层的绘制会把下面绘制的圆角给遮住了。如:

layoutMain->setContentsMargins(10,10,10,10);

设置布局间距离为10的效果:
在这里插入图片描述
设置为0效果:

    QVBoxLayout *layoutMain = new QVBoxLayout;layoutMain->setContentsMargins(0,0,0,0);

在这里插入图片描述
代码:

void StyleSheetWidget::paintEvent(QPaintEvent *event)
{QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing);painter.setBrush(QColor(255,255,255));      //背景颜色//painter.setPen(Qt::red);//Qt::transparent); //边框颜色QPen pen;pen.setColor(Qt::red);pen.setWidth(3);painter.setPen(pen);QRect rect = this->rect();rect.setWidth(rect.width());rect.setHeight(rect.height());painter.drawRoundedRect(rect, 15, 15);QWidget::paintEvent(event);
}
http://www.lryc.cn/news/443270.html

相关文章:

  • 研究生第一次刷力扣day1
  • flink自定义process,使用状态求历史总和(scala)
  • 股指期货理论价格计算公式是什么?
  • 解决R包依赖版本不兼容问题
  • HarmonyOS开发者基础认证考试试题
  • 如何使用 React、TypeScript、TailwindCSS 和 Vite 创建 Chrome 插件
  • 机器学习——Stacking
  • 在HTML中添加图片
  • R语言机器学习算法实战系列(二) SVM算法(Support Vector Machine)
  • gdb调试使用记录
  • ESXi安装【真机和虚拟机】(超详细)
  • 基于SpringBoot+Vue的高校门禁管理系统
  • 【Linux-基础IO】C语言文件接口回顾 系统文件概念及接口
  • 系统架构笔记-3-信息系统基础知识
  • Linux下编程实现网络传送文件
  • 【速成Redis】04 Redis 概念扫盲:事务、持久化、主从复制、哨兵模式
  • SQL Server 2022的数据类型
  • Linux基础3-基础工具4(git),冯诺依曼计算机体系结构
  • 后台数据管理系统 - 项目架构设计-Vue3+axios+Element-plus(0916)
  • MySQL基础篇(黑马程序员2022-01-18)
  • nodejs 013:Prect 样式复用(multiple classes)例子
  • MQ入门(一):同步调用和异步调用--RabbitMQ基础入门
  • 由于安全风险,安全领导者考虑禁止人工智能编码
  • 地图相关的系统软件及插件
  • Elasticsearch如何排序,分页以及高亮查询
  • Transformer预测 | 基于Transformer心率时间序列预测(tensorflow)
  • 科研绘图系列:R语言误差连线图(errobar linechart)
  • 智能BI项目第五期
  • Android-UI设计
  • docker desktop windows stop