Results 1 to 5 of 5

Thread: Find the Scene dimention from inside a QGraphicsItem

  1. #1
    Join Date
    Aug 2015
    Posts
    25
    Qt products
    Qt5
    Platforms
    Unix/X11
    Thanks
    3

    Default Find the Scene dimention from inside a QGraphicsItem

    Hi,

    I am trying to create a custom GraphicsItem that will set its width autocratically to the Scene width.
    Qt Code:
    1. sceneBoundingRect()
    To copy to clipboard, switch view to plain text mode 
    sounds like what I need, but my Program crashes all the time.

    Qt Code:
    1. #include "customparentitem.h"
    2. #include <QDebug>
    3.  
    4. customparentitem::customparentitem()
    5. {
    6. setFlag(ItemIsMovable);
    7. }
    8.  
    9. QRectF customparentitem::boundingRect() const
    10. {
    11. qDebug() << this->sceneBoundingRect();
    12. return QRectF(0,0,200,30);
    13. }
    14.  
    15. void customparentitem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    16. {
    17. QRectF temprec=boundingRect();
    18. painter->drawRoundedRect(temprec.x(),temprec.y(),temprec.width(),temprec.height(),8,8);
    19. }
    To copy to clipboard, switch view to plain text mode 

    The returned BoundingRect is just temporary until I get the desired output with qDebug().

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow),
    7. scene(new QGraphicsScene),
    8. customitem(new customparentitem)
    9. {
    10. ui->setupUi(this);
    11.  
    12. ui->graphicsView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    13. ui->graphicsView->setRenderHint(QPainter::Antialiasing);
    14. scene->setSceneRect(ui->graphicsView->rect());
    15.  
    16. scene->addItem(customitem);
    17.  
    18. ui->graphicsView->setScene(scene);
    19. }
    20.  
    21. MainWindow::~MainWindow()
    22. {
    23. delete ui;
    24. }
    To copy to clipboard, switch view to plain text mode 

    This compiles just fine, but as said earlier crashes. Any ideas why?

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

    Default Re: Find the Scene dimention from inside a QGraphicsItem

    Maybe you did not set the scene rect.
    In which case it will be determined by getting the union of all item bounding rects, which in your case would likely lead to a infinte cascading recursion.

    Cheers,
    _

  3. #3
    Join Date
    Aug 2015
    Posts
    25
    Qt products
    Qt5
    Platforms
    Unix/X11
    Thanks
    3

    Unhappy Re: Find the Scene dimention from inside a QGraphicsItem

    I want the Scenerect to be the same size as the graphicsView its sitting in.
    Set the Scenerect this way:
    Qt Code:
    1. scene->setSceneRect(ui->graphicsView->rect());
    To copy to clipboard, switch view to plain text mode 

    I will test out if it still crashes if I set the Scenerect manualy.

    Edit: Just tested it, no change. The program compiles, starts and crashes immediately.
    Last edited by 0backbone0; 26th August 2015 at 20:14.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,346
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: Find the Scene dimention from inside a QGraphicsItem

    The main window and all sub-widgets within it do not have any size until after the showEvent. So the setup you are trying to do in the main window constructor isn't going to result in anything useful. Implement showEvent() for your main window class and do the size determination there.

    You'll probably also want to implement resizeEvent() too if you want the graphics item to change size as the view changes.

    But then you could simply do all of this with a call to QGraphicsView::fitInView() with your top-level item as the argument.

  5. #5
    Join Date
    Aug 2015
    Posts
    25
    Qt products
    Qt5
    Platforms
    Unix/X11
    Thanks
    3

    Default Re: Find the Scene dimention from inside a QGraphicsItem

    Just found out that SceneBoundingRect just uses the BoundingRect of the Item and map it to Scene Coordinates.
    I tried to use this to view a Boundigrect (even use it to calculate one) that doesn’t exist jet,witch obviously can't work.

    I think the simplest workaround is to put a Slot to Mainwindow that updates the BoundingRect of the Graphicsitem.
    Like this:
    Qt Code:
    1. connect(scene,SIGNAL(sceneRectChanged(QRectF)),this,SLOT(setitemwidth(QRectF));
    To copy to clipboard, switch view to plain text mode 

    And QGraphicsView::fitInView() just updates the Scene, not the Items in it. Thus it isn't much use to me in this situation.

Similar Threads

  1. QGraphicsItem goes out of scene
    By pratham_shah in forum Qt Programming
    Replies: 5
    Last Post: 3rd July 2013, 16:44
  2. How to find out when mouse pointer is out side of scene
    By alizadeh91 in forum Qt Programming
    Replies: 0
    Last Post: 17th April 2012, 12:45
  3. QGraphicsItem independent of scene transformation
    By wagmare in forum Qt Programming
    Replies: 4
    Last Post: 19th January 2012, 09:47
  4. Can I copy a QGraphicsItem on my scene?
    By JesperWe in forum Qt Programming
    Replies: 3
    Last Post: 12th February 2010, 22:21
  5. How to find out scene update time
    By nileshsince1980 in forum Qt Programming
    Replies: 5
    Last Post: 20th September 2007, 10:46

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.