gpt4 book ai didi

c++ - QPalette 不适用于子 QWidget

转载 作者:可可西里 更新时间:2023-11-01 10:43:51 25 4
gpt4 key购买 nike

我想用 QLinerGradient 刷我的子部件。我使用 QtDesigner 创建了 ui。

enter image description here

但是我不能用这段代码刷这个widget。(ui.colorBarWidget是正常的QWidget是由QtDesigner创建的。)

   QPalette palette;
QLinearGradient gradient(ui.colorBarWidget->rect().topLeft(),ui.colorBarWidget->rect().topRight());

gradient.setColorAt(0, Qt::blue);
gradient.setColorAt(0.2, Qt::green);
gradient.setColorAt(0.4, Qt::red);
gradient.setColorAt(0.6, Qt::yellow);
gradient.setColorAt(1, Qt::cyan);
palette.setBrush(QPalette::Base, gradient);

ui.colorBarWidget->setPalette(palette);

此外,此代码可在独立的 QWidget 应用程序中运行。这是其输出:

enter image description here

但我不能在我的设计中做同样的事情。我可以用 styleSheet 做到这一点

ui.colorBarWidget->setStyleSheet( "background-color: qlineargradient( x1:0 y1:0, x2:0 y2:1, stop:0 blue, stop:1 red )" ); /* works */

但为什么我不能用 QPalette 做到这一点。

提前致谢。

最佳答案

我找到了解决方案。如果在设置调色板后使用:

ui.colorBarWidget->setAutoFillBackground(true);

此属性默认为 false。所以你应该启用它然后一切都很好。但是你也应该考虑尺寸,固定尺寸更好。

关于c++ - QPalette 不适用于子 QWidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36331496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com