Results 1 to 10 of 10

Thread: Smooth text scrolling

  1. #1

    Default Smooth text scrolling

    Hello!
    I'm writing a program that should render the text embedded in a QTextDocument on a QGraphicsView, the text must be scaled and smoothly scrolled.
    To do this job I've inherited the class QGraphicsItem and implemented the funciont paint, after the item is added to a QGraphicsScene that calls the function advance() every 30 milliseconds.

    here is the code of the paint function:


    Qt Code:
    1. void ScrollTextGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    2.  
    3. if(currentDocument){
    4.  
    5. painter->setRenderHint(QPainter::SmoothPixmapTransform);
    6.  
    7. painter->translate(0,-scrollValue);
    8.  
    9. painter->scale(renderScale.width(),renderScale.height());
    10.  
    11. QAbstractTextDocumentLayout::PaintContext pcontext;
    12. pcontext.palette.setColor(QPalette::Text, scrollScene->getDefaultTextColor());
    13.  
    14. pcontext.clip = QRectF(0,scrollValue/renderScale.height(),
    15. renderSize.width()/renderScale.width(),
    16. renderSize.height()/renderScale.height());
    17.  
    18. currentDocument->documentLayout()->draw(painter,pcontext);
    19.  
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 



    The matter is if the result is almost reasonable for light-weight documents and for low scale factor, the performances turn down even for a small portion of text (few lines) scaled and drawn in a window 800x600.
    Did i forget some optimization? Are there ways for improve the draw algorithm (for example using a QGLWidget)?
    Have you some hints or suggestions?


    thanks for the attention
    Regards

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

    Default Re: Smooth text scrolling

    Is there a reason why you didn't use QGraphicsTextItem?
    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.


  3. #3

    Default Re: Smooth text scrolling

    Thank you for your reply.
    Yes, I've tried it, but the performance problem persist.
    Do you have other advices?

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

    Default Re: Smooth text scrolling

    What is the performance problem? Please provide an example.
    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.


  5. #5

    Default Re: Smooth text scrolling

    OK, I've made some test. Although the CPU level is low, the scroll animation isn't smooth, indeed depends on the resolution of the window where the scene is placed. I think the matter is resize and scale the scene...
    For the animation I connect a timeout event of a timer with the function "advance" of the scene. The painter of the graphics item is moved by a number of pixel depends on the time elapsed sine last drawing.

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Smooth text scrolling

    That's still not an example.

    Here are a few:
    http://doc.qt.nokia.com/4.7/examples-animation.html

    I made a little test with a graphicsview and a qgraphicsproxywidget containing a label and using qpropertyanimation. The animation is smooth.

  7. #7

    Default Re: Smooth text scrolling

    thank you guys for the attention and the support, and sorry if i wasn't able to provide a clear question.
    My program should scroll in a full screen window a very large QTextDocument, about 5 lines in the same moment. I've tried several ways to perform the animation and render the text, but I wasn't able to obtain a real smooth and flicker-free animation.
    In your opinion, which is the best way to do that? I followed the Animation tutorial and read articles about QT Graphics View Framework, but I didn't find the answer.

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

    Default Re: Smooth text scrolling

    Why don't you just write a short (up to 30 lines of code) example demonstrating the problem?
    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.


  9. #9

    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!

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.