Adding Scene/Item to a graphicView which was created using the design mode
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.
Code:
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
//create a scene
//create the rectangle
rect->setRect(0,0,50,50);
//add the item into the scene
scene->addItem(rect);
//use the view from the mainwindow
graphicsView.show();
}
I have randomly tried including "ui_mainwindow.h" but it still doesn't work. Haha
Re: Adding Scene/Item to a graphicView which was created using the design mode
Quote:
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:
Code:
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
Code:
setCentralWidget( ui->whateverTheGraphicsViewIsNamed );
at the end of the MainWindow constructor.
Re: Adding Scene/Item to a graphicView which was created using the design mode
Quote:
Originally Posted by
d_stranz
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
Code:
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,
_
Re: Adding Scene/Item to a graphicView which was created using the design mode
Problem Solved! Many thanks :)