Results 1 to 7 of 7

Thread: Delays in processing events

  1. #1
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Delays in processing events

    Hello,
    I just draw some rectangles in a painter and I want to be able to move the rectangles. The coordinates data are stored using a QAbstractItemModel.
    I have a paint code as below:

    void
    vadControlPolygon:aintEvent(QPaintEvent *event)
    {
    QPainter painter(this->viewport());
    painter.setWindow(0, 0, 100, 100);
    QStyleOptionViewItem option;
    painter.eraseRect(painter.viewport());
    // getchar() // with this line the drawings update!
    // les sommets
    for (int i=0 ; i<3 ; i++)
    this->itemDelegate()->paint(&painter, option, this->model()->index(i, 0));
    }

    the painting itself being delegated, as you can see.
    Now I code the mouse moving event processing as below:

    void
    vadControlPolygon::mouseMoveEvent(QMouseEvent *e)
    {
    // check if we are moving any rectangle
    if (!this->index.isValid())
    return;
    // update the model data
    this->model()->setData(this->index, this->model()->data(this->index).toInt() +
    (e->pos().x() - this->lastx) * 100 / this->width());
    QModelIndex idx(this->index.sibling(this->index.row(), 1));
    this->model()->setData(idx, this->model()->data(idx).toInt() +
    (e->pos().y() - this->lasty) * 100 / this->height());
    // emit that signal, which has been connected to the paintEvent() slot
    emit repaint(new QPaintEvent(QRect()));
    // remember the current pointer position
    this->lastx = e->pos().x();
    this->lasty = e->pos().y();
    }

    the mouse press and release event handling is not here but you can imagine what it does.

    So my problem is that the painting does not change BUT if I add some kind of delay in the paintEvent() code, such as a getchar() (see the code, the getchar() is commented), the painting is updated just after the getchar() has been executed. The new figure then corresponds to the current situation, that is after all the remaining mouse move events have been processed (it means that the figure is suddenly changed, the intermediate drawings do not appear).
    So could it be a problem of several events of the same type arriving in a delay too short for them to be processed? any such experience among you Qt users?

  2. #2
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Delays in processing events

    Quote Originally Posted by Caius Aérobus
    // emit that signal, which has been connected to the paintEvent() slot
    emit repaint(new QPaintEvent(QRect()));
    Hi,PaintEvent is not a slot, you cannot connect a signal to it. Try to call update() instead.

  3. #3
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Delays in processing events

    but the signal-slot connection is not the problem: it works! I have replace the emit command but update() but same result, the problem is not here.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Delays in processing events

    Well, I must say, the updating mechanism of yours is really wicked..

    All you really need is:
    Qt Code:
    1. // member variables:
    2. // QPoint offset;
    3. // QPersistentModelIndex index;
    4.  
    5. void RectView::mousePressEvent(QMouseEvent* e)
    6. {
    7. index = indexAt(e->pos());
    8. offset = index.data().toRect().topLeft() - e->pos();
    9. }
    10.  
    11. void RectView::mouseMoveEvent(QMouseEvent* e)
    12. {
    13. if (index.isValid())
    14. {
    15. QRect r = index.data().toRect();
    16. r.moveTopLeft(e->pos());
    17. r.translate(offset);
    18. model()->setData(index, r);
    19. }
    20. }
    21.  
    22. void RectView::mouseReleaseEvent(QMouseEvent* e)
    23. {
    24. }
    25.  
    26. void RectView::paintEvent(QPaintEvent* e)
    27. {
    28. QAbstractItemView::paintEvent(e);
    29. QPainter painter(viewport());
    30. setDirtyRegion(viewport()->rect());
    31.  
    32. for (int row = 0; row < model()->rowCount(); ++row)
    33. {
    34. for (int col = 0; col < model()->columnCount(); ++col)
    35. {
    36. QModelIndex index = model()->index(row, col, QModelIndex());
    37. itemDelegate()->paint(&painter, QStyleOptionViewItem(), index);
    38. }
    39. }
    40. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Delays in processing events

    Thanks very much for your help. ALL my problems (view update from model and delays) have been solved simply by adding the following line:
    setDirtyRegion(viewport()->rect());
    A last question: why keeping a persistent model index at the end of the move process??
    index = QPersistentModelIndex();
    Regards.

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Delays in processing events

    I'm not sure if it really matters in this case, but
    From QModelIndex docs:
    Model indexes can become invalid over time so they should be used immediately and then discarded. If you need to keep a model index over time use a QPersistentModelIndex.
    J-P Nurmi

  7. #7
    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: Delays in processing events

    It matters if items are added or removed as their coordinates change then. In this situation it would be required to use persistent indexes if an item could be added or removed while the mouse was being dragged.

    but the signal-slot connection is not the problem: it works! I have replace the emit command but update() but same result, the problem is not here.
    It works because emit xxx() is changed into xxx() (as "emit" just gets ignored by the compiler), so if you "emit" a function, it'll just get called. I guess moc could report such wicked behaviours (at least warn about them) but they are harmless. It just makes the code more "wicked"

Similar Threads

  1. QThread and QEventLoop - Idle Processing
    By kloffy in forum Newbie
    Replies: 14
    Last Post: 28th April 2011, 11:41
  2. QGraphicsView Mouse Events
    By tomf in forum Qt Programming
    Replies: 5
    Last Post: 29th July 2008, 15:03
  3. How to suppress user defined events in processEvents()
    By Artschi in forum Qt Programming
    Replies: 5
    Last Post: 5th July 2007, 10:17
  4. Replies: 4
    Last Post: 20th February 2007, 12:35

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.