Results 1 to 4 of 4

Thread: Why changing x,y of rect in scene doesn't do anything?

  1. #1
    Join Date
    Apr 2019
    Posts
    4
    Qt products
    Qt5

    Default Why changing x,y of rect in scene doesn't do anything?

    I'm using the simple example from tutorial to show an rectangle on a scene.
    As I understand the axises are in the middle of the graphicalView, right?
    What I don't understand is why changing x,y of rectangle doesn't do anything.

    Here is the code

    Qt Code:
    1. QtGuiApplication1::QtGuiApplication1(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. ui.setupUi(this);
    5.  
    6. ui.graphicsView->setBackgroundBrush(Qt::red);
    7. _graphicsScene = new QGraphicsScene(this);
    8. ui.graphicsView->setScene(_graphicsScene);
    9.  
    10.  
    11. QBrush redBrush(Qt::yellow);
    12. QPen blackPen(Qt::black);
    13. blackPen.setWidth(6);
    14. _graphicsScene->addRect(0, 0, 100, 100, blackPen, redBrush);
    15. }
    To copy to clipboard, switch view to plain text mode 


    The result
    Untitled.jpg

    Even if I change x,y of the rect to
    Qt Code:
    1. addRect(50, 50, 100, 100, blackPen, redBrush)
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. addRect(10000, 10000, 100, 100, blackPen, redBrush)
    To copy to clipboard, switch view to plain text mode 
    it shows the same position of the rectangle. Why?
    Untitled.jpg

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

    Default Re: Why changing x,y of rect in scene doesn't do anything?

    You only have a single item, so the view "centers" on it.

    Cheers,
    _

  3. #3
    Join Date
    Apr 2019
    Posts
    4
    Qt products
    Qt5

    Default Re: Why changing x,y of rect in scene doesn't do anything?

    Quote Originally Posted by anda_skoa View Post
    You only have a single item, so the view "centers" on it.

    Cheers,
    _
    But, how I position the rectangle at specific location then? I tried
    Qt Code:
    1. rectangle = _graphicsScene->addRect(50, 50, 100, 100, blackPen, redBrush);
    2. rectangle->setPos(10, 20);
    To copy to clipboard, switch view to plain text mode 

    But, it still position the rectangle in the center of the scene.

    What I want to do after I figure out the how the coordinates works (I've read this btw) is to show an image and then add rectangles on an image at specific locations. Those rectangels will be interactive.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,546
    Thanks
    13
    Thanked 1,578 Times in 1,507 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Why changing x,y of rect in scene doesn't do anything?

    No, it places the rectangle in the scene at the location you supplied. When the view is shown, the view tries to do something sensible with the scene to map it to the available display coordinates. This may result in scaling and/or centreing the scene within the viewport. So, regardless of the scene coordinates, the single rectangle may appear in the same position in the viewport in spite of different scene coordinates.

    As to moving the rectangle, here is an example that moves the rectangle after the view is shown.
    Qt Code:
    1. #include <QApplication>
    2. #include <QGraphicsScene>
    3. #include <QGraphicsView>
    4. #include <QTimer>
    5.  
    6. class MyView: public QGraphicsView
    7. {
    8. Q_OBJECT
    9. QGraphicsScene *_graphicsScene;
    10.  
    11. public:
    12. explicit MyView(QWidget *p = nullptr): QGraphicsView(p), _graphicsScene(nullptr), _rect(nullptr)
    13. {
    14. _graphicsScene = new QGraphicsScene(0, 0, 500, 500, this);
    15. _graphicsScene->setBackgroundBrush(Qt::blue);
    16. // resize(800, 800);
    17. setScene(_graphicsScene);
    18.  
    19. QBrush redBrush(Qt::red);
    20. QPen blackPen(Qt::black, 6);
    21. _rect = _graphicsScene->addRect(0, 0, 100, 100, blackPen, redBrush);
    22. _graphicsScene->addLine( 0, 1000, 0, -1000, QPen(Qt::white));
    23. _graphicsScene->addLine(1000, 0, -1000, 0, QPen(Qt::white));
    24.  
    25.  
    26. // Make the square move 3 seconds from now
    27. QTimer::singleShot(3000, this, &MyView::moveItem);
    28. }
    29.  
    30. private slots:
    31. void moveItem() { _rect->moveBy(100, 100); }
    32. };
    33.  
    34. int main(int argc, char *argv[])
    35. {
    36. QApplication a(argc, argv);
    37. MyView view;
    38. view.show();
    39. return a.exec();
    40. }
    41. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Uncomment line 17 to see how the initial view changes for the same scene.

Similar Threads

  1. QGraphicsView and resizing scene rect
    By ratsrcute in forum Qt Programming
    Replies: 0
    Last Post: 10th October 2010, 04:23
  2. Replies: 7
    Last Post: 26th June 2009, 19:33
  3. How to set definite Scene Rect in QGraphicsView.
    By AmolShinde_8 in forum Qt Programming
    Replies: 2
    Last Post: 9th October 2008, 15:18
  4. Replies: 3
    Last Post: 12th July 2008, 22:51
  5. Zooming scene Rect(Qt bug?)
    By maverick_pol in forum Qt Programming
    Replies: 31
    Last Post: 30th August 2007, 09:31

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.