⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

Animate QLabel text

  • Hello, I found some old source code and edited it... I want to achieve Maruqee effect - horizontal text scrolling inside label...

    Current code scrolling whole label and and outside of label... I don't know for what reason it's scrolling at the top of widget...

    How to solve error commented in code??? How to make text scrolling in line where label is placed??? How to scroll text in limited area??? (Not it scrolls through whole window -> left -to right)


    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include "mplayer.h"

    #include <QTimer>
    #include <QLabel>
    namespace Ui {
    class MainWindow;

    class MainWindow : public QMainWindow

    explicit MainWindow(QWidget *parent = 0);

    void setAlignment(Qt::Alignment);
    int getSpeed();

    public slots:
    void setSpeed(int s);

    void paintEvent(QPaintEvent *evt);
    void resizeEvent(QResizeEvent *evt);
    void updateCoordinates();
    void refreshLabel();

    Ui::MainWindow *ui;

    int px;
    int py;
    QTimer timers;
    Qt::Alignment m_align;
    int speed;
    int fontPointSize;
    int textLength;


    #endif // MAINWINDOW_H


    @void MainWindow::refreshLabel()

    void MainWindow::setAlignment(Qt::Alignment al)
    m_align = al;

    void MainWindow::paintEvent(QPaintEvent *evt)
    QPainter p(this);
    px += speed;
    if(px >= width())
    px = - textLength;
    p.drawText(px, py + fontPointSize, ui->label_6->text());


    void MainWindow::resizeEvent(QResizeEvent evt)
    //This line above does not work for some reason and can't figure out why... It gives me error "Can't access protected member declared in class QWidget... I think this might be a reason why text is scrolling at the top of the window...


    void MainWindow::updateCoordinates()
    case Qt::AlignTop:
    py = 10;
    case Qt::AlignBottom:
    py = height()-10;
    case Qt::AlignVCenter:
    py = height()/2;
    fontPointSize = font().pointSize()/2;
    textLength = fontMetrics().width(ui->label_6->text());

    void MainWindow::setSpeed(int s)
    speed = s;

    int MainWindow::getSpeed()
    return speed;

    Maybe if someone know better approach to achieve Maruqee scrolling... I found image of what one guy did in Qt and thats exactly what I would like to achieve...


  • Lifetime Qt Champion


    Please don't open the same question twice, update the original post with your new informations


    As for the comment it your code, the error is well explained, you are trying to call a protected function. To resize a widget the correct approach is to call resize.

  • Oke sorry...

Log in to reply