21 #include <QPaintEvent> 27 setMinimumSize( QSize( 100, 40 ) );
32 setMinimumSize( QSize( 100, 40 ) );
37 QSlider::paintEvent( event );
38 QPainter painter(
this );
39 QRect rect = geometry();
40 painter.setPen( QPen( palette().color( QPalette::WindowText ) ) );
41 painter.drawText( QRectF( 0, rect.height() * 0.5, rect.width(), rect.height() ),
70 void QgsSlider::update()
72 if ( mMin.isNull() || mMax.isNull() || mStep.isNull() )
75 if ( mValue.isNull() )
78 if ( mMin.type() == QVariant::Int &&
79 mMax.type() == QVariant::Int &&
80 mStep.type() == QVariant::Int &&
81 mValue.type() == QVariant::Int )
83 QSlider::setMinimum( mMin.toInt() );
84 QSlider::setMaximum( mMax.toInt() );
85 QSlider::setSingleStep( mStep.toInt() );
86 QSlider::setValue( mValue.toInt() );
89 if ( mMin.type() == QVariant::Double &&
90 mMax.type() == QVariant::Double &&
91 mStep.type() == QVariant::Double &&
92 mValue.type() == QVariant::Double )
95 QSlider::setMinimum( 0 );
97 int max = std::ceil( ( mMax.toDouble() - mMin.toDouble() ) / mStep.toDouble() );
98 if ( maximum() != max )
99 QSlider::setMaximum( max );
101 if ( singleStep() != 1 )
102 QSlider::setSingleStep( 1 );
104 QSlider::setValue( std::ceil( ( mValue.toDouble() - mMin.toDouble() ) / mStep.toDouble() ) );
107 connect(
this, &QSlider::valueChanged,
this, &QgsSlider::onValueChanged );
115 void QgsSlider::onValueChanged(
int value )
117 if ( mMin.isNull() || mMax.isNull() || mStep.isNull() )
121 else if ( mMin.type() == QVariant::Int &&
122 mMax.type() == QVariant::Int &&
123 mStep.type() == QVariant::Int &&
124 mValue.type() == QVariant::Int )
128 else if ( mMin.type() == QVariant::Double &&
129 mMax.type() == QVariant::Double &&
130 mStep.type() == QVariant::Double &&
131 mValue.type() == QVariant::Double )
133 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