Results 1 to 7 of 7

Thread: Getting size of a QGraphicsScene

Threaded View

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

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.