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

发布于 2024-12-04 18:41:44 字数 2220 浏览 0 评论 0原文

现在我想更改 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 

谢谢您的帮助。

Now I want to change the ui->widget->windowOpacity and ui->stackedWidget->windowOpacity() ,but failed.

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++;
}  

Debug log :

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 

Thank you for your help .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯下孤影 2024-12-11 18:41:44

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

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

You can use QGraphicsOpacityEffect on your non-top-level widgets:

// in the constructor
opacityEffect = new QGraphicsOpacityEffect;
ui->widget->setGraphicsEffect(opacityEffect);
...
// in changeOpacity()
opacityEffect->setOpacity(opacity[opalevel]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文