How to move text in QLabel?
-
Hi,
I have long text in QLabel, which has width smaller than his text. Of course I don't see full text. For example if my text is "abcdefghij" I can only see in this QLabel "abc" and a half of letter "d". I would like to see other parts for example "bcd", so I would like to move text in QLabel. Is it possible?
-
@TomNow99
I don't think so. You can play about with moving/scrolling in aQLineEdit
, even a read-only one, but for aQlabel
I don't see how you could say "only starting displaying it from the second character onward", or similar. I think you would be responsible for changing the text to start frombcd
instead of fromabcd
to achieve what you ask for.You could try reading through https://doc.qt.io/qt-5/qtwidgets-widgets-elidedlabel-example.html. It's not the same as you want, but food for thought. You could probably borrow the idea of altering what it is you draw/paint from the label. But all a bit complex.
Or, as I said, you might change over to a read-only but not disabled
QLineEdit
, which would allow user to scroll through the text manually. Or, you could implement a "marquee"QLineEdit
so it constantly scrolls through its content for the user, but that's a different matter :) -
@JonB Thank you.
I find a strange solution to move text in QLabel - move a QLabel and have other QLabels, which will hidden that QLabel with text.
My code:
timer = new QTimer; connect(timer, SIGNAL(timeout()),this,SLOT(timeoutSlot())); text = new QLabel(this); labelLeft = new QLabel(this); labelRight = new QLabel(this); labelLeft->setStyleSheet("QLabel {background:rgb(240,240,240)}"); labelRight->setStyleSheet("QLabel {background:rgb(240,240,240)}"); text->setText("Random text, which will be display and move"); text->resize(300,50); labelLeft->resize(100,100); labelRight->resize(200,100); text->move(100,0); labelLeft->move(0,0); labelRight->move(200,0); timer->start(100);
and timeoutSlot:
void MainWindow::timeoutSlot() { int tmpPositionX = text->pos().x(); tmpPositionX-=1; text->move(tmpPositionX,0); if(tmpPositionX==-300) { timer->stop(); } }
Do you know better solution?
-
Hi,
There's a stack overflow thread about implementing a marquee which might be of interest.