qslider如何更改滑块和进度条的样式

neipe2003 2011-06-03 11:33:34
qslider滑块的大小,形状,颜色和进度条的颜色怎么改。
以美观为主!
...全文
2879 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xxcc309 2011-06-03
  • 打赏
  • 举报
回复
可以setStyle(new style);
在style中重写函数drawComplexControl(ComplexControl control, const QStyleOptionComplex *option,QPainter *painter, const QWidget *widget) const,在函数中画slider。

case CC_Slider:
if (const QStyleOptionSlider *slider = qstyleoption_cast<const QStyleOptionSlider *>(option)) {
QRect groove = subControlRect(CC_Slider, option, SC_SliderGroove, widget);
QRect handle = subControlRect(CC_Slider, option, SC_SliderHandle, widget);
painter->save();
QPixmap grv = cached( tr(":/MyQTInterface/Resources/sliderGroove.png") );//设置进度条图片
QPixmap hndl = cached( tr(":/MyQTInterface/Resources/sliderHandle.png") );//设置滑块图片
QPixmap hndlMove = cached( tr(":/MyQTInterface/Resources/sliderHandleMove.png"));
if ((option->subControls & SC_SliderGroove) && groove.isValid() && handle.isValid()) {
painter->drawPixmap(QRect(groove.x() , groove.y()-2 ,
groove.width(), grv.height()),
grv);
}
//如果被选中
if ((option->subControls & SC_SliderHandle) && (slider->state & State_Sunken))
painter->drawPixmap(handle.topLeft(), hndlMove);
else if((option->subControls & SC_SliderHandle))
painter->drawPixmap(handle.topLeft(), hndl);
painter->restore();
}
break;

图片根据自己需要换上自己喜欢的就可以了。
ppdayz 2011-06-03
  • 打赏
  • 举报
回复
qslider->setstylesheet()
QSlider::groove:horizontal {
border: 1px solid #999999;
height: 8px; /* the groove expands to the size of the slider by default. by giving it a height, it has a fixed size */
background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 #c4c4c4);
margin: 2px 0;
}

QSlider::handle:horizontal {
background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f);
border: 1px solid #5c5c5c;
width: 18px;
margin: -2px 0; /* handle is placed by default on the contents rect of the groove. Expand outside the groove */
border-radius: 3px;
}

如果想改变颜色的滑块部分之前和之后,可以使用dd-page and sub-page的子控制:

QSlider::groove:vertical {
background: red;
position: absolute; /* absolutely position 4px from the left and right of the widget. setting margins on the widget should work too... */
left: 4px; right: 4px;
}

QSlider::handle:vertical {
height: 10px;
background: green;
margin: 0 -4px; /* expand outside the groove */
}

QSlider::add-page:vertical {
background: white;
}

QSlider::sub-page:vertical {
background: pink;
}

16,239

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧