Results 1 to 5 of 5

Thread: Multiple use of QGraphicScene

  1. #1
    Join Date
    Sep 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Multiple use of QGraphicScene

    Hi all,

    I'm quite new here and just started with Qt for a couple of days. Currently I'm working on my first project with Qt and I'm trying to draw some kind of map where a few red dots will become visible after putting data in a table.

    I've already created a ui in Qt with a QGraphicsView included. Then created a QGraphicsScene named 'scene'. In that scene i draw a few circles and lines with the following code:

    Qt Code:
    1. //draw map
    2. void Unknown::drawMap(int width)
    3. {
    4. scene->setSceneRect(0, 0, width, width);
    5. scene->setBackgroundBrush(Qt::white);
    6.  
    7. //draw horizontal & vertical line
    8. scene->addLine(0, (width/2), width, (width/2));
    9. scene->addLine((width/2), 0, (width/2), width);
    10.  
    11. //draw 5 circles
    12. int x = (width/12), y = width-(width/6);
    13. for(int i=0; i<5; i++)
    14. {
    15. scene->addEllipse(x, x, y, y, QColor(180, 180, 180), Qt::NoBrush);
    16. x = x + (width/12);
    17. y = y - (width/6);
    18. }
    19.  
    20. //put scene on screen
    21.  
    22. ui->graphicsView->setScene(scene);
    To copy to clipboard, switch view to plain text mode 
    This works like a charm. However, when this function is finished I would like to add some red dots to this QGraphicsScene with a different function.
    Qt Code:
    1. //draw dot
    2. void Unknown::drawDot()
    3. {
    4. scene->addEllipse(50, 50, 1, 1, QColor(255, 0, 0), Qt::NoBrush);
    5. ui->graphicsView->setScene(scene);
    6. }
    To copy to clipboard, switch view to plain text mode 
    When applying this function to the main program and build it, I get the error: "scene not declared". I do understand why, but I don't know how to solve this problem without getting errors. I already tried the following in the main program:
    Qt Code:
    1. #include "unknown.h"
    2. #include "ui_unknown.h"
    3.  
    To copy to clipboard, switch view to plain text mode 
    When adding this to the program, the building process completes succesfully. The program shuts down immediately though.

    Can someone help me out here?
    Last edited by Xtresis; 6th September 2010 at 10:59.

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

    Default Re: Multiple use of QGraphicScene

    The scene variable in yourd drawMap() is local to the method. Make it a member of your class and everything will be fine. This is a lack-of-C++-skills problem, you know...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple use of QGraphicScene

    Thanks for you reply. I know indeed that this is a lack-of-C++-skills, but I can't figure out how to fix this problem ( and that's why I put this in the Newbie subforum ). I would really appriciate an example how to solve this is the right way.

    Thank you!

  4. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Multiple use of QGraphicScene

    if you dont want to make it a member than you can access the scene by calling

    Qt Code:
    1. //draw dot
    2. void Unknown::drawDot()
    3. {
    4. //scene->addEllipse(50, 50, 1, 1, QColor(255, 0, 0), Qt::NoBrush);
    5. ui->graphicsView->scene()->addEllipse(50, 50, 1, 1, QColor(255, 0, 0), Qt::NoBrush);
    6. //We already set this in previous function so no need now
    7. //ui->graphicsView->setScene(scene);
    8. }
    To copy to clipboard, switch view to plain text mode 

    but as suggested you should learn to make a class.

  5. #5
    Join Date
    Sep 2010
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Multiple use of QGraphicScene

    Thank you for your reply. Your solution works indeed, but I will try it with a class as well. Already found someone in my study book, so I think that shouldn't be a problem.

Similar Threads

  1. how to use Multiple timers?
    By qtlinuxnewbie in forum Newbie
    Replies: 8
    Last Post: 21st September 2012, 09:01
  2. Multiple QWidgets
    By codeman in forum Qt Programming
    Replies: 7
    Last Post: 23rd March 2010, 11:21
  3. multiple sort
    By baray98 in forum Qt Programming
    Replies: 3
    Last Post: 25th August 2009, 08:43
  4. multiple defination
    By phillip_Qt in forum Qt Programming
    Replies: 4
    Last Post: 13th December 2007, 17:32
  5. Replies: 0
    Last Post: 21st December 2006, 11:48

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.