Results 1 to 9 of 9

Thread: [PyQt4] Smooth text scrolling in QLabel (again)

  1. #1
    Join Date
    Oct 2011
    Posts
    14
    Qt products
    Platforms
    Windows

    Default [PyQt4] Smooth text scrolling in QLabel (again)

    Hello,

    I'm trying to implement a QLabel with fixed width, which scrolls the text it shows if it's too long (as many multimedia players do, also known as Marquee effect).
    It's basically the same question as in this thread: http://www.qtcentre.org/threads/3005...Text-Scrolling,
    however I don't understand how I have to "alter QLabel and use QWidget::scroll() with a timer", which was the solution there.
    What I've tried so far is using a QTimer together with QLabel.setIndent(QLabel.indent() + 1), which works fine, but scrolls in the wrong direction (values < 0 are ignored so QLabel.indent() - 1 wouldn't work).
    So the question is how can I use the scroll function on the QLabel, because right now nothing happens with QLabel.scroll(x, y).

    Sidenote: I'm using PyQt 4.8.6-1.

    Thanks in advance,
    Jay-D

  2. #2
    Join Date
    Nov 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [PyQt4] Smooth text scrolling in QLabel (again)

    so far I've been at least able to get scrolling by sticking a QLabel inside a QWidget and then calling
    name_of_qwidget->scroll(-5,0) which makes it scroll to the left.
    I haven't been able to actually see any MORE text than what's shown in the first place, though. I also don't know how to monitor when the text is off the widget and out of sight
    here is some of my code

    mainwindow.h:
    #include <QTimer>

    private slots:
    void onScrollTimerTimeout();

    private:
    QTimer* m_scrollTimer;


    mainwindow.cpp

    m_scrollTimer=new QTimer();
    connect(m_scrollTimer,SIGNAL(timeout()),this,SLOT( onScrollTimerTimeout()));
    m_scrollTimer->start(500); //to start the timout process

    void MainWindow:nScrollTimerTimeout()
    {
    //scrolls -5 pixels every .5 seconds
    ui->scrollWidget->scroll(-5,0);
    m_scrollTimer->start(500); //reset the timer to 500 milliseconds (half a second)
    }



    HOWEVER here is some code you might want to look at and use
    http://qt-apps.org/content/show.php/...content=113690

    you can see it in action herehttp://www.youtube.com/watch?v=AirpRX5EPF4
    if I learn something after looking at the code for it I'll post it here

  3. #3
    Join Date
    Nov 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [PyQt4] Smooth text scrolling in QLabel (again)

    I changed WidgetMarqueeLabel.h and.cpp to the following.

    Qt Code:
    1. #ifndef _WIDGETMARQUEELABEL_H_
    2. #define _WIDGETMARQUEELABEL_H_
    3.  
    4. #include <QLabel>
    5. #include <QTimer>
    6.  
    7. class WidgetMarqueeLabel : public QLabel
    8. {
    9. Q_OBJECT
    10.  
    11. public: //Member Functions
    12. enum Direction{LeftToRight,RightToLeft};
    13. WidgetMarqueeLabel(QWidget *parent = 0,int spd=50,int dir=WidgetMarqueeLabel::RightToLeft,bool start=FALSE);
    14. ~WidgetMarqueeLabel();
    15. void show();
    16. void setAlignment(Qt::Alignment);
    17. int getSpeed();
    18.  
    19. public slots: //Public Member Slots
    20. void setSpeed(int s);
    21. void setDirection(int d);
    22. void updateCoordinates();
    23. protected: //Member Functions
    24. void paintEvent(QPaintEvent *evt);
    25. void resizeEvent(QResizeEvent *evt);
    26.  
    27.  
    28. private: //Data Members
    29. bool autostart;
    30. int px;
    31. int py;
    32. QTimer timer;
    33. Qt::Alignment m_align;
    34. int speed;
    35. int direction;
    36. int fontPointSize;
    37. int textLength;
    38.  
    39. private slots: //Private Member Slots
    40. void refreshLabel();
    41. };
    42. #endif /*_WIDGETMARQUEELABEL_H_*/
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "WidgetMarqueeLabel.h"
    2. #include <QPainter>
    3. #include <QDebug>
    4.  
    5. WidgetMarqueeLabel::WidgetMarqueeLabel(QWidget *parent,int spd,int dir,bool start)
    6. :QLabel(parent)
    7. {
    8. px = 0;
    9. py = 15;
    10. speed = spd;
    11. direction = dir;
    12. autostart=start;
    13. connect(&timer, SIGNAL(timeout()), this, SLOT(refreshLabel()));
    14. if (autostart==FALSE){
    15. if (fontMetrics().width(text())> width())
    16. timer.start(10);
    17. }
    18. else
    19. timer.start(10);
    20. }
    21.  
    22. void WidgetMarqueeLabel::refreshLabel()
    23. {
    24. if(direction==RightToLeft)
    25. {
    26. px -=1;
    27. if(px <= (-textLength))
    28. px = width();
    29. }
    30. else
    31. {
    32.  
    33. px +=1;
    34. if(px >= width())
    35. px = - textLength;
    36. }
    37. timer.start(speed);
    38. repaint();
    39. }
    40.  
    41. WidgetMarqueeLabel::~WidgetMarqueeLabel()
    42. {}
    43.  
    44. void WidgetMarqueeLabel::show()
    45. {
    46. QLabel::show();
    47. }
    48.  
    49. void WidgetMarqueeLabel::setAlignment(Qt::Alignment al)
    50. {
    51. m_align = al;
    52. updateCoordinates();
    53. QLabel::setAlignment(al);
    54. }
    55.  
    56. void WidgetMarqueeLabel::paintEvent(QPaintEvent *evt)
    57. {
    58. QPainter p(this);
    59.  
    60. p.drawText(px, py + fontPointSize, text());
    61. p.translate(px,0);
    62.  
    63. }
    64.  
    65. void WidgetMarqueeLabel::resizeEvent(QResizeEvent *evt)
    66. {
    67. updateCoordinates();
    68. QLabel::resizeEvent(evt);
    69. }
    70.  
    71. void WidgetMarqueeLabel::updateCoordinates()
    72. {
    73. px=0;
    74. switch(m_align)
    75. {
    76. case Qt::AlignTop:
    77. py = 10;
    78. break;
    79. case Qt::AlignBottom:
    80. py = height()-10;
    81. break;
    82. case Qt::AlignVCenter:
    83. py = height()/2;
    84. break;
    85. }
    86. fontPointSize = font().pointSize()/2;
    87. textLength = fontMetrics().width(text());
    88. if (textLength>width())
    89. timer.start(10);
    90. else{
    91. timer.stop();
    92. repaint();
    93. }
    94. }
    95.  
    96. void WidgetMarqueeLabel::setSpeed(int s)
    97. {
    98. speed = s;
    99. }
    100.  
    101. int WidgetMarqueeLabel::getSpeed()
    102. {
    103. return speed;
    104. }
    105.  
    106. void WidgetMarqueeLabel::setDirection(int d)
    107. {
    108. direction = d;
    109. if (direction==RightToLeft){
    110. px = width() - textLength;
    111.  
    112. }
    113. else
    114. px = 0;
    115. refreshLabel();
    116. }
    To copy to clipboard, switch view to plain text mode 

    I changed a few things, including the paintEvent function. The scroll was too fast the way it was, so I made sure it was setup to the timer
    that they had instead of just the paintEvent. I also added a few features, like only scrolling if the text amount is larger than the space.
    If the text changes in my MainWindow.cpp I emit a SIGNAL songChanged and connect it to WidgetMarqueeLabel's SLOT updateCoordinates;
    here is a snippet from my code:
    Qt Code:
    1. song=new WidgetMarqueeLabel(ui->centralWidget,1);
    2. song->setObjectName(QString::fromUtf8("song"));
    3. song->setGeometry(QRect(60, 10, 281, 21));
    4. connect(this,SIGNAL(songChanged()),song,SLOT(updateCoordinates()));
    5. song->setText("hello");
    6. song->setSpeed(100);
    7.  
    8. void MainWindow::onSourceChanged(Phonon::MediaSource source)
    9. {
    10. QMultiMap<QString,QString> tags=getTags(source);
    11. QString file;
    12. if(tags.value("TITLE").isEmpty())
    13. file=source.fileName().mid(source.fileName().lastIndexOf("/")+1,source.fileName().size());
    14. else
    15. file=tags.value("ARTIST")+"-"+tags.value("TITLE");
    16. song->setText(file);
    17. emit songChanged();
    18. global_index=sources.indexOf(source);
    19. populateList();
    20. }
    To copy to clipboard, switch view to plain text mode 
    There isn't any reason to include 1 as a speed in my instantiation of song but at least you can see me setting the speed.
    take a look at the examples in the zip file.
    I tried to make a plugin for this to Designer. I was able to get Designer to display the widget, but as soon as I went to click on it it would crash Designer/Qt Creator. So I'm stuck with programatically adding a WidgetMarqueeLabel. If you have any info on this feel free to share cw9000@hotmail.com

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: [PyQt4] Smooth text scrolling in QLabel (again)

    After a brief look, I would advice to cache the text in a pixmap and draw that. It should be much faster. Also do not use repaint(). Use update().

  5. #5
    Join Date
    Apr 2012
    Posts
    4

    Default Re: [PyQt4] Smooth text scrolling in QLabel (again)

    Hi,
    I wondered if it was possibile to reappear before the text that reaches the end of the last letter on the right:
    I want something like this: for example (white space are 25):

    Qt Code:
    1. WidgetMarqueeLabel
    2. Wi dgetMarqueeLabel
    3. tMarqueeLabel Widge
    4. eLabel WidgetMarque
    5. el WidgetMarqueeLa
    6. WidgetMarqueeLabel
    To copy to clipboard, switch view to plain text mode 

    Is possibile?

    thanks
    Last edited by enaud; 17th May 2012 at 22:22.

  6. #6
    Join Date
    Apr 2012
    Posts
    4

    Default Re: [PyQt4] Smooth text scrolling in QLabel (again)

    up nothing here?

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: [PyQt4] Smooth text scrolling in QLabel (again)

    Ehm, yes, it is possible. What exactly is the problem?

  8. #8
    Join Date
    Apr 2012
    Posts
    4

    Default Re: [PyQt4] Smooth text scrolling in QLabel (again)

    Look here: I no want wait the end of text for restart text at beginning..
    But i want restart text before the beginning of screen.

    Quote Originally Posted by enaud View Post
    Hi,
    I wondered if it was possibile to reappear before the text that reaches the end of the last letter on the right:
    I want something like this: for example (white space are 25):

    Qt Code:
    1. WidgetMarqueeLabel
    2. Wi dgetMarqueeLabel
    3. tMarqueeLabel Widge
    4. eLabel WidgetMarque
    5. el WidgetMarqueeLa
    6. WidgetMarqueeLabel
    To copy to clipboard, switch view to plain text mode 

    Is possibile?

    thanks

  9. #9
    Join Date
    Sep 2014
    Posts
    1
    Qt products
    Qt3 Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: [PyQt4] Smooth text scrolling in QLabel (again)

    Hi enaud,

    Since I'm on something realy simillar to your implemetation, did you make this Marquee effect happen in the end?

    thanks.

Similar Threads

  1. Smooth Text Scrolling
    By Jones in forum Newbie
    Replies: 11
    Last Post: 19th November 2011, 19:40
  2. Smooth text scrolling
    By francesco.frankie in forum Newbie
    Replies: 9
    Last Post: 29th November 2010, 23:16
  3. Replies: 1
    Last Post: 12th August 2010, 15:42
  4. Replies: 3
    Last Post: 25th May 2010, 11:29
  5. "Smooth" Scrolling, QGraphicsView
    By nearlyNERD in forum Qt Programming
    Replies: 5
    Last Post: 25th February 2010, 18:18

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.