Results 1 to 6 of 6

Thread: Understanding QGraphics

  1. #1
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Understanding QGraphics

    Hi,

    I'm practicing with QGraphics and it works ok when placed in the main but onces I try
    to use the same code in a main window when a button is clicked it doesn't work.

    This works...

    Qt Code:
    1. #include <QtGui>
    2. #include <QtCore>
    3.  
    4. int main( int argc, char **argv )
    5. {
    6. QGraphicsScene scene( QRect( -50, -50, 400, 200 ) );
    7. QGraphicsRectItem *rectItem = new QGraphicsRectItem(QRect(-25, 25, 200, 40 ), 0, &scene );
    8. rectItem->setPen( QPen( Qt::red, 3, Qt::SolidLine ) );
    9. rectItem->setBrush( Qt::green );
    10.  
    11. view.setScene( &scene );
    12. view.show();
    13. }
    To copy to clipboard, switch view to plain text mode 

    This doesn't work...


    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtGui>
    4. #include <QtCore>
    5.  
    6.  
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12. }
    13.  
    14. MainWindow::~MainWindow()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void MainWindow::on_pushButton_Draw_clicked()
    20. {
    21. QGraphicsScene scene( QRect( -50, -50, 400, 200 ) );
    22. QGraphicsRectItem *rectItem = new QGraphicsRectItem(QRect(-25, 25, 200, 40 ), 0, &scene );
    23. rectItem->setPen( QPen( Qt::red, 3, Qt::SolidLine ) );
    24. rectItem->setBrush( Qt::green );
    25.  
    26. view.setScene( &scene );
    27. view.show();
    28. }
    To copy to clipboard, switch view to plain text mode 

    Can someone tell me why the second example doen't work?

    Thanks a lot.
    Last edited by fs_tigre; 27th April 2012 at 17:21.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,327
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Understanding QGraphics

    Qt Code:
    1. void MainWindow::on_pushButton_Draw_clicked()
    2. {
    3. QGraphicsScene scene( QRect( -50, -50, 400, 200 ) );
    4. QGraphicsRectItem *rectItem = new QGraphicsRectItem(QRect(-25, 25, 200, 40 ), 0, &scene );
    5. rectItem->setPen( QPen( Qt::red, 3, Qt::SolidLine ) );
    6. rectItem->setBrush( Qt::green );
    7.  
    8. view.setScene( &scene );
    9. view.show();
    10. }
    To copy to clipboard, switch view to plain text mode 

    Here's a hint: What happens to your local variables "scene" and "view" after the slot exits? How is the lifetime of these variables different from those in your first example?

  3. #3
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Understanding QGraphics

    I do understand variable scopes (sort of) I'm just not too familiar with classes, what goes in the .h file etc. I do understand the problem with the variable dieing after the function finish executing I just dont know how to break it down.

    I tried adding the following two declaration to the .h file but nothing ... I know I know I'm an Id!@%^.
    QGraphicsScene scene;
    QGraphicsView view;
    Thanks a lot for your help and sorry about the silly questions.

  4. #4
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: Understanding QGraphics

    As Amleto told you -->here<--, it would be sensible to provide all the .h file. You should make scene and view a member of MainWindow. If this->scene works, then you have made that correctly.

    And you should decide where you ask your question. Doing that in different forums at around the same time (aka "cross posting") is frowned upon for a reason :-). I wait at least 24 hours for an answer before I try somewhere else.

  5. #5
    Join Date
    Apr 2012
    Posts
    10
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Understanding QGraphics

    And you should decide where you ask your question. Doing that in different forums at around the same time (aka "cross posting") is frowned upon for a reason :-). I wait at least 24 hours for an answer before I try somewhere else.
    I apologize, I just wanted different opinions to have a better understanding, thats all. From now on I will not do this, sorry again.


    Added after 1 17 minutes:


    This is how I did it...

    .h file
    private:
    Ui::MainWindow *ui;
    QGraphicsScene * sheet;
    QGraphicsRectItem *rectangle;

    cpp file:

    void MainWindow:n_pushButton_Calculate_clicked()
    {
    calculate();
    sheet = new QGraphicsScene (this);
    ui->graphicsView->setScene(sheet);

    QBrush redBrush(Qt::red);
    QBrush blueBrush(Qt::blue);

    QPen blackpen(Qt::black);
    blackpen.setWidth(6);

    rectangle = sheet->addRect(10,10,100,100, blackpen, redBrush);
    }
    Thank you all very much for your help.
    Last edited by fs_tigre; 28th April 2012 at 03:21.

  6. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Understanding QGraphics

    memory leak (only cleaned up when your mainwondow object is disposed of).
    http://www.qtforum.org/post/118523/d...tml#post118523
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. QGraphics scene position
    By junix in forum Newbie
    Replies: 6
    Last Post: 17th April 2012, 03:41
  2. QGraphics*.*Item and __del__
    By prashant in forum Qt Programming
    Replies: 0
    Last Post: 31st October 2009, 16:11
  3. Animation and QGraphics
    By PrimeCP in forum Qt Programming
    Replies: 2
    Last Post: 7th May 2009, 00:31
  4. Qgraphics View
    By amagdy.ibrahim in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2008, 10:10
  5. QGraphics view problem
    By kiranraj in forum Qt Programming
    Replies: 5
    Last Post: 6th March 2007, 21: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
  •  
Qt is a trademark of The Qt Company.