[英]Changing the position of qlabel dynamically
I am a newbie in QT programming and I have a question. 我是QT编程的新手,我有一个问题。 I have a qslider and qlabel and a video playing. 我有一个qslider和qlabel以及一个视频播放。 While video is playing, tick of qslider is moving forward as you can guess. 在播放视频时,qslider的标记正在向前推进,您可以猜到。 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... 我想要做的是在qslider的勾号下放置一个qlabel,并在qlabel的文本正在更新的时候用勾号移动它...
You can't 'move' QLavel all over the widget due to the Qt layout system : the widget exists inside a layout. 由于Qt布局系统,您无法在整个窗口小部件中“移动”QLavel:窗口小部件存在于布局中。 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
): 最简单的方法是使用QLabel::setIndent
函数(它将在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. 如果标签显示文本,则如果alignment()为Qt :: AlignLeft,则缩进应用于左边缘;如果alignment()为Qt :: AlignRight,则缩进应用于右边缘,如果alignment()为Qt :: AlignTop,则缩写应用于上边缘如果alignment()是Qt :: AlignBottom,则到底边。
Also you can try this way (it will 'move' QLabel
itself): 你也可以尝试这种方式(它将'移动' QLabel
本身):
QHBoxLayout
or QVBoxLayout
). 创建框布局( QHBoxLayout
或QVBoxLayout
)。 QBoxLayout::addStretch
). 添加一个拉伸(调用QBoxLayout::addStretch
)。 QLayout::addWidget
). 添加QLabel(调用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: 模拟QLabel从左向右移动的代码示例:
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. 另一种变体适用于你的情况是继承QSlider
,然后重新实现受保护的函数QWidget::paintEvent
,以便在滑块上绘制一些标签。
First you can try QLabel.setIndent(self, int), call it whenever the tick changes. 首先,你可以尝试QLabel.setIndent(self,int),每当tick变化时调用它。 If this works for you it's the easiest possibility, if not please repost. 如果这适合你,这是最简单的可能性,如果不是请转发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.