Results 1 to 7 of 7

Thread: Getting size of a QGraphicsScene

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

    Default Getting size of a QGraphicsScene

    Hi, this is a "spin-off" question of the thread here.

    To keep control over available screen space I want to know the visible part of a QGraphicsScene - in scene coordinates.

    To verify that my information is correct, I want to draw a black rectangle all over the visble scene area. Of course, the rectangle is just for testing purposes :-)

    I've tried the following approach:
    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3.  
    4. this->showMaximized();
    5.  
    6. scene=new QGraphicScene(this);
    7. view = new QGraphicsView(scene);
    8. QWidget *widget = new QWidget;
    9. QHBoxLayout *layout = new QHBoxLayout;
    10.  
    11.  
    12. //widget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    13. setCentralWidget(widget);
    14. widget->setLayout(layout);
    15. layout->addWidget(view);
    16.  
    17. this->scene->setSceneRect(this->view->rect());
    18.  
    19. rectItem->setRect(this->view->mapToScene(this->view->rect()).boundingRect());
    20.  
    21. scene->addItem(rectItem);
    22. rectItem->setZValue(1000);
    23. rectItem->setBrush(QBrush(Qt::black,Qt::SolidPattern));
    24.  
    25.  
    26. view->setRenderHints(QPainter::HighQualityAntialiasing|QPainter::TextAntialiasing);
    27. view->setDragMode(QGraphicsView::ScrollHandDrag);
    28. view->setInteractive(true);
    29. view->setMouseTracking(true);
    30.  
    31.  
    32.  
    33. [...put other stuff onto scene...]
    34.  
    35. }
    To copy to clipboard, switch view to plain text mode 

    I'd love to see a black screen now - but the RectItem is much smaller than expected - thus I have quite obviously made a mistake in getting the size of the viewable area...

    Only the code seems logical and correct to me.

    Desired result: desiredResult.jpg

    Actual result: viewportRecNewTry.jpg

    What am I doing wrong?

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

    Default Re: Getting size of a QGraphicsScene

    You haven't tried to get the viewable area anywhere, though. You are setting the scene to be the same size as the view widget.

    When you set the scene rect to the rect of the view, you expect that http://qt-project.org/doc/qt-4.8/qgr...html#transform is identity. Have you checked it?



    edit:

    see my sig as well.
    scene=new QGraphicScene(this);
    that will give compiler error. Please only cut/paste code.



    You also have the problem of not updating your rects on size change of the view...
    Having done a quick test, this is your problem

    I added some code like this
    Qt Code:
    1. void
    2. Widget::resizeEvent(QResizeEvent* event)
    3. {
    4. QWidget::resizeEvent(event);
    5.  
    6. m_view->scene()->clear();
    7.  
    8. m_view->scene()->setSceneRect(m_view->rect());
    9.  
    10. QTransform t = m_view->transform();
    11.  
    12. rectItem->setRect(m_view->mapToScene(m_view->rect()).boundingRect());
    13. rectItem->setBrush(QBrush(Qt::black,Qt::CrossPattern));
    14.  
    15. m_view->scene()->addItem(rectItem);
    16. }
    To copy to clipboard, switch view to plain text mode 

    and the rectangle always fills the widget.
    Last edited by amleto; 29th April 2012 at 14:03.
    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.

  3. The following user says thank you to amleto for this useful post:

    sedi (29th April 2012)

  4. #3
    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: Getting size of a QGraphicsScene

    Thank you amleto, nice to see you over here, too! Well, I don't know how to get the area - that's kind of my problem ;-). I thought the viewable size might be exactly that of the view. I've changed "view" to view->viewport():
    Qt Code:
    1. rectItem->setRect(this->view->mapToScene(this->view->viewport()->rect()).boundingRect());
    To copy to clipboard, switch view to plain text mode 
    But it didn't change the result.

    I have never set any translation on scene or view, thus I hadn't checked that and I'm not quite sure if I did it correctly now:
    Qt Code:
    1. qDebug()<<this->view->transform();
    To copy to clipboard, switch view to plain text mode 
    yields this:
    QTransform(type=TxNone, 11=1 12=0 13=0 21=0 22=1 23=0 31=0 32=0 33=1)
    Because of "type=TxNone" I think this should be a non-transforming matrix. Thinking about it... it's a 3x3 matrix with diagonal "1"s, that's identity, of course. Long time since I last thought about that stuff... :-)

    As for the compilable code: it's a >6000 lines project by now, so I have built a new project here that is compilable and seems the smallest size possible to reproduce my problem:
    main.cpp:
    Qt Code:
    1. #include <QtGui>
    2. #include <QApplication>
    3. #include "mainwindow.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. MainWindow mainWin;
    9. mainWin.show();
    10. return app.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. #include <QGraphicsScene>
    4. #include <QMainWindow>
    5.  
    6. class MainWindow : public QMainWindow
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. MainWindow();
    12. ~MainWindow();
    13. private:
    14. };
    15.  
    16.  
    17. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    mainwindow.cpp:
    Qt Code:
    1. #include <QtGui>
    2. #include "mainwindow.h"
    3. #include <qdebug.h>
    4.  
    5. MainWindow::MainWindow()
    6. {
    7. this->showMaximized();
    8. scene=new QGraphicsScene(this);
    9. view = new QGraphicsView(scene);
    10. QHBoxLayout *layout = new QHBoxLayout;
    11. QWidget *widget = new QWidget;
    12.  
    13. setCentralWidget(widget);
    14. widget->setLayout(layout);
    15.  
    16. layout->addWidget(view);
    17. this->scene->setSceneRect(this->view->viewport()->rect());
    18.  
    19. rectItem->setRect(this->view->mapToScene(this->view->viewport()->rect()).boundingRect());
    20. qDebug()<<this->view->transform();
    21.  
    22. scene->addItem(rectItem);
    23. rectItem->setBrush(QBrush(Qt::black,Qt::SolidPattern));
    24. }
    25. MainWindow::~MainWindow()
    26. {
    27. delete scene;
    28. }
    To copy to clipboard, switch view to plain text mode 
    Instead of the scene being filled entirely by the black QGraphicsRectItem (as expected) the result of this "naked" example is this one:
    tryoutResult.jpg
    I've tried to follow this thread, but my result doesn't change, when I do the correspondend changes in my mainwindow.cpp:
    Qt Code:
    1. QMatrix const matrix = view->matrix().inverted();
    2. QRect visibleRect = matrix.mapRect(view->viewport()->rect());
    3. // when the viewport is scrolled, then the top left point of the visible rectangle needs to be moved
    4. // according to the scroll bar positions
    5. visibleRect.moveTopLeft(matrix.map(QPoint(view->horizontalScrollBar()->value(),
    6. view->verticalScrollBar()->value())));
    7.  
    8. rectItem->setRect(visibleRect);
    To copy to clipboard, switch view to plain text mode 
    I am quite desperate now. It seemed to work for shad0w, why not for me?
    Thank you for your time!
    Last edited by sedi; 29th April 2012 at 15:49.

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

    Default Re: Getting size of a QGraphicsScene

    sorry, I edited my post a few times and took some questions out as I found the actual problem I think you are facing - see the end of my previous post.
    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.

  6. The following user says thank you to amleto for this useful post:

    sedi (29th April 2012)

  7. #5
    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: Getting size of a QGraphicsScene

    Hi Amleto, you've made my week. Thinking about it, it's just logical. I had thought, "this->showMaximized();" would have put me onto the sunny side of a maximized window already - but, of course, the "mainWin.show();" is yet to come after all the stuff I did in the constructor. Actually I took a different approach in the details, but you are right, the real problem is: i have to wait for the resize. I've tried it out quickly with a single shot timer - BINGO.
    Qt Code:
    1. QPointF tl(view->horizontalScrollBar()->value(), view->verticalScrollBar()->value());
    2. QPointF br = tl + view->viewport()->rect().bottomRight();
    3. QMatrix mat = view->matrix().inverted();
    4. QRectF visibleRect=mat.mapRect(QRectF(tl,br));
    5. rectItem->setPos(view->mapToScene(view->viewport()->pos()));
    6. visibleRect.setRect(visibleRect.x(),visibleRect.y(),visibleRect.width()-1,visibleRect.height()-1); //the "-1" is just for being able to see the red pen
    7. rectItem->setPen(QPen(Qt::red));
    8. rectItem->setRect(visibleRect);
    To copy to clipboard, switch view to plain text mode 

    Edit:
    To make it work in the initialisation of my real program it was not enough to hook into the resizeEvent, I also had to call the calculation from QEvent::show, as done here:
    Qt Code:
    1. void CentralWidget::resizeEvent(QResizeEvent *)
    2. {
    3. emit this->centralWidgetWasResized();
    4. }
    5. bool CentralWidget::event(QEvent * event)
    6. {
    7. if (event->type()==QEvent::Show) emit this->centralWidgetWasResized();
    8. event->ignore();
    9. QWidget::event(event);
    10. return true;
    11. }
    To copy to clipboard, switch view to plain text mode 
    Still, though, I'll have to keep in mind that the correct size is provided only after nearly all my constructors' work has been done.
    Thank you very much for all the effort you've put into this!
    Last edited by sedi; 30th April 2012 at 02:29. Reason: updated contents

  8. #6
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Getting size of a QGraphicsScene

    To make it work in the initialisation of my real program it was not enough to hook into the resizeEvent, I also had to call the calculation from QEvent::show, as done here:
    That is incorrect. If you had to do that - you did something wrong.

    Take a look (as compile at try) at this example code:
    Qt Code:
    1. // mainwindow.h
    2.  
    3. class MainWindow : public QMainWindow
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. MainWindow( QWidget* parent = 0) ;
    9.  
    10. protected:
    11. void resizeEvent( QResizeEvent* event );
    12.  
    13. private:
    14. };
    15.  
    16. //mainwindow.cpp
    17. MainWindow::MainWindow( QWidget* parent )
    18. :
    19. QMainWindow( parent ),
    20. view( new QGraphicsView( this ) ),
    21. item( NULL )
    22. {
    23. QGraphicsScene* scene = new QGraphicsScene( this );
    24. this->item = scene->addRect( 0,0,0,0, QPen(), Qt::blue );
    25.  
    26. this->view->setScene( scene );
    27. this->setCentralWidget( view );
    28. }
    29.  
    30. void MainWindow::resizeEvent( QResizeEvent* e )
    31. {
    32. this->view->scene()->setSceneRect( this->view->viewport()->rect() );
    33. this->item->setRect( this->view->viewport()->rect() );
    34. }
    35.  
    36. // main
    37. int main(int argc, char *argv[])
    38. {
    39. QApplication a(argc, argv);
    40. MainWindow w;
    41. w.showMaximized(); // here you show the window, not in the constructor
    42.  
    43. return a.exec();
    44. }
    To copy to clipboard, switch view to plain text mode 
    Resize event always has the new size of the window and is delivered after the show event. The is no reason for you to check size of the window in the show event.
    Also, you should not show widget from within the widget, you've got main() for that.

    Still, though, I'll have to keep in mind that the correct size is provided only after nearly all my constructors' work has been done.
    Again not correct - window size is set after calling show (for first time or resize or any other method of changing window geometry) but only after execution flow goes back to the event loop.
    So not only window size isn't set when you constructor work is entirely finished, it will not be set untill the system takes over (in this case when you call a.exec() ).

  9. The following user says thank you to Spitfire for this useful post:

    sedi (1st May 2012)

  10. #7
    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: Getting size of a QGraphicsScene

    Thank you, I'll look int that!!

Similar Threads

  1. How to fixed the size of the QGraphicsScene.?
    By syclopse in forum Qt Programming
    Replies: 9
    Last Post: 4th July 2011, 14:41
  2. QGraphicsScene size
    By coder89 in forum Qt Programming
    Replies: 4
    Last Post: 26th June 2011, 11:03
  3. how to check QGrapicsView/QGraphicsScene size
    By estel in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 10th February 2010, 00:38
  4. QGraphicsScene size
    By invictus in forum Qt Programming
    Replies: 11
    Last Post: 12th August 2007, 22:26
  5. How can I make size of QGraphicsScene smaller?
    By troorl_ua in forum Qt Programming
    Replies: 6
    Last Post: 21st April 2007, 08:56

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.