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)

    !http://img849.imageshack.us/img849/6866/dfrq.png(i)!

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

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

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

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

    public slots:
    void setSpeed(int s);

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

    private:
    Ui::MainWindow *ui;

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

    };

    #endif // MAINWINDOW_H

    @

    .cpp
    @void MainWindow::refreshLabel()
    {
    repaint();
    }

    void MainWindow::setAlignment(Qt::Alignment al)
    {
    m_align = al;
    updateCoordinates();
    ui->label_6->setAlignment(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());
    p.translate(px,0);

    }

    void MainWindow::resizeEvent(QResizeEvent evt)
    {
    updateCoordinates();
    //ui->label_6->resizeEvent(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()
    {
    switch(m_align)
    {
    case Qt::AlignTop:
    py = 10;
    break;
    case Qt::AlignBottom:
    py = height()-10;
    break;
    case Qt::AlignVCenter:
    py = height()/2;
    break;
    }
    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...

    !http://img826.imageshack.us/img826/3433/eo1i.png(a)!


  • Lifetime Qt Champion

    Hi,

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

    "Duplicate":http://qt-project.org/forums/viewthread/36078/

    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.