Results 1 to 10 of 10

Thread: implementing drawForeGround

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

    Question implementing drawForeGround

    Hi,
    I'm quite new to Qt. I have a problem with drawing on a QGraphicsView. I have added a working example in the zip file:

    I create a MainWindow app in Designer, and add a GraphicsView to it. In the code from MainWindow.cpp I create a QGraphicsscene, draw 256 little green rectangles on it (the rectangles are an instance from my own made class Graphics, which implements QGraphicItem), and let the GraphicsViewer select the scene. And this all works fine.

    But now I want to change the color of a rectangle when clicked on it. I know that I have to use te mousepressedEvent (propably in the Graphics class), and that I maybe should use the drawForeGround methode. But I don't really get how that works Anyone who can help me, and help me on the right way?

    Thanx in advance,
    Wirloff
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: implementing drawForeGround

    You don't need to use drawForeground. You only need to reimplement the paint() method of your item class (which you probably already did) and mouse press event to modify the item when it's clicked.

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

    Default Re: implementing drawForeGround

    I'm afraid I don't really get it. This is my GraphicsItem code:

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

    I have read that there is no such thing as a "repaint"-method. So know I'm wondering how to call a kind of repaint from in mousePressEvent??

  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 drawForeGround

    As I said in your other thread, you have the update() method, that will just call paint !

  5. #5
    Join Date
    Apr 2007
    Posts
    12
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: implementing drawForeGround

    if you want to change the color of the item upon selection then restore the color again upon de-selection:
    Set ItemIsSelectable flag in Graphics::Graphics()
    Qt Code:
    1. setFlag(ItemIsSelectable);
    To copy to clipboard, switch view to plain text mode 
    then in Graphics::paint() change the color of the brush if the item is selected
    Qt Code:
    1. if (isSelected())
    2. painter->setBrush(Qt::black);
    3. else
    4. painter->setBrush(Qt::green);
    To copy to clipboard, switch view to plain text mode 

    if you want to change the color of the item upon selection forever:
    no flags, just reimplement mousePressEvent()
    Qt Code:
    1. void Graphics::mousePressEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. if (brushcolor == Qt::green)
    4. brushcolor = Qt::black;
    5. update();
    6. QGraphicsItem::mousePressEvent(event);
    7. }
    To copy to clipboard, switch view to plain text mode 
    then your Graphics::paint() should set the brush color:
    Qt Code:
    1. painter->setBrush(brushcolor);
    To copy to clipboard, switch view to plain text mode 
    and in your constructor:
    Qt Code:
    1. brushcolor = Qt::green;
    To copy to clipboard, switch view to plain text mode 
    also, don't forget to define brushcolor as member of Graphics class:
    Qt Code:
    1. private:
    2. ...
    3. Qt::GlobalColor brushcolor;
    To copy to clipboard, switch view to plain text mode 
    i tried both, and they worked fine with me. except the second method, two items are selected at a time. i think you have something strange in your bounding rect or i did something wrong.

    Regards,
    -Mustafa

  6. The following user says thank you to elsheikhmh for this useful post:

    Wirloff (10th April 2007)

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

    Default Re: implementing drawForeGround

    Thanx, I have got I working I understand everything now, except one thing: what does the next sentence do:

    Qt Code:
    1. QGraphicsItem::mousePressEvent(event); //in the paint method
    To copy to clipboard, switch view to plain text mode 

    Thanx again to all of you

  8. #7
    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 drawForeGround

    This sentence calls the default implementation of this method.
    Really useful, you don't have to handle all the possible events : just reimplement the one you are interested in, and let the default code handle the others ;-)

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

    Default Re: implementing drawForeGround

    Everything works very fine but now I have another (new) problem:

    When clicked on a rectangle, the main program should be able to know where there has been clicked. Is it possible to send a signal to the Scene from within eg: Item's mouseEvent???

  10. #9
    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 drawForeGround

    Well, items are not QObject, so you can't use signals / slots, unless you made a subclass which inherit QObject

    You can always have access to the scene pointer with scene() (only if you stored your item in the scene).

    QGraphicsMouseEvent has useful methods to obtain position : pos() -> item coordinates, scenePos() -> scene coordinates.

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: implementing drawForeGround

    If an item ignores an event, it will propagate to the scene and can be handled there. I don't know if it is exactly what you want... Maybe it'll be easier to call some method of the scene in item's mouse event handler instead?

Similar Threads

  1. Implementing scale-invariant QGraphicsItems
    By spud in forum Qt Programming
    Replies: 12
    Last Post: 13th January 2012, 06:18
  2. Implementing MenuBar AutoHide function
    By viking in forum Qt Programming
    Replies: 31
    Last Post: 1st April 2011, 21:17
  3. Implementing IME using QT
    By pmohod in forum Qt Programming
    Replies: 0
    Last Post: 16th February 2007, 13:22
  4. Implementing Resize funtionality
    By PiXeL16 in forum Qt Programming
    Replies: 2
    Last Post: 15th February 2007, 00:12
  5. Making 3D controls in QT, implementing a game loop in QT
    By smurrish in forum Qt Programming
    Replies: 10
    Last Post: 26th April 2006, 05:37

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.