Results 1 to 6 of 6

Thread: Adding QGraphicsView to QVBoxLayout

  1. #1
    Join Date
    Mar 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Adding QGraphicsView to QVBoxLayout

    Hello,
    I've got an application in which I'm trying to display something in a graphics view on a button click in a mainwindow.cpp file.
    I've looked at several examples, and many of them seem to be doing such with using something along the lines of

    layout->addWidget( view );

    where layout is a defined QVBoxLayout, or QHBoxLayout, and view is a QGraphicsView they've defined.
    This is definitely not working for me, and I have no idea why.
    Any help would be greatly appreciated.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Adding QGraphicsView to QVBoxLayout

    What are you doing with the layout after adding QGraphicsView ? You need to set it on a widget:
    Qt Code:
    1. QLayout * layout = new QVBoxLayout();
    2. layout->addWidget(view);
    3. QWidget * w = new QWidget();
    4. w->setLayout(layout);
    5. w->show();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Adding QGraphicsView to QVBoxLayout

    This completely solved my problem.
    Thanks a lot!

  4. #4
    Join Date
    Jan 2010
    Posts
    10
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Adding QGraphicsView to QVBoxLayout

    Hi, I have a similar problem, but I did put my layout onto a widget, so that's not the mistake.

    I created a QGraphicsView with content and it shows up when I use view->show() or view->showMaximized(), but now I want it within the MainWindow (having buttons on the left side) but there is just a blank white area showing up... so here is my code, hope somebody can see the mistake:

    This is in MainWindow::MainWindow()
    Qt Code:
    1. ... // creating some items that inherit from QGraphicsItem - like I said, there shouldn't be an error here
    2. // cause the view itself shows when I use it as the main widget
    3. scene.addItem(pa);
    4.  
    5. QGraphicsView *view = new QGraphicsView(&scene);
    6. QBrush red(QColor(250,50,50));
    7. view->setBackgroundBrush(red); // the red doesn't even show up in the mainwindow, it's a white space beside my buttons
    8.  
    9. QWidget *win = new QWidget(this); //this <=> MainWindow
    10. win->setMinimumSize(desktopWidth, desktopHeight - 10); // tried that to make sure it's not the non-existence of size
    11.  
    12. QHBoxLayout *layout = new QHBoxLayout();
    13. layout->addWidget(bw); // my buttons
    14. layout->addWidget(view);
    15. win->setLayout(layout);
    To copy to clipboard, switch view to plain text mode 

    Thanks for any help
    Uli


    Added after 11 minutes:


    I don't know if this tells somebody something, but I noticed something weird in general:

    Earlier I tried creating the view and its context without using the mainWindow at all.. used view->showMaximized() and everything was perfect.
    Now I tried to just show my QGraphicsView from within the MainWindow (so removed the whole part that is after line 10 in my earlier comment, that is QWidget and QLayout) and simply put a view->showMaximized() .. as a result I see my QGraphicsView for a second and then it disappears and I have a white blank area instead of the red background with my content.. could that be the same white area that I see when adding it to the layout ? Is there something wrong with putting my view in the MainWindow ? (IMO it should be fine existing in MainWindow ?)
    Last edited by invisible_uli; 30th January 2012 at 07:41.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Adding QGraphicsView to QVBoxLayout

    Is the QGraphicsScene allocated on the heap or is it going out of scope before it can be displayed?

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

    invisible_uli (30th January 2012)

  7. #6
    Join Date
    Jan 2010
    Posts
    10
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Adding QGraphicsView to QVBoxLayout

    Hey Chris, thank you so much.. such a stupid mistake.. I should have seen.. haven't worked in c++ for a while, but this is something so essential. Thank you for taking the time, I really appreciate it !

Similar Threads

  1. QVBoxLayout to center
    By seltra in forum Newbie
    Replies: 2
    Last Post: 7th October 2010, 18:23
  2. QHBoxLayout & QVBoxLayout
    By damodharan in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2010, 14:13
  3. QGraphicsView and adding QWidget with transparent areas
    By sting73 in forum Qt Programming
    Replies: 2
    Last Post: 28th April 2009, 11:19
  4. QScrollView with a QVboxlayout
    By rishid in forum Newbie
    Replies: 1
    Last Post: 18th January 2008, 16:14
  5. Adding widgets to a QGraphicsView's margins
    By nmather in forum Qt Programming
    Replies: 2
    Last Post: 7th April 2007, 16:52

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.