Results 1 to 7 of 7

Thread: Getting size of a QGraphicsScene

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

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

    sedi (29th April 2012)

  3. #2
    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 01:29. Reason: updated contents

  4. #3
    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() ).

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

    sedi (30th April 2012)

  6. #4
    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, 13:41
  2. QGraphicsScene size
    By coder89 in forum Qt Programming
    Replies: 4
    Last Post: 26th June 2011, 10:03
  3. how to check QGrapicsView/QGraphicsScene size
    By estel in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 9th February 2010, 23:38
  4. QGraphicsScene size
    By invictus in forum Qt Programming
    Replies: 11
    Last Post: 12th August 2007, 21:26
  5. How can I make size of QGraphicsScene smaller?
    By troorl_ua in forum Qt Programming
    Replies: 6
    Last Post: 21st April 2007, 07: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
  •  
Qt is a trademark of The Qt Company.