PDA

View Full Version : Adding QGraphicsView to QVBoxLayout



awseward1
5th March 2011, 02:28
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.

stampede
5th March 2011, 08:20
What are you doing with the layout after adding QGraphicsView ? You need to set it on a widget:


QLayout * layout = new QVBoxLayout();
QGraphicsView * view = new QGraphicsView();
layout->addWidget(view);
QWidget * w = new QWidget();
w->setLayout(layout);
w->show();

awseward1
6th March 2011, 00:14
This completely solved my problem.
Thanks a lot!

invisible_uli
30th January 2012, 07:41
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()


QGraphicsScene scene;
... // creating some items that inherit from QGraphicsItem - like I said, there shouldn't be an error here
// cause the view itself shows when I use it as the main widget
scene.addItem(pa);

QGraphicsView *view = new QGraphicsView(&scene);
QBrush red(QColor(250,50,50));
view->setBackgroundBrush(red); // the red doesn't even show up in the mainwindow, it's a white space beside my buttons

QWidget *win = new QWidget(this); //this <=> MainWindow
win->setMinimumSize(desktopWidth, desktopHeight - 10); // tried that to make sure it's not the non-existence of size

QHBoxLayout *layout = new QHBoxLayout();
layout->addWidget(bw); // my buttons
layout->addWidget(view);
win->setLayout(layout);


Thanks for any help
:confused: 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 ?)

ChrisW67
30th January 2012, 08:00
Is the QGraphicsScene allocated on the heap or is it going out of scope before it can be displayed?

invisible_uli
30th January 2012, 17:38
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 !