Results 1 to 16 of 16

Thread: No QGraphicsScene proper coordinates

  1. #1
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default No QGraphicsScene proper coordinates

    Hello!

    I'd like to assign a QGraphicsScene to a QGraphicsView which is set as the central widget of the main window.

    Moreover the scene coordinate system should be set as the view inner space...I wrote the following code:

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QGraphicsTextItem>
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10.  
    11. scene = new QGraphicsScene;
    12. view = new QGraphicsView;
    13. view->setScene(scene);
    14. view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    15. view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    16.  
    17. setCentralWidget(view);
    18.  
    19. scene->setSceneRect(view->geometry());
    20. view->setFocus();
    21.  
    22. showLine();
    23. }
    24.  
    25. void MainWindow::showLine()
    26. {
    27. scene->addLine(0, 0, 100, 100);
    28. }
    29.  
    30. MainWindow::~MainWindow()
    31. {
    32. delete ui;
    33. }
    To copy to clipboard, switch view to plain text mode 

    (scene and view are two MainWindow object's attributes)

    but, running this code the line is drawn about at the center of the view...why doesn't the line start at the top left corner?

    Thanks

  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: No QGraphicsScene proper coordinates

    See what view->geometry() returns in the constructor and you'll be able to answer your question yourself.
    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
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: No QGraphicsScene proper coordinates

    Hi

    I'm not able to find the error...I wrote this and now it works
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. view = new QGraphicsView;
    8.  
    9. view->setGeometry(0, 0, 300, 300);
    10. view->setParent(this);
    11.  
    12. }
    13.  
    14. void MainWindow::drawLine()
    15. {
    16. scene = new QGraphicsScene;
    17. view->setScene(scene);
    18. scene->setSceneRect(view->rect());
    19. scene->addLine(0, 0, 100, 100);
    20. }
    To copy to clipboard, switch view to plain text mode 

    but why the previous code doesn't work?

    Thanks

  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: No QGraphicsScene proper coordinates

    Quote Originally Posted by harmodrew View Post
    but why the previous code doesn't work?
    Did you check what geometry() returns from within the constructor?
    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 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: No QGraphicsScene proper coordinates

    it returns me a QRect with this values:

    x1 = 0
    x2 = 639
    y1 = 0
    y2 = 479

  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: No QGraphicsScene proper coordinates

    Quote Originally Posted by harmodrew View Post
    it returns me a QRect with this values:
    Could you please show us how you got those values (including the whole method body where you added your call)?
    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 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: No QGraphicsScene proper coordinates

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QGraphicsTextItem>
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10.  
    11. scene = new QGraphicsScene;
    12. view = new QGraphicsView;
    13. setCentralWidget(view);
    14.  
    15. view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    16. view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    17. view->setScene(scene);
    18. aux = view->geometry(); // AFTER DEBUGGING THIS ROW: AUX = 640x480+0+0
    19. }
    20.  
    21. void MainWindow::drawLine()
    22. {
    23. scene->addLine(0, 0, 100, 100);
    24. }
    25.  
    26. MainWindow::~MainWindow()
    27. {
    28. delete ui;
    29. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.showMaximized();
    9. w.drawLine(); // THE LINE WON'T START AT THE TOP LEFT CORNER
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QGraphicsView>
    6. #include <QGraphicsScene>
    7.  
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15. private:
    16. Ui::MainWindow *ui;
    17. QRect aux;
    18.  
    19. public:
    20. explicit MainWindow(QWidget *parent = 0);
    21. void drawLine();
    22. ~MainWindow();
    23. };
    24.  
    25. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

  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: No QGraphicsScene proper coordinates

    You are not setting the scene size anywhere so why would the line be in the top-left corner? By default the scene gets the boundingRect of all its items and is centered in the view.
    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.


  9. #9
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: No QGraphicsScene proper coordinates

    I added this row
    Qt Code:
    1. scene->setSceneRect(aux);
    To copy to clipboard, switch view to plain text mode 

    after the "aux row" but I gain the same result

  10. #10
    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: No QGraphicsScene proper coordinates

    I'm sure you get a different result but not the one you would probably like to get. You are showing the window maximized but I'm pretty sure the size of your screen is not even close to 640x480, right? So your scene is 640x480 but your view is much bigger. Correct?
    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.


  11. #11
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: No QGraphicsScene proper coordinates

    yes, you're right..but if I run the code at post #3 it draws the line at the right coords

  12. #12
    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: No QGraphicsScene proper coordinates

    You are setting the scene to the wrong size because in the constructor the size of the view is not correct. You can see that in the figures you gave me, right?

    Even if it worked then still trying to match the scene size with the view size is just a sign you don't understand how Graphics View architecture works. The scene operates in logical coordinates which are independent of how the scene is displayed in the view. The scene should have a fixed size and the view can zoom in on it so that it covers the whole area of the view's viewport.
    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.


  13. #13
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: No QGraphicsScene proper coordinates

    so I should assign a scenerect to the scene at the startup and then the view will scale oll the graphical items into the scene?

  14. #14
    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: No QGraphicsScene proper coordinates

    Quote Originally Posted by harmodrew View Post
    and then the view will scale oll the graphical items into the scene?
    If you tell it to do so, yes.
    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.


  15. #15
    Join Date
    Aug 2010
    Posts
    62
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: No QGraphicsScene proper coordinates

    And how can I tell the view to "stretch" the scene object to the view's viewport?
    for example if I assign a scenerect of 100x100 with the top letf corner as (0,0) to the scene and then draw a line from 0,0 to 100,100, I'd like the view to show the diagonal of the screen (so the scene is shown entirely into the view and is "stretched" into the view")...

    Thanks

  16. #16
    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: No QGraphicsScene proper coordinates

    Quote Originally Posted by harmodrew View Post
    And how can I tell the view to "stretch" the scene object to the view's viewport?
    I suggest you look at the available API of QGraphicsView.
    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. QGraphicsView, QGraphicsScene item coordinates in scene
    By harakiri in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2010, 10:02
  2. Using custom coordinates with QGraphicsScene
    By rec in forum Qt Programming
    Replies: 1
    Last Post: 7th June 2010, 08:44
  3. Replies: 7
    Last Post: 21st March 2010, 03:11
  4. Working with coordinates (QGraphicsScene/view)
    By maverick_pol in forum Qt Programming
    Replies: 5
    Last Post: 27th March 2008, 08:35
  5. Retrieving mouseover coordinates of QGraphicsScene
    By forrestfsu in forum Qt Programming
    Replies: 2
    Last Post: 23rd October 2006, 18:36

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.