Results 1 to 6 of 6

Thread: QGraphicsWidgets erased slowly

  1. #1
    Join Date
    Mar 2019
    Posts
    7
    Thanks
    1
    Qt products
    Qt5

    Default QGraphicsWidgets erased slowly

    Hi,

    I'm working on a QGraphicsScene application in Qt 5,12. In my scene, I have static and dynamic elements. I have two lists of texts on the two side of my screen, as sources and targets. In between them, I have dynamic circle elements which make a trajectory between the source and the target based on the time.

    My text item is a QGraphicsWidget in order to be able to stock them in a layout. I overrode the paint function of the QGraphicsWidget in which I call QGraphicsWidget:aint and I also draw a text using QPainter::drawText.

    My circle item inherits from QGraphicsEllipseItem and it has two pointers to its source and destination.

    By using a QTimer I update the position of my dynamic items so that they are moving from the source to the target.

    My problem is that slowly my texts become erased as the circles pass. Please look at the attached screenshot.

    I found a workaround to solve this problem by calling update on all of my text item when the timer times out.

    Another solution I found is to subclass QgraphicsLayoutItem instead of subclassing the QGraphicsWidget.

    Could you please someone explain me why in the first case my texts become erased?

    Thanks
    Attached Images Attached Images

  2. #2
    Join Date
    Mar 2019
    Posts
    7
    Thanks
    1
    Qt products
    Qt5

    Default Re: QGraphicsWidgets erased slowly

    I have noticed that also when I resize my widow my texts are corrected, due to the update event.

    I've tried to change the ZValue of my elements because I thought that my texts are not visible due to a circle which masks them but even setting the ZValue does not solve the problem.

  3. #3
    Join Date
    Mar 2019
    Posts
    7
    Thanks
    1
    Qt products
    Qt5

    Default Re: QGraphicsWidgets erased slowly

    Does somebody has an idea , I still could not figure it out?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QGraphicsWidgets erased slowly

    Without seeing your actual code, or a small, self-contained example that shows the problem it is hard to say. Not sure why you are using a custom painter for the text

  5. #5
    Join Date
    Mar 2019
    Posts
    7
    Thanks
    1
    Qt products
    Qt5

    Default Re: QGraphicsWidgets erased slowly

    Thank you for your response.

    I cannot post my code because it is too large but I wrote a small self-contained example to indicate the problem.

    Actually I'm using custom painter for the text because QGraphicsTextItem cannot be inserted in a layout. Hence, first I decided to subclass QGraphicsWidget and override its paint function. With this solution I encountered the problem I've mentioned above. Here is the code:

    Qt Code:
    1. class GraphicsTextItem : public QGraphicsWidget
    2. {
    3. public:
    4. QString m_Name;
    5. QColor m_Color;
    6.  
    7. GraphicsTextItem(QGraphicsItem * parent = nullptr, const QString& name = QString())
    8. : QGraphicsWidget(parent)
    9. , m_Name(name)
    10. , m_Color(50 + (std::rand() % (255 - 50 + 1)), 50 + (std::rand() % (255 - 50 + 1)), 50 + (std::rand() % (255 - 50 + 1)))
    11. {
    12. setMaximumHeight(12);
    13. setMinimumHeight(12);
    14. }
    15.  
    16. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override
    17. {
    18. QFont font("Times", 10);
    19. painter->setFont(font);
    20. painter->setPen(m_Color);
    21. painter->drawText(0, 0, m_Name);
    22. }
    23. };
    24.  
    25. class GraphicsTextItem;
    26. class GraphicsCircleItem : public QGraphicsEllipseItem
    27. {
    28. private:
    29. const GraphicsTextItem* m_Source;
    30. bool m_SourceGood;
    31.  
    32. public:
    33. GraphicsCircleItem(const GraphicsTextItem* source)
    34. , m_Source(source)
    35. , m_SourceGood(false)
    36. {
    37. setBrush(QBrush(m_Source->m_Color));
    38. setPen(QPen(m_Source->m_Color));
    39. setRect(0, 0, 20, 20);
    40. }
    41.  
    42. bool isSourcePositionValid() const { return m_Source->pos().x() != 0.0 || m_Source->pos().y() != 0.0; }
    43.  
    44. void moveAndShow()
    45. {
    46. if(isSourcePositionValid() && m_SourceGood == false)
    47. {
    48. m_SourceGood = true;
    49. setPos(m_Source->scenePos());
    50. show();
    51. return;
    52. }
    53. else
    54. {
    55. if(!m_SourceGood)
    56. hide();
    57. }
    58.  
    59. if(m_SourceGood)
    60. {
    61. moveBy(1,6);
    62. }
    63. }
    64. };
    65.  
    66. int main(int argc, char *argv[])
    67. {
    68. QApplication a(argc, argv);
    69. MainWindow w;
    70.  
    71. QGraphicsWidget* layout_widget = new QGraphicsWidget();
    72. QGraphicsLinearLayout* layout = new QGraphicsLinearLayout(Qt::Vertical);
    73. layout_widget->setLayout(layout);
    74.  
    75. QGraphicsScene* scene = new QGraphicsScene(&w);
    76. scene->setItemIndexMethod(QGraphicsScene::NoIndex);
    77. scene->setBackgroundBrush(Qt::black);
    78. scene->addItem(layout_widget);
    79.  
    80. QGraphicsView* scene_view = new QGraphicsView(&w);
    81. scene_view->setScene(scene);
    82. scene_view->setSceneRect(0, 0, 1024, 768);
    83.  
    84. std::vector<GraphicsTextItem*> source_list;
    85. std::vector<GraphicsCircleItem*> message_list;
    86. for(int i = 0; i < 1000; i++)
    87. {
    88. std::string source = std::string("SourceName_") + std::to_string(i);
    89. GraphicsTextItem* source_item = new GraphicsTextItem(nullptr, QString(source.c_str()));
    90. layout->addItem(source_item);
    91. source_list.push_back(source_item);
    92.  
    93. GraphicsCircleItem* message_item = new GraphicsCircleItem(source_item);
    94. scene->addItem(message_item);
    95. message_list.push_back(message_item);
    96. }
    97.  
    98. QTimer timer;
    99. timer.callOnTimeout([&]()
    100. {
    101. static uint64_t counter = 0;
    102. if(counter == 500)
    103. {
    104. for(auto& item : message_list)
    105. {
    106. scene->removeItem(item);
    107. delete item;
    108. }
    109. message_list.clear();
    110.  
    111. for(auto& item : source_list)
    112. {
    113. GraphicsCircleItem* message_item = new GraphicsCircleItem(item);
    114. scene->addItem(message_item);
    115. message_list.push_back(message_item);
    116. }
    117.  
    118. counter = 0;
    119. }
    120.  
    121. for(auto& item : message_list)
    122. {
    123. item->moveAndShow();
    124. }
    125. counter++;
    126.  
    127. });
    128. timer.start(10);
    129.  
    130. w.setCentralWidget(scene_view);
    131. w.show();
    132.  
    133. return a.exec();
    134. }
    To copy to clipboard, switch view to plain text mode 

    I attach also a photo about the issue with the sample code. As I wrote before I have not encountered this problem if I subclass QGraphicsLayoutItem instead of QGraphicsWidget.

    Screenshot from 2019-04-25 13-17-55.jpg

    I hope the sample code helps.

  6. #6
    Join Date
    Mar 2019
    Posts
    7
    Thanks
    1
    Qt products
    Qt5

    Default Re: QGraphicsWidgets erased slowly

    Anybody could please advise?

Similar Threads

  1. why Qt5.5.1 qDebug so slowly?
    By tiaoweiliao in forum Qt Programming
    Replies: 1
    Last Post: 16th May 2016, 10:23
  2. QGraphicsWidgets on QGraphicsGridLayout
    By onurozcelik in forum Qt Programming
    Replies: 4
    Last Post: 4th April 2010, 13:38
  3. Replies: 0
    Last Post: 16th November 2009, 17:16
  4. very slowly apps
    By swiety in forum Qt Programming
    Replies: 2
    Last Post: 20th January 2008, 12:05

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.