Results 1 to 8 of 8

Thread: QPaint Class and QGraphicsScene ....I am stuck !!

  1. #1
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QPaint Class and QGraphicsScene ....I am stuck !!

    Hi,

    I have written a program to show some buttons and use QGraphicsscene and paint class but when i run this program the output is only a 'WHITE BLANK SCREEN '

    Below is the implementation of this class and main function,any help/suggestion would be welcome.....pls give it a look !!

    Qt Code:
    1. int main(int argc,char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. paintClass paint;
    6. paint.addScene();
    7. paint.setIcons();
    8. paint.drawBGround();
    9. paint.show();
    10.  
    11.  
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #ifndef PAINTCLASS_H
    2. #define PAINTCLASS_H
    3.  
    4. #include <QGraphicsScene>
    5. #include <QGraphicsView>
    6.  
    7. #include <QGraphicsItem>
    8. #include <QGraphicsEllipseItem>
    9.  
    10. #include <QCursor>
    11. #include <QRectF>
    12.  
    13. #include <QPushButton>
    14. #include <QGraphicsProxyWidget>
    15.  
    16. #include <QIcon>
    17.  
    18. #include <QVBoxLayout>
    19.  
    20. #include <QPainter>
    21. #include <QWidget>
    22.  
    23. class paintClass:public QPainter,public QGraphicsView
    24. {
    25. public:
    26. QRectF r1;
    27. QRectF r2;
    28. QGraphicsItem *parent;
    29. QPainter painter;
    30. QWidget *widg ;
    31. QPushButton *stopButton;
    32. QPushButton *playButton;
    33. QHBoxLayout *hLayout;
    34. QGraphicsProxyWidget *proxy;
    35.  
    36. paintClass(QGraphicsView *parent = 0);
    37. void drawBGround();
    38. void addScene();
    39. void setIcons();
    40. };
    41.  
    42. #endif
    To copy to clipboard, switch view to plain text mode 



    Qt Code:
    1. #include "paintClass.h"
    2.  
    3. paintClass::paintClass(QGraphicsView *parent):QGraphicsView(parent)
    4. {
    5. r1.setRect(100,200,100,160);
    6. r2.setRect(0,0,300,300);
    7. widg = new QWidget;
    8. stopButton = new QPushButton;
    9. playButton = new QPushButton;
    10. hLayout = new QHBoxLayout;
    11. }
    12.  
    13. void paintClass::drawBGround()
    14. {
    15. painter.setPen(Qt::black);
    16. scene.addText("Hello, world!");
    17. scene.setBackgroundBrush(Qt::blue);
    18. }
    19.  
    20. void paintClass::addScene()
    21. {
    22. scene.addText("Hello, world!");
    23.  
    24. hLayout->addWidget(stopButton);
    25. hLayout->addWidget(playButton);
    26. widg->setLayout(hLayout);
    27. scene.addWidget(widg);
    28. }
    29.  
    30. void paintClass::setIcons()
    31. {
    32. playButton->setIcon(QIcon("./pics/play.JPG"));
    33. stopButton->setIcon(QIcon("./pics/stop.JPG"));
    34.  
    35. QGraphicsView view(&scene);
    36. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 28th May 2008 at 16:16. Reason: missing [code] tags

  2. #2
    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: QPaint Class and QGraphicsScene ....I am stuck !!

    The code above is not very well structured. It almost looks like you are randomly trying things. You should start with going through the Qt tutorial. Then, you can take a look at graphics view examples.
    J-P Nurmi

  3. #3
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPaint Class and QGraphicsScene ....I am stuck !!

    There is a new Graphics View webcast from ICS Network. It is available for viewing online, or video download. You can find it at: <http://www.ics.com/icsnetwork/>. There is also a brand new thread for the webcast here at QtCenter: <http://www.qtcentre.org/forum/f-icsnetwork-25.html>.

    This is a good place to start learning about Qt's Graphics View framework, starting with a simple "hello world" program and advancing to animation and optimization.

  4. #4
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPaint Class and QGraphicsScene ....I am stuck !!

    Hi..
    thanks for the reply ..i have just starting streaming the video..i hope it would be helpful...any ways ,the problem with this code is more with the class structure i think (as mentioned by jpn ),initialy i was trying these things in the main function and all was working nice but then i had to use QT'S drawBackground() function which is virtual and protected so i had to create a class in which i could be using it...so...the code in main which looks like this works fine...

    Qt Code:
    1. #include <QApplication>
    2. #include <QGraphicsScene>
    3. #include <QGraphicsView>
    4.  
    5. #include <QGraphicsItem>
    6. #include <QGraphicsEllipseItem>
    7.  
    8. #include <QCursor>
    9. #include <QRectF>
    10.  
    11. #include <QPushButton>
    12. #include <QGraphicsProxyWidget>
    13.  
    14. #include <QIcon>
    15.  
    16. #include <QVBoxLayout>
    17.  
    18. #include <QPainter>
    19.  
    20. #include "paintClass.h"
    21.  
    22. int main(int argc,char *argv[])
    23. {
    24. QApplication app(argc, argv);
    25.  
    26.  
    27. const QRectF r1(100,200,100,160);
    28. const QRectF r2(0,0,300,300);
    29.  
    30.  
    31. QGraphicsItem *parent = 0;
    32. QGraphicsEllipseItem ellipseItem(r1,parent);
    33. ellipseItem.setFlag(QGraphicsItem::ItemIsFocusable,true);
    34. ellipseItem.setSelected(true);
    35.  
    36. ellipseItem.setVisible(true);
    37.  
    38. scene.addItem(&ellipseItem);
    39.  
    40. scene.addText("Hello, world!");
    41.  
    42. QWidget *widg = new QWidget;
    43. QPushButton *stopButton = new QPushButton;
    44. stopButton->setIcon(QIcon("C:/pics/stop.JPG"));
    45.  
    46. QPushButton *playButton = new QPushButton;
    47. playButton->setIcon(QIcon("C:/pics/play.JPG"));
    48.  
    49. QHBoxLayout *hLayout = new QHBoxLayout;
    50. hLayout->addWidget(stopButton);
    51. hLayout->addWidget(playButton);
    52. widg->setLayout(hLayout);
    53. QGraphicsProxyWidget *proxy = scene.addWidget(widg);
    54.  
    55. paintClass *paint ;
    56. paint->drawBGround(&scene);
    57.  
    58. QGraphicsView view(&scene);
    59. view.show();
    60.  
    61. return app.exec();
    62. }
    To copy to clipboard, switch view to plain text mode 
    ..and in my first post the code is almost same except that (i think so )the class is not properly called...any advice would be welcomed...thanks again ...!!1
    Last edited by jpn; 29th May 2008 at 10:56. Reason: missing [code] tags

  5. #5
    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: QPaint Class and QGraphicsScene ....I am stuck !!

    Quote Originally Posted by salmanmanekia View Post
    the code in main which looks like this works fine...
    Qt Code:
    1. paintClass *paint ;
    2. paint->drawBGround(&scene);
    To copy to clipboard, switch view to plain text mode 
    Are you sure?
    J-P Nurmi

  6. #6
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPaint Class and QGraphicsScene ....I am stuck !!

    sorry man ...i forgot to comment it...!!...you know , for now there is no paintClass ..so ...
    // 1. paintClass *paint ;
    // 2. paint->drawBGround(&scene);
    and everything works fine....

  7. #7
    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: QPaint Class and QGraphicsScene ....I am stuck !!

    So, have you looked at graphics view examples and did you see the webcast as proposed? It makes no sense inherit something from QPainter and QGraphicsView. Also, writing something like:
    Qt Code:
    1. {
    2. ...
    3. QGraphicsView view(&scene);
    4. }
    To copy to clipboard, switch view to plain text mode 
    has no effect at all. The QGraphicsView object gets immediately destructed, according to normal C++ rules.

    I would recommend taking one of the graphics view examples as a base. That would give you a properly constructed base where you can start adding your own things.
    J-P Nurmi

  8. The following user says thank you to jpn for this useful post:

    salmanmanekia (30th May 2008)

  9. #8
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QPaint Class and QGraphicsScene ....I am stuck !!

    yes i have taken look at webcast ....what i think is that ,that i can understand the graphic related api in qt,the problem lies in how to manage classes ,in this case its graphics ...it could have been a network related api and i would have the same problem...
    still i understand that this statement executes and ends at the same time...so no effect at all
    1. {
    2. ...
    3. QGraphicsView view(&scene);
    4. }

    can you guide me to some good resource/tutorial for the class heirachy like how inheritance works in qt ...also if you can pinpoint the wrong things/concept which i have in my code ,so that i can look into solution to that..!!!!..because i am still not crystal clear that wht the problem is what exactly is the way to solve it...thanks again....

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.