大家好,我按照别的程序做了一个小闹钟。 现在想把他的背景颜色设置为半透明色,可是看了很久,也没有设置从成功。
源代码如下,大家帮我看看是怎么回事,谢谢了!!! (注释部分是我不停的测试用的)
#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();
}