Set background color only partially with stylesheets

隐身守侯 提交于 2019-12-01 05:07:29

问题


How can I set the background color for a part of the background like in the following image:

Of course, without border frames, I want to set only the cyan color.

I need to set the length of the left part (cyan) as the percentage of the widget length, e.g 30%.


回答1:


With css I would hack qlineargradient a little bit. Note that edge of cyan may be a little blurry.

QFrame
{
        background-color: qlineargradient(x1:0, x2: 1, stop: 0 cyan, stop: 0.29 cyan, stop: 0.2901 white, stop: 1 white);
}



回答2:


If you want it hard-coded in the application, you can overload the paintEvent function in a widget. Something like this:

void MyWidget::paintEvent(QPaintEvent *event)
{
  QPainter painter(this);
  QPen pen(Qt::NoPen);
  painter.setPen(pen);
  painter.fillRect(0, 0, width(), height(), Qt::white);
  painter.fillRect(0, 0, 0.3*width(), height(), Qt::cyan);
  ...
}


来源:https://stackoverflow.com/questions/10633966/set-background-color-only-partially-with-stylesheets

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!