Reputation: 1114
I am a newbie in QT programming and I have a question. I have a qslider and qlabel and a video playing. While video is playing, tick of qslider is moving forward as you can guess. What I want to do is to put a qlabel just under the tick of qslider and moving it with the tick as the text of qlabel is updating...
Upvotes: 0
Views: 3710
Reputation: 321
You can't 'move' QLavel all over the widget due to the Qt layout system: the widget exists inside a layout. So if you want to 'move' your label just only vertically of horizontally there are few ways to solve your problem.
The easiest one is to use the QLabel::setIndent
function (it will 'move' text inside the QLabel
):
If a label displays text, the indent applies to the left edge if alignment() is Qt::AlignLeft, to the right edge if alignment() is Qt::AlignRight, to the top edge if alignment() is Qt::AlignTop, and to the bottom edge if alignment() is Qt::AlignBottom.
Also you can try this way (it will 'move' QLabel
itself):
QHBoxLayout
or QVBoxLayout
).QBoxLayout::addStretch
).QLayout::addWidget
).The initial stretch factor's value will define the initial position of the label.
The code example to simulate QLabel moving left to right:
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget* parent) : QWidget(parent)
{
layout_ = new QHBoxLayout(this);
layout_->addStretch();
layout_->addWidget(new QLabel("Moving text", this));
layout_->addStretch(100);
connect(&timer_, SIGNAL(timeout()), SLOT(moveLabel()));
timer_.start(1000);
}
public slots:
void moveLabel()
{
layout_->setStretch(0, layout_->stretch(0) + 1);
layout_->setStretch(1, layout_->stretch(1) - 1);
if(layout_->stretch(0) == 100)
timer_.stop();
}
private:
QBoxLayout* layout_;
QTimer timer_;
};
And another variant for your situation is to inherit QSlider
and then reimplement protected function QWidget::paintEvent
in order to paint some label over the slider.
Upvotes: 2
Reputation: 23550
First you can try QLabel.setIndent(self, int), call it whenever the tick changes. If this works for you it's the easiest possibility, if not please repost.
Upvotes: 0