From 07720c5f66ae8fdde121ef5462abda11b88e0383 Mon Sep 17 00:00:00 2001 From: laserpants Date: Mon, 2 May 2016 23:35:04 +0300 Subject: [PATCH] set correct slider thumb position from current value --- components/slider.cpp | 5 +++-- components/slider_p.h | 16 +++++++++++++-- examples/sliderexamples.cpp | 39 ++++++++++++++++++++++++++++++++++++- examples/sliderexamples.h | 3 +++ 4 files changed, 58 insertions(+), 5 deletions(-) diff --git a/components/slider.cpp b/components/slider.cpp index 74bb54d..03f2672 100644 --- a/components/slider.cpp +++ b/components/slider.cpp @@ -1,5 +1,6 @@ #include "slider.h" #include +#include #include #include @@ -49,8 +50,8 @@ void Slider::paintEvent(QPaintEvent *event) painter.drawRect(rect().adjusted(0, 0, -1, -1)); painter.setFont(QFont("monospace", 8)); - painter.drawText(8, 18, "Value: "); - painter.drawText(8, 36, "Position: "); + painter.drawText(8, 18, "Value: " % QString::number(value())); + painter.drawText(8, 36, "Position: " % QString::number(sliderPosition())); #endif } diff --git a/components/slider_p.h b/components/slider_p.h index 028c597..ac70358 100644 --- a/components/slider_p.h +++ b/components/slider_p.h @@ -3,6 +3,7 @@ #include "slider.h" #include +#include "lib/style.h" #define THUMB_OUTER_SIZE 30 @@ -60,10 +61,21 @@ public: { Q_Q(const Slider); + const int span = Qt::Horizontal == orientation + ? q->rect().width() - THUMB_OUTER_SIZE + : q->rect().height() - THUMB_OUTER_SIZE; + + const int pos = Style::sliderPositionFromValue( + q->minimum(), + q->maximum(), + q->value(), + span, + false); + return Qt::Horizontal == orientation - ? QRectF(0, q->rect().height()/2 - THUMB_OUTER_SIZE/2, + ? QRectF(pos, q->rect().height()/2 - THUMB_OUTER_SIZE/2, THUMB_OUTER_SIZE, THUMB_OUTER_SIZE) - : QRectF(q->rect().width()/2 - THUMB_OUTER_SIZE/2, 0, + : QRectF(q->rect().width()/2 - THUMB_OUTER_SIZE/2, pos, THUMB_OUTER_SIZE, THUMB_OUTER_SIZE); } diff --git a/examples/sliderexamples.cpp b/examples/sliderexamples.cpp index cf29192..141e8be 100644 --- a/examples/sliderexamples.cpp +++ b/examples/sliderexamples.cpp @@ -10,7 +10,9 @@ SliderExamples::SliderExamples(QWidget *parent) : ExampleList(parent), _edit(new QLineEdit), - _slider(new Slider) + _edit2(new QLineEdit), + _slider(new Slider), + _slider2(new Slider) { QLayout *mainLayout = widget()->layout(); @@ -96,6 +98,34 @@ SliderExamples::SliderExamples(QWidget *parent) connect(button, SIGNAL(pressed()), this, SLOT(flip())); } + { + QVBoxLayout *layout = new QVBoxLayout; + QWidget *widget = new QWidget; + widget->setLayout(layout); + widget->setMinimumWidth(350); + + QHBoxLayout *hLayout = new QHBoxLayout; + QPushButton *button = new QPushButton("Update value"); + hLayout->addWidget(_edit2); + hLayout->addWidget(button); + + _slider2->setMinimumWidth(250); + layout->addWidget(_slider2); + layout->addLayout(hLayout); + + ExampleView *view = new ExampleView; + view->setWidget(widget); + + Frame *frame = new Frame; + frame->setCodeSnippet( + "Slider *slider = new Slider;" + ); + frame->setWidget(view); + + mainLayout->addWidget(frame); + + connect(button, SIGNAL(pressed()), this, SLOT(updateSliderValue())); + } } SliderExamples::~SliderExamples() @@ -112,3 +142,10 @@ void SliderExamples::flip() _slider->setOrientation(Qt::Horizontal == _slider->orientation() ? Qt::Vertical : Qt::Horizontal); } + +void SliderExamples::updateSliderValue() +{ + int n = _edit2->text().toInt(); + qDebug() << n; + _slider2->setValue(n); +} diff --git a/examples/sliderexamples.h b/examples/sliderexamples.h index 890c7c6..92c565a 100644 --- a/examples/sliderexamples.h +++ b/examples/sliderexamples.h @@ -17,10 +17,13 @@ public: protected slots: void updateValue(int value); void flip(); + void updateSliderValue(); private: QLineEdit *const _edit; + QLineEdit *const _edit2; Slider *const _slider; + Slider *const _slider2; }; #endif // SLIDEREXAMPLES_H