set correct slider thumb position from current value

This commit is contained in:
laserpants 2016-05-02 23:35:04 +03:00
parent d68dd2e6cf
commit 07720c5f66
4 changed files with 58 additions and 5 deletions

View File

@ -1,5 +1,6 @@
#include "slider.h"
#include <QPainter>
#include <QStringBuilder>
#include <QMouseEvent>
#include <QDebug>
@ -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
}

View File

@ -3,6 +3,7 @@
#include "slider.h"
#include <QPainter>
#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);
}

View File

@ -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);
}

View File

@ -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