View Full Version : Promlems with setStyleSheet

17th June 2011, 00:13
Hi to all,
I'm doing some test with the style sheet. I'm trying to customize a QSlider.
I create a class inherited from QFrame containing just a QSlider so:

#ifndef __VOLWIDGET_H__
#define __VOLWIDGET_H__

#include <QFrame>
#include <QSlider>

class VolWidget : public QFrame

VolWidget( QWidget* parent );
virtual ~VolWidget();

void sig_volChanged(int);


QSlider* m_volSlider;
#endif // __VOLWIDGET_H__
After creating the QSlider in the ctor I would apply to it a style using the setStyleSheet routine.

VolWidget::VolWidget( QWidget* parent )
: QFrame(parent)
setFrameStyle( QFrame::Panel | QFrame::Raised );

setStyleSheet(" QSlider::groove:vertical { \
background: red; \
position: absolute; \
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; }");

m_volSlider = new QSlider(Qt::Horizontal);
m_volSlider->setMinimum( 0 );
m_volSlider->setMaximum( 100 );
m_volSlider->setValue( 100 );

QVBoxLayout *vLayout = new QVBoxLayout();
vLayout->setContentsMargins(0, 0, 0, 0);


connect(m_volSlider, SIGNAL( valueChanged(int) ), this, SIGNAL( sig_volChanged(int) ) );

In my case the code has no effect. No style sheet is applied to the QSlider.
Where I'm wrong?

Santosh Reddy
17th June 2011, 01:35
Your code is correct, the reason you not able to see the style sheet applied is...

You are setting style for QSlider::groove:vertical, QSlider::handle:vertical, QSlider::add-page:vertical and QSlider::sub-page:vertical, all these are vertical components; and you are adding a new QSlider(Qt::Horizontal), which uses horizontal components

so either change you style to horizontal, or change QSlider to vertical:cool: