Results 1 to 3 of 3

Thread: QGraphicsView and QGraphicsScene resizing problem when using layouts

  1. #1
    Join Date
    Jul 2011
    Location
    Paris
    Posts
    31
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QGraphicsView and QGraphicsScene resizing problem when using layouts

    Hello,

    I'm having some problems with the implementation of these classes,
    What I'm doing so far is I have a MainWindow and the window will have a QVBoxLayout. So basically this window will be divided by two vertically. I make two QWidgets and put each widget in the layout.

    I want to set one image in the top half and another image in the Bottom half. So for this I created a QGraphicsView class that has a scene and an item derived from QGraphicsItem.

    So I have in QMainWindow class:

    Qt Code:
    1. ...
    2. topWidget = new QWidget;
    3. bottomWidget = new QWidget;
    4.  
    5. grView = new QGraphicsView(topWidget);
    6. scene = new QGraphicsScene(this);
    7. scene->setScenRect(0,0,topWidget.width(), topWidget->height());
    8. item = new GrItem(scene);
    9.  
    10. scene->addItem(item);
    11. grView->setScene(scene);
    12.  
    13. ...
    To copy to clipboard, switch view to plain text mode 

    Here I pass topWidget as the parent to the GraphicsView class so that it will contain the view widget. GrItem is a class derived from QGraphicsItem. I pass the scene as a parameter to be able to get the size of the sceneRect and in the paint method do :


    Qt Code:
    1. ...
    2. painter->drawImage(sceneRect, myImage);
    To copy to clipboard, switch view to plain text mode 

    And after I set the layout:

    Qt Code:
    1. mainLayout = QVBoxLayout;
    2. mainLayout->addWidget(topWidget);
    3. mainLAyout->addWidget(bottomWidget);
    4.  
    5. setLayout(mainLayout);
    To copy to clipboard, switch view to plain text mode 

    The problem I'm having is that only a portion of the image is getting drawn instead of the whole image getting drawn scaled to the size of the topWidget.

    My question is where do I have to do the image sizing handling to display the whole image in this case in the whole top half of the mainwindow? I can't give setSceneRect a fixed QRecF because I also want to be able to resize the topWidget and bottomWidget and have the image always occupy the whole area of the widget that is containing the image. And since I am using layouts I don't set the size of the widgets beforehand. Also how do I properly set parent child relationships between GraphicsView, GraphicsScene and GraphicsItem?

    Could someone please give me some guidance I would really appreciate it.

    Thanks!

  2. #2
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsView and QGraphicsScene resizing problem when using layouts

    If your QGraphicsItem only paints an image to occupy the whole scene why arent you using QGraphicsScene::drawBackground()?

    Also are you changing the size of the scene rect when you change the size of your QGraphicsView widget? This could be done in QGraphicsView::resizeEvent().

  3. #3
    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: QGraphicsView and QGraphicsScene resizing problem when using layouts

    How about just using QLabel instead of those graphics views? Or even a custom widget?
    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. Replies: 2
    Last Post: 23rd October 2012, 01:10
  2. problem with QGraphicsScene\QGraphicsView
    By Wojtek_SZ in forum Qt Programming
    Replies: 4
    Last Post: 11th August 2011, 15:03
  3. mouseMoveEvent problem in QGraphicsView/QGraphicsScene
    By Lendrick in forum Qt Programming
    Replies: 3
    Last Post: 23rd September 2010, 05:26
  4. Layouts and form resizing
    By Chisum in forum Newbie
    Replies: 1
    Last Post: 2nd April 2009, 00:58
  5. Layouts to ease resizing... [solved]
    By Dumbledore in forum Qt Tools
    Replies: 6
    Last Post: 16th February 2008, 16:06

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.