21 #include <QPaintEvent> 28 setMinimumSize( QSize( 100, 40 ) );
33 setMinimumSize( QSize( 100, 40 ) );
38 QSlider::paintEvent( event );
39 QPainter painter(
this );
40 QRect rect = geometry();
41 painter.setPen( QPen( palette().color( QPalette::WindowText ) ) );
42 painter.drawText( QRectF( 0, rect.height() * 0.5, rect.width(), rect.height() ),
71 void QgsSlider::update()
73 if ( mMin.isNull() || mMax.isNull() || mStep.isNull() )
76 if ( mValue.isNull() )
79 if ( mMin.type() == QVariant::Int &&
80 mMax.type() == QVariant::Int &&
81 mStep.type() == QVariant::Int &&
82 mValue.type() == QVariant::Int )
84 QSlider::setMinimum( mMin.toInt() );
85 QSlider::setMaximum( mMax.toInt() );
86 QSlider::setSingleStep( mStep.toInt() );
87 QSlider::setValue( mValue.toInt() );
90 if ( mMin.type() == QVariant::Double &&
91 mMax.type() == QVariant::Double &&
92 mStep.type() == QVariant::Double &&
93 mValue.type() == QVariant::Double )
96 QSlider::setMinimum( 0 );
98 int max = std::ceil( ( mMax.toDouble() - mMin.toDouble() ) / mStep.toDouble() );
99 if ( maximum() != max )
100 QSlider::setMaximum( max );
102 if ( singleStep() != 1 )
103 QSlider::setSingleStep( 1 );
105 QSlider::setValue( std::ceil( ( mValue.toDouble() - mMin.toDouble() ) / mStep.toDouble() ) );
108 connect(
this, &QSlider::valueChanged,
this, &QgsSlider::onValueChanged );
116 void QgsSlider::onValueChanged(
int value )
118 if ( mMin.isNull() || mMax.isNull() || mStep.isNull() )
122 else if ( mMin.type() == QVariant::Int &&
123 mMax.type() == QVariant::Int &&
124 mStep.type() == QVariant::Int &&
125 mValue.type() == QVariant::Int )
129 else if ( mMin.type() == QVariant::Double &&
130 mMax.type() == QVariant::Double &&
131 mStep.type() == QVariant::Double &&
132 mValue.type() == QVariant::Double )
134 mValue = QVariant( mMin.toDouble() + value * mStep.toDouble() );
void setValue(const QVariant &value)
void setMaximum(const QVariant &max)
void setSingleStep(const QVariant &step)
void setMinimum(const QVariant &min)
QgsSlider(QWidget *parent=nullptr)
Constructor for QgsSlider.
void valueChanged(const QVariant &)
void paintEvent(QPaintEvent *event) override
QVariant variantValue() const