Results 1 to 4 of 4

Thread: Implementing paint()

  1. #1
    Join Date
    Apr 2007
    Posts
    9
    Thanks
    3

    Default Implementing paint()

    Hi, I have a class named "Graphics" which is derived from "QGraphicsItem" and represents a small green rectangle:

    Qt Code:
    1. #include <QtGui>
    2. #include <QPainter>
    3. #include <iostream>
    4. #include <QColor>
    5.  
    6. #include "Graphics.hpp"
    7.  
    8. Graphics::Graphics(): color(qrand() % 256, qrand() % 256, qrand() % 256)
    9. {
    10. setCursor(Qt::OpenHandCursor);
    11. }
    12.  
    13. QRectF Graphics::boundingRect() const
    14. {
    15. return QRectF(0, 0, 25, 25);
    16. }
    17.  
    18. void Graphics::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    19. {
    20. Q_UNUSED(option);
    21. Q_UNUSED(widget);
    22. painter->setPen(Qt::NoPen);
    23. painter->setBrush(Qt::green);
    24. painter->drawRect(0, 0, 10, 20);
    25.  
    26. }
    27.  
    28. void Graphics::mousePressEvent(QGraphicsSceneMouseEvent *event)
    29. {
    30.  
    31. }
    To copy to clipboard, switch view to plain text mode 

    But now, when clicked on a rectangle, I want it's color to change to red. But I have read that there is no such thing as a "repaint" methode. Anoyne who can help me, and tell me how to get the clicked rectangle red? I have no idea what to do within the mouseEvent

  2. #2
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Implementing paint()

    Well, the method is quite simple

    You just have to use a variable to store your color, let's say bColor,
    set by default with Qt::green.

    1) In your mousePressEvent, you change this variable :
    Qt Code:
    1. bColor = Qt::red;
    2. update();
    To copy to clipboard, switch view to plain text mode 

    2) In your paint method
    Qt Code:
    1. painter->setBrush(bColor);
    To copy to clipboard, switch view to plain text mode 

    I think that should work.

  3. #3
    Join Date
    Apr 2007
    Posts
    9
    Thanks
    3

    Default Re: Implementing paint()

    Thanx, update() was the methode that I was searching for. But now I have an other (weird) problem. I have added a 3 little zipfiles with 3 screenshots, which can show you the problem:

    - When I click on a rectangle (I have 256 rectangels drawn next to each other on a GraphicsScene), not only thát rectangle is redrawn in red, but 2,5(!) rectangles are redrawn in red.

    -After coloring some rectangles red, if I then scroll to the right, all the rectangles that weren't visible yet outside the screen, also have changed color It's al quit obvious in the screens I added.

    The method setupGraphics() from class MainWindow looks like this:
    Qt Code:
    1. void MainWindow::setupGraphics()
    2. {
    3. QGraphicsScene *scene = new QGraphicsScene();;
    4. for (int i = 0; i < 256; ++i)
    5. {
    6. Graphics* item = new Graphics;
    7. item->setPos(i*12,12);
    8. scene->addItem(item);
    9. }
    10. m_ui.graphicsView->setScene(scene);
    11. }
    To copy to clipboard, switch view to plain text mode 

    This draws the 256 rectangles for the first time....

    Anyone who knows what I have to do so that only the rectangle which I have clicked on changes from color? Or what I'm doing wrong?? This is my code of the GraphicsItem again I have added a bool to control if an item has been clicked or not):

    Qt Code:
    1. #include <QtGui>
    2. #include <QPainter>
    3. #include <iostream>
    4. #include <QColor>
    5.  
    6. #include "Graphics.hpp"
    7.  
    8.  
    9. bool used;
    10.  
    11. Graphics::Graphics(): color(qrand() % 256, qrand() % 256, qrand() % 256)
    12. {
    13. used=false;
    14. }
    15.  
    16. QRectF Graphics::boundingRect() const
    17. {
    18. return QRectF(0, 0, 25, 25);
    19. }
    20.  
    21. void Graphics::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    22. {
    23. Q_UNUSED(option);
    24. Q_UNUSED(widget);
    25. painter->setPen(Qt::NoPen);
    26. if (used)
    27. {
    28. painter->setBrush(Qt::red);
    29. }
    30. else
    31. {
    32. painter->setBrush(Qt::green);
    33. }
    34. painter->drawRect(0, 0, 10, 20);
    35.  
    36. }
    37.  
    38. void Graphics::mousePressEvent(QGraphicsSceneMouseEvent *event)
    39. {
    40. if (!used)
    41. {
    42. used=true;
    43. }
    44. else
    45. {
    46. used=false;
    47. }
    48.  
    49. update();
    50.  
    51. }
    To copy to clipboard, switch view to plain text mode 

    Thanx in advance
    Attached Files Attached Files

  4. #4
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Implementing paint()

    Okay, I got it.

    When you call update() without specifying a clip rect, it may cause adjacent items to be repainted as well...and that would explain your issues.
    Read closely the docs about this method, it's well said

    To avoid this, call update() with a specified rect (the current item rect) :
    Qt Code:
    1. update( boundingRect() );
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to guilugi for this useful post:

    Wirloff (10th April 2007)

Similar Threads

  1. Using QGLWidget paint engine to draw regular widgtes?
    By high_flyer in forum Qt Programming
    Replies: 11
    Last Post: 9th October 2006, 12:06
  2. How to paint across/over multiple widgets?
    By richy in forum Qt Programming
    Replies: 3
    Last Post: 31st August 2006, 10:57
  3. Paint MainWindow Regions
    By pedros09 in forum Qt Programming
    Replies: 1
    Last Post: 5th May 2006, 09:43
  4. paint QTreeView item !!
    By mcenatie in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2006, 14:24
  5. PrimitiveElemet's Paint Area
    By derick in forum Qt Programming
    Replies: 1
    Last Post: 1st February 2006, 15:52

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.