Results 1 to 12 of 12

Thread: GraphicsView Foreground

  1. #1
    Join Date
    Jun 2009
    Location
    India
    Posts
    143
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default GraphicsView Foreground

    Hi all,

    I have sub-classed graphics view and i have implemented "draw foreground" function ..I need to draw ellipse's on the view foreground...Provided my code for reference .

    Header File:

    Qt Code:
    1. #include <QGraphicsView>
    2.  
    3. class MyView: public QGraphicsView
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8.  
    9. MyView(QWidget *parent = 0);
    10.  
    11. protected:
    12.  
    13. void drawForeground(QPainter * painter, const QRectF & rect);
    14.  
    15.  
    16. };
    To copy to clipboard, switch view to plain text mode 


    Source File:

    Qt Code:
    1. #include <QtGui>
    2. #include <QX11Info>
    3.  
    4. #include "graphics.h"
    5.  
    6.  
    7. MyView::MyView(QWidget *parent) : QGraphicsView(parent)
    8.  
    9. {
    10.  
    11.  
    12. //--- Graphics View Properties
    13.  
    14. this->resize ( 768, 768 );
    15.  
    16.  
    17.  
    18. this->setStyleSheet("background-image: url(/aegean-120km.png)");
    19.  
    20. /*
    21.  
    22.   QGraphicsScene scene;
    23.  
    24.   scene.setSceneRect (0,0, 768, 768 );
    25.  
    26.   this->setScene ( &scene );
    27.  
    28. */
    29.  
    30. QColor color(Qt::yellow);
    31.  
    32. QBrush brush(color, Qt::SolidPattern);
    33.  
    34. this->setForegroundBrush(brush);
    35.  
    36.  
    37. this->show();
    38.  
    39.  
    40.  
    41.  
    42. }
    43.  
    44.  
    45. void MyView::drawForeground(QPainter * painter, const QRectF & rect)
    46. {
    47.  
    48. QColor color1(156, 189, 176,254);
    49.  
    50. painter->setPen(Qt::lightGray);
    51.  
    52. int x,y,w,h,i;
    53.  
    54. double centre_x=384, centre_y=384;
    55.  
    56. double step_size=38.5;
    57.  
    58. for(i=1;i<=20 ;i++)
    59. {
    60. x = centre_x - step_size * i ;
    61. y = centre_y - step_size * i ;
    62.  
    63. w=h=step_size * i * 2;
    64.  
    65. painter->drawEllipse(x,y,w,h);
    66. }
    67.  
    68. }
    To copy to clipboard, switch view to plain text mode 


    I didn't get any ellipse on the foreground ....By ref to manual Whether "draw foreground " function will be applicable only to QGraphicsScene ? how the draw foreground function will get called, automatically or any update mechanism need to be added in the code ...

    Please advise..
    Attached Images Attached Images

  2. #2
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: GraphicsView Foreground

    Did you try this: viewport()->update(); ?

    And yet remove this part:
    Qt Code:
    1. #
    2. QBrush brush(color, Qt::SolidPattern);
    3.  
    4. this->setForegroundBrush(brush);#
    5. QBrush brush(color, Qt::SolidPattern);
    6.  
    7. this->setForegroundBrush(brush);
    To copy to clipboard, switch view to plain text mode 
    Last edited by Rachol; 23rd June 2011 at 11:02.

  3. #3
    Join Date
    Jun 2009
    Location
    India
    Posts
    143
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: GraphicsView Foreground

    Quote Originally Posted by Rachol View Post
    Did you try this: viewport()->update(); ?
    I have tried the method suggested , it didnt have any impact

    viewport()->update()

    Any ideas?

  4. #4
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: GraphicsView Foreground

    Have you tried to draw backgroung using backgroundBrush and not using Stylesheet?
    A camel can go 14 days without drink,
    I can't!!!

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: GraphicsView Foreground

    Try calling base-class implementation QGraphicsView::drawForeground(painter,rect) after your drawing code.

  6. #6
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: GraphicsView Foreground

    I don't know if you have noticed, but I have modified my previous message. Remove this->setForegroundBrush(brush); and I am quite sure it will work.

  7. #7
    Join Date
    Jun 2009
    Location
    India
    Posts
    143
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: GraphicsView Foreground

    I have tried the follwing suggested by you people:

    Call the base class implementation after drawing code .....QGraphicsView::drawForeground(painter,rect) ...... Didnt get any drawing ouptut on graphics view....

    I have removed this "QBrush brush(color, Qt::SolidPattern); this->setForegroundBrush(brush);" .....

    Didnt get any output....

    Quote Originally Posted by mcosta View Post
    Have you tried to draw backgroung using backgroundBrush and not using Stylesheet?
    I haven't tried this ....Whether this will have an impact ?

  8. #8
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: GraphicsView Foreground

    Might do something with your problem

  9. #9
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: GraphicsView Foreground

    I tried this code

    Qt Code:
    1. Widget::Widget(QWidget *parent)
    2. : QGraphicsView(parent)
    3. {
    4. QPixmap px (":/images/bg.png");
    5. this->setBackgroundBrush (QBrush(px));
    6. }
    7.  
    8. Widget::~Widget()
    9. {
    10.  
    11. }
    12.  
    13. void Widget::drawForeground(QPainter *painter, const QRectF &rect)
    14. {
    15. // QColor color1(156, 189, 176,254);
    16.  
    17. painter->setPen(Qt::darkRed);
    18.  
    19. double centre_x=384, centre_y=384;
    20.  
    21. double step_size=38.5;
    22.  
    23. int x, y, h, w;
    24.  
    25. for(int i=1;i<=20 ;i++) {
    26. x = centre_x - step_size * i ;
    27. y = centre_y - step_size * i ;
    28.  
    29. w=h=step_size * i * 2;
    30.  
    31. painter->drawEllipse(x,y,w,h);
    32. }
    33.  
    34. }
    To copy to clipboard, switch view to plain text mode 
    This is the result
    gg.jpg
    A camel can go 14 days without drink,
    I can't!!!

  10. #10
    Join Date
    Jun 2009
    Location
    India
    Posts
    143
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: GraphicsView Foreground

    Quote Originally Posted by Rachol View Post
    Might do something with your problem
    Hi ,

    Thanks to your support ...

    Is my code is right completely ? Any changes need to be done....


    Added after 5 minutes:


    Quote Originally Posted by mcosta View Post
    I tried this code

    Qt Code:
    1. Widget::Widget(QWidget *parent)
    2. : QGraphicsView(parent)
    3. {
    4. QPixmap px (":/images/bg.png");
    5. this->setBackgroundBrush (QBrush(px));
    6. }
    7.  
    8. Widget::~Widget()
    9. {
    10.  
    11. }
    12.  
    13. void Widget::drawForeground(QPainter *painter, const QRectF &rect)
    14. {
    15. // QColor color1(156, 189, 176,254);
    16.  
    17. painter->setPen(Qt::darkRed);
    18.  
    19. double centre_x=384, centre_y=384;
    20.  
    21. double step_size=38.5;
    22.  
    23. int x, y, h, w;
    24.  
    25. for(int i=1;i<=20 ;i++) {
    26. x = centre_x - step_size * i ;
    27. y = centre_y - step_size * i ;
    28.  
    29. w=h=step_size * i * 2;
    30.  
    31. painter->drawEllipse(x,y,w,h);
    32. }
    33.  
    34. }
    To copy to clipboard, switch view to plain text mode 
    This is the result
    gg.jpg


    hi mcosta,

    surprise.....

    I have tried the same ......

    set the background brush and used the drawforeground......

    I didnt get any circle and image too....

    With stylesheet i could set the image and it is visible, but by background brush its not visible......

    How it can be........

    Are you drawing in a graphics Scene ......


    If you don't mind can i have your complete source code of main and header and i will try it in my system....


    Added after 13 minutes:


    hi mcosta,

    I have tried the code of yours too ....i didnt get any output.......

    Any problem specific to system or code ....
    Last edited by augusbas; 23rd June 2011 at 11:52.

  11. #11
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: GraphicsView Foreground

    Hi, my code use a Resource File

    gg.qrc
    Qt Code:
    1. <RCC>
    2. <qresource prefix="/images">
    3. <file>bg.png</file>
    4. </qresource>
    5. </RCC>
    To copy to clipboard, switch view to plain text mode 

    This is my .pro
    Qt Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2011-06-23T12:06:26
    4. #
    5. #-------------------------------------------------
    6.  
    7. QT += core gui
    8.  
    9. TARGET = gg
    10. TEMPLATE = app
    11.  
    12.  
    13. SOURCES += main.cpp\
    14. Widget.cpp
    15.  
    16. HEADERS += Widget.h
    17.  
    18. OTHER_FILES += \
    19. bg.png
    20.  
    21. RESOURCES += \
    22. gg.qrc
    To copy to clipboard, switch view to plain text mode 

    and my main
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QtGui/QGraphicsScene>
    3.  
    4. #include "Widget.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9.  
    10. Q_INIT_RESOURCE (gg);
    11.  
    12. scene.setSceneRect (0, 0, 768, 768);
    13.  
    14. Widget w;
    15. w.resize (768, 768);
    16. w.setScene (&scene);
    17.  
    18. w.show();
    19.  
    20. return a.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  12. The following user says thank you to mcosta for this useful post:

    augusbas (23rd June 2011)

  13. #12
    Join Date
    Jun 2009
    Location
    India
    Posts
    143
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: GraphicsView Foreground

    Hi mcosta,

    I tried your code and found working fine.....

    Its the problem with the Qgraphicsscene ....i.e drawforeground applies to the graphics scene ....

    Thanks to your support dude..

    if i need to set the foreground fully opaque ... I need to set through painter ?

    One more thing i found when a image is added in back ground , the complete view is having the replica of images in the background ...how can i have only one time a image in view...

Similar Threads

  1. QGraphicsView Draw Foreground
    By augusbas in forum Qt Programming
    Replies: 4
    Last Post: 22nd June 2011, 13:01
  2. QGLWidget Render Text in the foreground
    By arpspatel in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2010, 11:35
  3. How to keep window in foreground
    By szicsi in forum Qt Programming
    Replies: 5
    Last Post: 17th January 2008, 14:40
  4. Painting: How to control what's in the foreground?
    By Mister_Crac in forum Qt Programming
    Replies: 16
    Last Post: 8th May 2007, 13:00
  5. Updating Foreground for QGraphicsView
    By tts80 in forum Qt Programming
    Replies: 5
    Last Post: 4th January 2007, 15:28

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.