如何更改ui-> widget-> windowOpacity?

时间:2011-09-13 05:29:56

标签: qt qt4

现在我想更改ui-> widget-> windowOpacity和ui-> stackedWidget-> windowOpacity(),但是失败了。

void Widget::on_pushButton_clicked()
{
   QTimer *timerOpacity = new QTimer(this);
   connect(timerOpacity,SIGNAL(timeout()),this,SLOT(changeOpacity()));
   timerOpacity->start(100);
}

void Widget::changeOpacity()
{
    if (opalevel > 8) return;

    double opacity[9] =  {0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1};

    ui->stackedWidget->setWindowOpacity(opacity[opalevel]);
    ui->widget->setWindowOpacity(opacity[opalevel]);
    this->setWindowOpacity(opacity[opalevel]);

    qDebug()<<"opalevel = "<< opalevel <<" ui->widget opacity:"<<ui->widget->windowOpacity();
    qDebug()<<"opalevel = "<< opalevel <<" ui->stackedWidget opacity:"<<ui->stackedWidget->windowOpacity();
    qDebug()<<"opalevel = "<< opalevel <<" ui opacity : "<< this->windowOpacity()<<endl;

    opalevel++;
}  

调试日志:

opalevel =  0  ui->widget opacity: 1 
opalevel =  0  ui->stackedWidget opacity: 1 
opalevel =  0  ui opacity:  0.898039 

opalevel =  1  ui->widget opacity: 1 
opalevel =  1  ui->stackedWidget opacity: 1 
opalevel =  1  ui opacity:  0.8 

opalevel =  2  ui->widget opacity: 1 
opalevel =  2  ui->stackedWidget opacity: 1 
opalevel =  2  ui opacity:  0.698039 

opalevel =  3  ui->widget opacity: 1 
opalevel =  3  ui->stackedWidget opacity: 1 
opalevel =  3  ui opacity:  0.596078 

opalevel =  4  ui->widget opacity: 1 
opalevel =  4  ui->stackedWidget opacity: 1 
opalevel =  4  ui opacity:  0.498039 

opalevel =  5  ui->widget opacity: 1 
opalevel =  5  ui->stackedWidget opacity: 1 
opalevel =  5  ui opacity:  0.4 

opalevel =  6  ui->widget opacity: 1 
opalevel =  6  ui->stackedWidget opacity: 1 
opalevel =  6  ui opacity:  0.298039 

opalevel =  7  ui->widget opacity: 1 
opalevel =  7  ui->stackedWidget opacity: 1 
opalevel =  7  ui opacity:  0.2 

opalevel =  8  ui->widget opacity: 1 
opalevel =  8  ui->stackedWidget opacity: 1 
opalevel =  8  ui opacity:  0.0980392 

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您可以在非顶级小部件上使用QGraphicsOpacityEffect

// in the constructor
opacityEffect = new QGraphicsOpacityEffect;
ui->widget->setGraphicsEffect(opacityEffect);
...
// in changeOpacity()
opacityEffect->setOpacity(opacity[opalevel]);