In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
这篇文章主要介绍"Qt如何实现无边框背景透明窗体",在日常操作中,相信很多人在Qt如何实现无边框背景透明窗体问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Qt如何实现无边框背景透明窗体"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
一、前言
用Qt来做无边框北京透明窗体非常简单,根本不需要用什么系统层的API来实现透明什么的,Qt本身提供了很多种设置窗体透明的方法,除了可以设置窗体的属性为透明以外,还可以设置透明度函数,以及qss来设置透明度颜色等,方法很多,按照需要可以选用自己最合适的办法,如果想要整个窗体的背景图类似于无边框的异行,你只需要准备一张美工做好的png带透明的背景图即可,直接用qss的形式设置为窗体的背景图,你也可以用painter绘制上去,这样就可以产生各种奇形怪状的异行窗体,比如中间挖个洞的背景图,可以直接穿透桌面。
二、代码思路Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget){ ui->setupUi(this); this->setAttribute(Qt::WA_TranslucentBackground); this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint); ui->widget->installEventFilter(this); ui->widget->setStyleSheet(QString("background-image:url(:/image/%1.png);").arg(1));}Widget::~Widget(){ delete ui;}bool Widget::eventFilter(QObject *watched, QEvent *evt){ static int index = 1; static QPoint mousePoint; static bool mousePressed = false; QMouseEvent *event = static_cast(evt); if (event->type() == QEvent::MouseButtonPress) { if (event->button() == Qt::LeftButton) { mousePressed = true; mousePoint = event->globalPos() - this->pos(); if (index == 5) { index = 1; } else { index++; } ui->widget->setStyleSheet(QString("background-image:url(:/image/%1.png);").arg(index)); return true; } else { exit(0); } } 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);}三、效果图
到此,关于"Qt如何实现无边框背景透明窗体"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.