PDA

View Full Version : Adding Scene/Item to a graphicView which was created using the design mode



tuerahyosua
27th June 2015, 17:29
Hi everyone. I am a newbie in Qt and I have a question.

So I created a new project of QMainWindows. Then I go directly to Forms -> mainwindow.ui and drag Graphics View into the main window. The next thing that I want to do is to add a rectangle inside the graphicView (name of the QGraphicView object) and make it move around just as I want it to be. Any suggestions on how to do that?

I want to make the animation inside the MainWindow, not in another window. I have tried creating scene and trying to put in by code in the mainwindow.cpp (as below), but it doesn't work. If i was able to put the rectangle into the scene/view, i can move to animating the rectangle. But i'm still stuck here.



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

//create a scene
QGraphicsScene * scene = new QGraphicsScene();

//create the rectangle
QGraphicsRectItem * rect = new QGraphicsRectItem();
rect->setRect(0,0,50,50);

//add the item into the scene
scene->addItem(rect);

//use the view from the mainwindow
graphicsView = new QGraphicsView(scene);
graphicsView.show();

}


I have randomly tried including "ui_mainwindow.h" but it still doesn't work. Haha

d_stranz
27th June 2015, 21:33
Then I go directly to Forms -> mainwindow.ui and drag Graphics View into the main window.

I presume you mean you did this in Qt Designer. What name did you give to this QGraphicsView instance? (Look at the properties in Qt Designer if you don't remember).

And if you did that, why are you creating another QGraphicsView instance inside the MainWindow constructor when you already have one created during the call to ui->setupUi() (because you dragged one into the main window in Qt Designer)?

Use that one:
ui->whateverTheGraphicsViewIsNamed->setScene( scene ); No need to call show(), because it will be shown when the MainWindow is shown.

I don't create MainWindow UIs in Qt Designer. Dialogs and other QWidget-based forms, yes, but not main windows. So I don't know if dragging something into a main window form makes that the main window's "central widget" or not. If the graphics view does not fill the entire main window when the app comes up, then it hasn't been set as the central widget. You should add a call to
setCentralWidget( ui->whateverTheGraphicsViewIsNamed ); at the end of the MainWindow constructor.

anda_skoa
28th June 2015, 08:55
I don't create MainWindow UIs in Qt Designer. Dialogs and other QWidget-based forms, yes, but not main windows. So I don't know if dragging something into a main window form makes that the main window's "central widget" or not. If the graphics view does not fill the entire main window when the app comes up, then it hasn't been set as the central widget. You should add a call to
setCentralWidget( ui->whateverTheGraphicsViewIsNamed ); at the end of the MainWindow constructor.

The generated code takes care of that. The GraphicsView will be a child of the central widget.

Cheers,
_

tuerahyosua
28th June 2015, 09:51
Problem Solved! Many thanks :)