Results 1 to 11 of 11

Thread: Adding Rectangular overlay on graphics view

  1. #1
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Adding Rectangular overlay on graphics view

    To simplify my problem, lets assume that we have two different graphics views. Both of these views have the same scene applied to it. When the user clicks on a specific point in the first graphics view I want to draw a rectangle related to the point she clicked, but I want it to only be visible in that graphics view. So I'm assuming I can't just add a rectangle to the scene because it would be visible on both. I was looking into using QGraphicsView's foregroundBrush(), but I'm not sure if that is what I'm looking for either. Doesn't the foregroundBrush tile the brushes pixmap? Any ideas would be appreciated.

  2. #2
    Join Date
    Oct 2006
    Posts
    279
    Thanks
    6
    Thanked 40 Times in 39 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Adding Rectangular overlay on graphics view

    You probably want to override QGraphicsView::drawForeground().

  3. #3
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding Rectangular overlay on graphics view

    I'm having problems writing the code to override drawForeground (or any virtual protected function for that matter). Does anyone know if there is an example I can look at? I'm trying to go through the message boards but no luck so far.

    thanks for the tip spud...

  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: Adding Rectangular overlay on graphics view

    Here's a little example how to override QGraphicsView::drawForeground():

    Class declaration:
    Qt Code:
    1. class MyGraphicsView : public QGraphicsView
    2. {
    3. public:
    4. MyGraphicsView(QWidget* parent = 0);
    5. ...
    6.  
    7. protected:
    8. void drawForeground(QPainter* painter, const QRectF& rect);
    9. };
    To copy to clipboard, switch view to plain text mode 

    Implementation:
    Qt Code:
    1. void MyGraphicsView::drawForeground(QPainter* painter, const QRectF& rect)
    2. {
    3. painter->drawRect(...);
    4. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding Rectangular overlay on graphics view

    thanks j-p,

    I'm not seeing anything in the graphicsview's with:
    Qt Code:
    1. void myForm::drawForeground(QPainter *painter, const QRectF &rect)
    2. {
    3. QPen border(QColor(200,100,40));
    4. painter->setPen(border);
    5. painter->drawRect(10,10,100,300);
    6. }
    To copy to clipboard, switch view to plain text mode 
    It compiles, but I'm not seeing any rectangle being drawn. The other thing I'm worried about is that I currently have more then 1 graphics view in this class. I only want this drawForeground function to run for one of them, is that do-able? Also, is there a way to call this function whenever I need the rectangle to be updated?

  6. #6
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Adding Rectangular overlay on graphics view

    Both QGraphicsView and QGraphicsScene define a virtual drawForeground() function. If you reimplement the one in the scene, all views will see this foreground, but you can just reimplement it in a view to create a view-specific foreground.

    These functions draw in scene coordinates, so you just need to make sure that what you are drawing fits inside the scene rect. If your scene rect (see QGraphicsScene::sceneRect()) is defined as (-1000, -1000, 2000, 2000), you could do this to draw a transparent green overlay:

    void MyScene::drawForeground(QPainter *painter, const QRectF &exposed)
    {
    // 50% transparent green overlay
    painter->fillRect(-1000, -1000, 2000, 2000, QColor(0, 255, 0, 127));
    // or
    painter->fillRect(exposed, QColor(0, 255, 0, 127));
    }
    The exposed argument is an optimization for partial updates; you don't want to fill the whole scene it only parts of it are exposed.
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  7. #7
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding Rectangular overlay on graphics view

    How exactly am I supposed to call this function for a particular graphicsView? Is there a specific QPainter pointer that I need to pass as a parameter that is related to the specific graphicsView? I'm just unclear as to how and when this drawForeground function is being called.

  8. #8
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Adding Rectangular overlay on graphics view

    u need not call this function... it is called automatically.
    U just need to override it in ur scene class and do the drawing using the painter pointer provided

  9. #9
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding Rectangular overlay on graphics view

    I think I'm beginning to understand what you've all been talking about. But the QGraphicsView's that I'm using were all added to the same widget using Qt Designer. Am I supposed to write a class for each graphicsView that I added in designer? If so, how can I write a class for each individual graphicsView? Or am I still way of target?

  10. #10
    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: Adding Rectangular overlay on graphics view

    Qt Designer has a widget promotion feature. Subclass QGraphicsView, open context menu over the graphics view widget in the designer and choose "Promote to Custom Widget".
    J-P Nurmi

  11. #11
    Join Date
    Oct 2006
    Posts
    83
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Adding Rectangular overlay on graphics view

    Thanks everyone! I got it working using a variety of all your tips. Mainly I used the widget promotion in designer, and then subclassed QGraphicsView to do what I wanted.

Similar Threads

  1. Automatic scroll in graphics view
    By Morea in forum Qt Programming
    Replies: 1
    Last Post: 18th November 2006, 15:07

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.