Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Animate QLabel text

    General and Desktop
    3
    4
    2939
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      Anonymous last edited by

      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)!

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • A
          Anonymous last edited by

          Oke sorry...

          1 Reply Last reply Reply Quote 0
          • D
            dscyw last edited by

            This maybe helpful.It is Chinese."RollLabel":http://download.csdn.net/download/lh806732/6262947

            1 Reply Last reply Reply Quote 0
            • First post
              Last post