Results 1 to 5 of 5

Thread: Create a marquee with QGraphicsScene

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2007
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Create a marquee with QGraphicsScene

    Hi all,

    I'm looking to create a marquee like the ones we could watch on TV, normally horizontal scrolls. What I'm having is a lot of flickering on the animation, and I would like to have other opinions because I'm a newbie on Qt Graphics.

    If I resize the window, the animation stops, is there a way to avoid that? I don't know if there is a way to have a thread for the scene.

    Here is the main code I'm using, but you can find attached all the project.

    Qt Code:
    1. Item::Item(QGraphicsItem * parent)
    2. : QGraphicsItem(parent)
    3. {
    4. setCacheMode(DeviceCoordinateCache);
    5. _boundingRect = QRectF(0, 0, 960, 220);
    6. _animationSpeed = 0.5;
    7. }
    8.  
    9. QRectF Item::boundingRect() const
    10. {
    11. return _boundingRect;
    12. }
    13.  
    14. void Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    15. QWidget *widget)
    16. {
    17. painter->setBrush(Qt::yellow);
    18. painter->drawRoundedRect(2, 2, 84, 84, 4, 4);
    19. painter->setBrush(QColor(50,50,50));
    20. painter->drawRoundedRect(90, 2, 84, 84, 4, 4);
    21. painter->drawRoundedRect(176, 2, 534, 84, 4, 4);
    22. painter->drawRoundedRect(712, 2, 245, 84, 4, 4);
    23. }
    24.  
    25. void Item::advance(int phase)
    26. {
    27. if (phase == 1)
    28. {
    29. moveBy(-(_animationSpeed), 0.0);
    30. }
    31. }
    32.  
    33. void Item::setAnimationSpeed(qreal value)
    34. {
    35. _animationSpeed = value;
    36. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. scene->setBackgroundBrush(Qt::black);
    8.  
    9. ui->graphicsView->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
    10. ui->graphicsView->setScene(scene);
    11. ui->graphicsView->setSceneRect(0,0,960,230);
    12.  
    13. for (int i = 0; i < 50; i++)
    14. {
    15. Item *item = new Item();
    16. item->setPos(item->boundingRect().width() * i, 0);
    17. item->setAnimationSpeed(ui->horizontalSlider->value() / 10.0);
    18. scene->addItem(item);
    19. _items.append(item);
    20. }
    21.  
    22. QTimer *timer = new QTimer();
    23. connect(timer, SIGNAL(timeout()), this, SLOT(timer_timeout()));
    24. timer->start(1);
    25.  
    26. _elapsedTimer = new QElapsedTimer();
    27. _elapsedTimer->start();
    28. }
    29.  
    30. MainWindow::~MainWindow()
    31. {
    32. delete ui;
    33. }
    34.  
    35. void MainWindow::timer_timeout()
    36. {
    37. if (_elapsedTimer->hasExpired(8))
    38. {
    39. _elapsedTimer->start();
    40. ui->graphicsView->scene()->advance();
    41. }
    42. }
    43.  
    44. void MainWindow::on_horizontalSlider_valueChanged(int value)
    45. {
    46. foreach(Item *i, _items)
    47. i->setAnimationSpeed(value / 10.0);
    48. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files
    Last edited by plitex; 31st May 2013 at 12:18.

Similar Threads

  1. Marquee QLabel
    By embitel in forum Qt-based Software
    Replies: 16
    Last Post: 4th June 2014, 08:50
  2. Qt in VS does not create an .exe
    By Nazgul in forum Installation and Deployment
    Replies: 7
    Last Post: 14th August 2012, 09:34
  3. Marquee selection for Qcheckboxes
    By hamid ghous in forum Qt Programming
    Replies: 3
    Last Post: 23rd February 2011, 13:12
  4. Marquee progress bar?
    By daviddoria in forum Newbie
    Replies: 2
    Last Post: 13th February 2010, 02:20
  5. Replies: 4
    Last Post: 1st May 2009, 11:00

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.