QT widget设置半透明的问题

sno_guo sno_guo 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
大家好,我按照别的程序做了一个小闹钟。 现在想把他的背景颜色设置为半透明色,可是看了很久,也没有设置从成功。
源代码如下,大家帮我看看是怎么回事,谢谢了!!! (注释部分是我不停的测试用的)

#include "widget.h"
#include <QTime>
#include <QTimer>
#include <QMouseEvent>

DIgiClock::DIgiClock(QWidget *parent) :
QLCDNumber(parent)
{
resize(250,60);

QPalette p=palette(); //调色板
p.setColor(QPalette::Window,Qt::blue);
p.setColor(QPalette::Background, QColor(0x00,0xff,0x00,0x00));
setPalette(p);

setWindowFlags(Qt::FramelessWindowHint);//去掉边框。
setNumDigits(8);//设置
// setWindowOpacity(0.7);
// setAttribute(Qt::WA_TranslucentBackground, true);
//setAttribute(Qt::WA_TranslucentBackground);

QTimer *timer=new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(ShowTime()));
timer->start(500);
ShowTime();

showColon=true;//只是滴答滴答跳到。就是不停取反。
}

void DIgiClock::ShowTime()
{
QTime time=QTime::currentTime();
QString text=time.toString("hh:mm:ss");
if(showColon)
{
text[2]=':'; //因为hh:mm:ss text[2] =':'
text[5]=':';
showColon=false;
}
else
{
text[2]=' ';
text[5]=' ';
showColon=true;
}
display(text);
}

void DIgiClock::mousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
dragPosition=event->globalPos()-frameGeometry().topLeft(); //新位置
event->accept();//子类要了这个事件,如果子类不要,传送到基类
}
else if(event->button()==Qt::RightButton)
{
close();
}
}
void DIgiClock::mouseMoveEvent(QMouseEvent *event)
{
move(event->globalPos()-dragPosition);//两个类也可以相减
event->accept();
}
...全文

相关推荐

评论 15
收藏 已收藏
分享
返回顶部
sno_guo
Qt 21,503 成员 26,693 内容