Results 1 to 10 of 10

Thread: Smooth text scrolling

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Default Re: Smooth text scrolling

    Quote Originally Posted by wysota View Post
    Why don't you just write a short (up to 30 lines of code) example demonstrating the problem?
    Well, here is a quick example about what i told.
    I made a main window with a QTextEdit and a PushButton, let's try to copy and paste some text and click "start"
    In this case I used a QGraphicsTextItem (as suggested by wysota) a timer and the "translate" method to arrange the scrolling.
    The result (at least on my computer) flickers and isn't smooth.
    Ok, this is a real simple example, but starting from this code, how can i improve the results?
    Or...should I chose a totally different way?

    Qt Code:
    1. //file: mainwindow.h
    2. #ifndef MAINWINDOW_H
    3. #define MAINWINDOW_H
    4.  
    5. #include <QtGui/QApplication>
    6. #include <QMainWindow>
    7. #include <QGraphicsTextItem>
    8. #include <QPushButton>
    9. #include <QTextEdit>
    10. #include <QGraphicsView>
    11. #include <QGraphicsTextItem>
    12. #include <QTimeLine>
    13. #include <QTimer>
    14. #include <QTime>
    15. #include <QRect>
    16. #include <QPushButton>
    17. #include <QVBoxLayout>
    18.  
    19. class MainWindow : public QMainWindow
    20. {
    21. Q_OBJECT
    22.  
    23. public:
    24. explicit MainWindow(QWidget *parent = 0) : QMainWindow(parent){
    25. QVBoxLayout* layout = new QVBoxLayout();
    26.  
    27. pushButton = new QPushButton("start");
    28. textEdit = new QTextEdit();
    29. layout->addWidget(textEdit);
    30. layout->addWidget(pushButton);
    31. QWidget* central = new QWidget();
    32. central->setLayout(layout);
    33. setCentralWidget(central);
    34. connect(pushButton, SIGNAL(clicked()),SLOT(start()));
    35. }
    36.  
    37. ~MainWindow(){
    38.  
    39. }
    40.  
    41. QPushButton* pushButton;
    42. QTextEdit* textEdit;
    43. QTime* timeSinceLastUpdate;
    44.  
    45.  
    46. public slots:
    47.  
    48. void start(){
    49. scene = new QGraphicsScene();
    50. text = new QGraphicsTextItem();
    51. text->setDocument(textEdit->document());
    52. text->scale(6,6);
    53. text->setPos(-100,0);
    54. text->setTextWidth(140);
    55.  
    56. scene->addItem(text);
    57. scene->setSceneRect(0,0,400,400);
    58. view = new QGraphicsView(scene);
    59. view->fitInView(view->sceneRect(),Qt::KeepAspectRatio);
    60. view->setRenderHint(QPainter::SmoothPixmapTransform);
    61. view->showFullScreen();
    62.  
    63. QTimer* sctT = new QTimer();
    64. connect(sctT,SIGNAL(timeout()),SLOT(scroll()));
    65.  
    66. sctT->start(30);
    67.  
    68. timeSinceLastUpdate = new QTime();
    69. timeSinceLastUpdate->start();
    70. }
    71.  
    72. void scroll(){
    73. int time = timeSinceLastUpdate->elapsed(); //make the scrolling indipendent from frame rate
    74. float scrollValue = (40.0 * float(time))/1000.0;
    75. text->translate(0,-scrollValue);
    76. timeSinceLastUpdate->restart();
    77.  
    78. }
    79. };
    80.  
    81.  
    82.  
    83.  
    84. #endif // MAINWINDOW_H
    85.  
    86. file: main.cpp
    87.  
    88. #include <QtGui/QApplication>
    89.  
    90. #include "mainwindow.h"
    91.  
    92. int main(int argc, char *argv[])
    93. {
    94. QApplication a(argc, argv);
    95. MainWindow w;
    96. w.show();
    97.  
    98. return a.exec();
    99. }
    To copy to clipboard, switch view to plain text mode 

    Thank for the support!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Smooth text scrolling

    The animation is fluent on my machine but you can try this:

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MainWindow : public QMainWindow
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. explicit MainWindow(QWidget *parent = 0) : QMainWindow(parent){
    9. QVBoxLayout* layout = new QVBoxLayout();
    10.  
    11. pushButton = new QPushButton("start");
    12. textEdit = new QTextEdit();
    13. layout->addWidget(textEdit);
    14. layout->addWidget(pushButton);
    15. QWidget* central = new QWidget();
    16. central->setLayout(layout);
    17. setCentralWidget(central);
    18. connect(pushButton, SIGNAL(clicked()),SLOT(start()));
    19. }
    20.  
    21. QPushButton* pushButton;
    22. QTextEdit* textEdit;
    23.  
    24. public slots:
    25.  
    26. void start(){
    27. scene = new QGraphicsScene();
    28. text = new QGraphicsTextItem();
    29. text->setDocument(textEdit->document());
    30. text->scale(6,6);
    31. text->setPos(-100,0);
    32. text->setTextWidth(140);
    33.  
    34. scene->addItem(text);
    35. scene->setSceneRect(0,0,400,400);
    36. view = new QGraphicsView(scene);
    37. view->fitInView(view->sceneRect(),Qt::KeepAspectRatio);
    38. view->setRenderHint(QPainter::SmoothPixmapTransform);
    39. view->showFullScreen();
    40.  
    41. // the important part:
    42. QPropertyAnimation *anim = new QPropertyAnimation(text, "pos", text);
    43. anim->setStartValue(text->pos());
    44. anim->setEndValue(QPointF(-100, -1000));
    45. anim->setDuration(5000);
    46. anim->start();
    47. }
    48. };
    49.  
    50. #include "main.moc"
    51.  
    52. int main(int argc, char *argv[])
    53. {
    54. QApplication a(argc, argv);
    55. MainWindow w;
    56. w.show();
    57.  
    58. return a.exec();
    59. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Smooth Text Scrolling
    By Jones in forum Newbie
    Replies: 11
    Last Post: 19th November 2011, 18:40
  2. Replies: 1
    Last Post: 12th August 2010, 14:42
  3. Replies: 3
    Last Post: 25th May 2010, 10:29
  4. Replies: 2
    Last Post: 14th May 2010, 08:53
  5. "Smooth" Scrolling, QGraphicsView
    By nearlyNERD in forum Qt Programming
    Replies: 5
    Last Post: 25th February 2010, 17:18

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
  •  
Qt is a trademark of The Qt Company.