Results 1 to 8 of 8

Thread: Can't get positions of graphics items in scene

  1. #1
    Join Date
    Aug 2014
    Posts
    13
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Can't get positions of graphics items in scene

    Hi,

    I am trying to get the positions of the graphicsitems in the scene.
    But their QPointF value always remains (0,0).

    I am painting when mouse-click event occurs. On debugging scene->items(), I get

    (QGraphicsItem(this =0x22edff0, parent =0x0, pos =QPointF(0, 0) , z = 0 , flags = ( ) ) )

    for each graphics item in scene but with different memory address.

    This is my mainwindow.cpp code:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QDebug>
    3.  
    4. MainWindow::MainWindow()
    5. {
    6. scene = new QGraphicsScene;
    7. view = new QGraphicsView;
    8. view->setScene(scene);
    9. button = new QPushButton("Item");
    10. QGridLayout *layout = new QGridLayout;
    11. layout->addWidget(button);
    12. layout->addWidget(view);
    13. setLayout(layout);
    14. connect(button, SIGNAL(clicked()), this, SLOT(createItem()));
    15. }
    16.  
    17. void MainWindow::createItem()
    18. {
    19. myEntity = new Item;
    20. scene->addItem(myEntity);
    21. count_items();
    22. }
    23.  
    24. void MainWindow::count_items()
    25. {
    26. qDebug() << scene->items().count();
    27. qDebug() << scene->items();
    28. }
    29.  
    30. MainWindow::~MainWindow()
    31. {
    32. }
    To copy to clipboard, switch view to plain text mode 

    This is my item.cpp code:
    Qt Code:
    1. #include "item.h"
    2.  
    3. Item::Item()
    4. {
    5. ClickFlag = true;
    6. PaintFlag = false;
    7. }
    8.  
    9. Item::~Item()
    10. {
    11. }
    12.  
    13. QRectF Item::boundingRect() const
    14. {
    15. // outer most edges
    16. return QRectF(0,0,1450,1400);
    17. }
    18. void Item::mousePressEvent(QGraphicsSceneMouseEvent *event)
    19. {
    20. if(event->button()==Qt::LeftButton){
    21. if(ClickFlag){
    22. x = event->pos().x();
    23. y = event->pos().y();
    24. PaintFlag = true;
    25. ClickFlag = false;
    26. }
    27. }
    28. }
    29.  
    30. void Item::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
    31. QWidget *widget)
    32. {
    33. if(PaintFlag){
    34. QPen paintPen;
    35. paintPen.setWidth(4);
    36. pt.setX(x);
    37. pt.setY(y);
    38. painter->setPen(paintPen);
    39. painter->drawPoint(x,y);
    40. update();
    41. }
    42. }
    To copy to clipboard, switch view to plain text mode 

    I can't seem to find the position of these items correctly.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can't get positions of graphics items in scene

    And where do you set the item positions?

    Cheers,
    _

  3. #3
    Join Date
    Aug 2014
    Posts
    13
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Can't get positions of graphics items in scene

    Quote Originally Posted by anda_skoa View Post
    And where do you set the item positions?

    _
    How do I set the item's positions? Being a newbie, I just know setPos() is to be used but I do not know how it is to implemented.

  4. #4
    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: Can't get positions of graphics items in scene

    Quote Originally Posted by Kamalpreet View Post
    Being a newbie, I just know setPos() is to be used but I do not know how it is to implemented.
    I don't see any call to setPos() in your code. And I don't think your paint() code makes any sense, especially the part where you call update(), since it makes an infinite loop of repaints.
    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.


  5. #5
    Join Date
    Aug 2014
    Posts
    13
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Can't get positions of graphics items in scene

    Quote Originally Posted by wysota View Post
    I don't see any call to setPos() in your code. And I don't think your paint() code makes any sense, especially the part where you call update(), since it makes an infinite loop of repaints.
    So how can painting in scene using mouse clicks be achieved in appropriate manner? I also want to store the positions of points painted in the scene. I am making an application similar to CAD. I am referring diagram scene example provided in the documentation. Is it appropriate to serve my purpose?

  6. #6
    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: Can't get positions of graphics items in scene

    Quote Originally Posted by Kamalpreet View Post
    So how can painting in scene using mouse clicks be achieved in appropriate manner?
    Well... it depends how you want to represent your paintings in the scene. Most direct approach would be to create items for each path drawn by the user. For that you'd have to handle events in the scene and not in the item.
    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.


  7. #7
    Join Date
    Aug 2014
    Posts
    4
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Can't get positions of graphics items in scene

    Quote Originally Posted by wysota View Post
    Well... it depends how you want to represent your paintings in the scene. Most direct approach would be to create items for each path drawn by the user. For that you'd have to handle events in the scene and not in the item.
    Hi,
    We are taking the user's input by mouse clicks on where to draw the entities like point, line, circle, etc. So we were creating different classes for each entity.

    So does that mean we should only have paint event in these entity classes and get the user inputs(mouse events) in a graphics-scene class separately?

  8. #8
    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: Can't get positions of graphics items in scene

    Quote Originally Posted by Gurjot View Post
    So does that mean we should only have paint event in these entity classes and get the user inputs(mouse events) in a graphics-scene class separately?
    You should have as many paint events as the scene asks you to perform. An item needs to be repainted when its state changes. Thus if you have an item representing a circle then as long as you don't modify its color, size or move it around (or move another item over it), then it won't have to be repainted.
    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.


Similar Threads

  1. Replies: 0
    Last Post: 30th April 2013, 17:32
  2. Problem with mapping positions into the scene
    By jano_alex_es in forum Newbie
    Replies: 3
    Last Post: 11th September 2009, 08:33
  3. Replies: 6
    Last Post: 8th June 2009, 21:44
  4. graphics scene
    By dognzhe in forum Qt Programming
    Replies: 3
    Last Post: 3rd June 2009, 08:55
  5. Buttons in Graphics Scene
    By aamer4yu in forum Qt Programming
    Replies: 3
    Last Post: 28th November 2007, 19:24

Tags for this Thread

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.