当前位置: 代码迷 >> QT开发 >> QT widget设置半透明的有关问题
  详细解决方案

QT widget设置半透明的有关问题

热度:56   发布时间:2016-04-25 04:48:17.0
QT widget设置半透明的问题
大家好,我按照别的程序做了一个小闹钟。 现在想把他的背景颜色设置为半透明色,可是看了很久,也没有设置从成功。
源代码如下,大家帮我看看是怎么回事,谢谢了!!! (注释部分是我不停的测试用的)

[code=C/C++][/code]#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();
}


------解决方案--------------------
setWindowOpacity 这个函数 在X11平台上需要composite扩展才能工作。在XP MAC平台直接支持。下午看书的时候看到这了!182页,精通QT编程第二版。
  相关解决方案