Results 1 to 9 of 9

Thread: 2 different custom paintevents are not working

  1. #1
    Join Date
    Jul 2014
    Posts
    32
    Qt products
    Qt5
    Platforms
    Windows

    Default 2 different custom paintevents are not working

    Hi,
    I have a strange problem. I have custom qlabel with a new paintevent function. This function will paint and connect points on a QLabel image. So far so good. This works, when I only use one label.

    But if I create another label or copy the code and rename "RenderArea" to "RenderArea2", it is not working. Painting inside the first label also paints in the second label. If I raise another window, e.g. taskmanager, the application crashed. Really weired.

    I hope you can understand my problem. Thanks.

    Qt Code:
    1. class RenderArea : public QLabel {
    2. Q_OBJECT
    3.  
    4. public:
    5. RenderArea(QWidget *parent = 0);
    6.  
    7. protected:
    8. void paintEvent(QPaintEvent *event);
    9.  
    10. private:
    11. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void RenderArea::paintEvent(QPaintEvent *event)
    2. {
    3. QPainter painter(this);
    4.  
    5. painter.setRenderHint(QPainter::SmoothPixmapTransform, true );
    6. painter.setRenderHint( QPainter::Antialiasing, true);
    7. painter.fillRect(event->rect(), QBrush(Qt::white));
    8. painter.translate(30, 30);
    9. painter.save();
    10. painter.restore();
    11.  
    12. QPen pen;
    13. pen.setColor(Qt::green);
    14. pen.setWidth(4);
    15. pen.setJoinStyle(Qt::RoundJoin);
    16. pen.setCapStyle(Qt::RoundCap);
    17. painter.setPen(pen);
    18. QPoint p;
    19. p.setX(globalX-30);
    20. p.setY(globalY-30);
    21.  
    22. for(int i=0; i<pValues.count();i++)
    23. {
    24. painter.drawPoint(pValues.at(i));
    25. if(i > 0)
    26. {
    27. if(pValues.at(i).x() == pValues.at(i-1).x() && pValues.at(i).y() == pValues.at(i-1).y())
    28. {
    29. i++;
    30. }
    31. else
    32. {
    33. painter.drawLine(pValues.at(i-1), pValues.at(i));
    34. }
    35.  
    36.  
    37. }
    38.  
    39. }
    To copy to clipboard, switch view to plain text mode 

    pValues just stores the painted points so I can repaint them. The strange thing is that the paintevent from label1 paints also the in label2!
    I also get the error message "QWidget::repaint: Recursive repaint detected".

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: 2 different custom paintevents are not working

    That looks fairly ok (other than then obvious unnecessary save/restore).

    So you have two instances of that label?
    In a layout?
    With different data?

    Cheers,
    _

  3. #3
    Join Date
    Jul 2014
    Posts
    32
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: 2 different custom paintevents are not working

    Yes, two instances of the label. I also tried to use two different classes instead of instancing. Same result.

    I added them to the same layout. The data is different. But it seems that the paint event from "label1" is also applied on "label2". I try to provide the whole code in a simplified way so you can understand the problem better. Maybe it is something very simple.

  4. #4
    Join Date
    Jul 2014
    Posts
    32
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: 2 different custom paintevents are not working

    I attached a simple project.

    The window shows 2 large labels in white. Try painting on the first one. It will also paint onto the lower one.

    I guess that the problem here is that paintEvent will be triggered for every label. But it is strange that it also paints inside the 2nd label. When I paint onto the 2nd label nothing happens.

    ImageManipulate2.zip

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: 2 different custom paintevents are not working

    So you have two labels that draw the same data and you are surprised that they look identical?

    Cheers,
    _

  6. #6
    Join Date
    Jul 2014
    Posts
    32
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: 2 different custom paintevents are not working

    I found my mistake by trying it with another approach (promoting a custom class via the gui). I put all the globals into the custom label class and now every label has its own values. Damn i forgot about the the attributes of an objects.

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: 2 different custom paintevents are not working

    You know, I asked you if the two instances had different data.
    A reasonable approach would have been to check that before answering yes despite the facts pointing to the opposite.

    Cheers,
    _

  8. #8
    Join Date
    Jul 2014
    Posts
    32
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: 2 different custom paintevents are not working

    I thought they were different. At least I thought that the MousePos were Screen Positions. Because the coordinates are based on the widget coordinates they are also appearing in the 2nd widget.

    And in my last reply I already said that I did a mistake.

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: 2 different custom paintevents are not working

    Quote Originally Posted by jho View Post
    I thought they were different. At least I thought that the MousePos were Screen Positions. Because the coordinates are based on the widget coordinates they are also appearing in the 2nd widget.
    No, that's not the cause at all.
    What the data contains is totally irrelevant.

    You are using the same list of points, literally the exactly the same data.

    Cheers,
    _

Similar Threads

  1. custom table model is not working
    By aurora in forum Qt Programming
    Replies: 3
    Last Post: 8th May 2012, 20:02
  2. PaintEvents() don't erase content before call
    By sagirahmed in forum Qt Programming
    Replies: 3
    Last Post: 12th October 2010, 05:44
  3. Qt Designer Custom Widget Not Working
    By davidpiedra in forum Qt Programming
    Replies: 0
    Last Post: 19th February 2010, 18:36
  4. Qt Designer Custom Widget Not Working
    By davidpiedra in forum Qt Programming
    Replies: 0
    Last Post: 19th February 2010, 18:30
  5. Help with getting my custom model working
    By thomaspu in forum Qt Programming
    Replies: 19
    Last Post: 29th July 2007, 18: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.