-1

My program has a QGraphicsView called SimulatorGV and it has a QGraphicsScene called Simulation. In the main function I have drawn a few rectangles and ellipses. I want to be able to draw on the same scene in another function too.

In essence, how do you add shapes to a scene from another function? (Which is called when a button on the user interface is pressed)


So far I've tried to pass the QGraphicsView to the other function? Below is the main ui function (irrelevant statements cut out):

{
    ui->setupUi(this);
    Simulation = new QGraphicsScene(this);


        ui->SimulatorGV->setScene(Simulation);

        Simulation->addRect(0,415,20,50,noPen,greyBrush); 
        Simulation->addRect(425,230,10,420,noPen,goldBrush); 
        Simulation->addEllipse(80,90,700,700,greyPen,noBrush); 
        Simulation->addRect(72,215,90,450,noPen,blackBrush); 

}

In the header file I declared this function in the private slots:

void DrawingSimulation(QGraphicsView *SimulationGV);

Should it be like this instead?

void DrawingSimulation(QGraphicsScene *Simulation);

I called the function in another function like this:

DrawingSimulation(ui->SimulatorGV);

Should it be like this instead?

DrawingSimulation(ui->SimulatorGV->Simulation);

or?

DrawingSimulation(ui->Simulation);

This is the function I want to be able to draw on the scene from:

void RASP::DrawingSimulation(QGraphicsView *SimulationGV)
{
    for (int i = 0; i < DisplayAlphaParticleNumber; i++)
    {
        if (ParticleLocation[i*6+3] != 0 || ParticleLocation[i*6+6] != 0) 
        {
            ui->SimulatorGV->setScene(Simulation);
            Simulation->addEllipse(ParticleLocation[i*6+3],ParticleLocation[i*6+4],10,10);
        }
    }
}

SimulatorGV is the name of the QGraphicsView in my ui form. RASP is the name of the project. ParticleLocation[i*6+3] is the x coordinate and [i*6+4] is the y coordinate.

Was I right to pass the QGraphicsView onto the varibale instead of the QGraphicsScene Simulation?

Did I pass it correctly?

In the DrawingSimulation function did I add the ellipse correctly?

Edit: In essence, how do you add shapes to a scene from another function? (Which is called when a button on the user interface is pressed)

When a button is pressed this function is called:

    void RASP::on_Button1_clicked()
    {
    //some values set
    //some other functions called then the main one that leads to the drawingsimulation   

     mainfunction();
    }

Then inside the mainfunction():

void RASP::mainfunction()
{
     DrawingSimulation(); //is called
}

Now the function DrawingSimulation() which I would like to draw on the original scene in the RASP::RASP() (MyClass::MyClass) is called.


My previous attempt was to have a boolean function that is set true by the button then the addEllipse:

MyClass::MyClass()
{
ui->setupUi(this);
        Simulation = new QGraphicsScene(this);


            ui->SimulatorGV->setScene(Simulation);

            Simulation->addRect(0,415,20,50,noPen,greyBrush); 
            Simulation->addRect(425,230,10,420,noPen,goldBrush); 
            Simulation->addEllipse(80,90,700,700,greyPen,noBrush); 
            Simulation->addRect(72,215,90,450,noPen,blackBrush); 

if (SimulationRun == true)
{
for (int i = 0; i < DisplayAlphaParticleNumber; i++)
        {
            if (ParticleLocation[i*6+3] != 0 || ParticleLocation[i*6+6] != 0) 
            {
                ui->SimulatorGV->setScene(Simulation);
                Simulation->addEllipse(ParticleLocation[i*6+3],ParticleLocation[i*6+4],10,10);
            }
        }
}
}

and then in the button clicked function setting SimulationRun = to true.

jpo38
  • 20,821
  • 10
  • 70
  • 151
Alarming
  • 137
  • 1
  • 3
  • 14
  • This is becoming messy. I feel like your problem is actually to know how to call functions and pass parameters in C++....no real specific problem with the `QGraphicsScene` class. Please learn some C++ basic before trying to do complex stuff as you are trying to. – jpo38 Dec 30 '15 at 17:29
  • @jpo38 My main problem is that I didn't use signals & slots. I think I can solve this problem using it thanks to your post. I'm a student who has been a bit ambitious with a school project but you're answer has definitely been helpful, thank you very much :). – Alarming Dec 30 '15 at 17:36

1 Answers1

1

Let's keep it simple.

If you have:

MyClass::MyClass() : ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    Simulation = new QGraphicsScene(this);

    ui->SimulatorGV->setScene(Simulation);

    Simulation->addRect(0,415,20,50,noPen,greyBrush); 
    Simulation->addRect(425,230,10,420,noPen,goldBrush); 
    Simulation->addEllipse(80,90,700,700,greyPen,noBrush); 
    Simulation->addRect(72,215,90,450,noPen,blackBrush); 
}

If this works, then, this will work too:

MyClass::MyClass() : ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    Simulation = new QGraphicsScene(this); // supposing MyClass has a Simulation attribute of type QGraphicsScene

    ui->SimulatorGV->setScene(Simulation);

    addItems(); // calling function below
}

void MyClass::addItems()
{
    // declared "void addItems();" in header file
    addItems( Simulation ); // calling function below that could be static
}

void MyClass::addItems( QGraphicsScene* simu )
{
    // declared "static void addItems(QGraphicsScene* simu);" in header file
    simu->addRect(0,415,20,50,noPen,greyBrush); 
    simu->addRect(425,230,10,420,noPen,goldBrush); 
    simu->addEllipse(80,90,700,700,greyPen,noBrush); 
    simu->addRect(72,215,90,450,noPen,blackBrush); 
}

Then, if this works, you now know how to modify the secene from "another function".

Finally, you should also have:

void MyClass::DrawingSimulation()
{
    // declared "void DrawingSimulation();" in header file
    DrawingSimulation( Simulation );
}

void MyClass::DrawingSimulation(QGraphicsScene *simu)
{
    // declared "void DrawingSimulation(QGraphicsScene *simu);" in header file
    for (int i = 0; i < DisplayAlphaParticleNumber; i++)
    {
        if (ParticleLocation[i*6+3] != 0 || ParticleLocation[i*6+6] != 0) 
        {
            simu->addEllipse(ParticleLocation[i*6+3],ParticleLocation[i*6+4],10,10);
        }
    }
}

Note that DrawingSimulation() could also be a slot (declare it using public slots: in your header file. Then, if you connect it to the clicked() signal of a QPushButton of your GUI (for instance), it will be called when the button is clicked and ellipse will be added.

Like this:

MyClass::MyClass()
{
    ...

    connect( ui->pushButton, SIGNAL(clicked()), this, SLOT(DrawingSimulation()) );
}
jpo38
  • 20,821
  • 10
  • 70
  • 151
  • Thanks for the reply :D You're method is greater. it's my mistake for not mentioning in the question that the other function is called when a button in my user interface is pressed (actually it is called by another function that is called when a button is pressed). I've added the code for the button being pressed in the main question at the end. Could you explain how I could call the "add items" function when a button is pressed? I've also included my attempt at doing this. – Alarming Dec 30 '15 at 17:19
  • Updated my post, please have a look. – jpo38 Dec 30 '15 at 17:25
  • Hey, I've been trying to implement your above suggestion (the second code block) and have two questions: what do you mean by `ui = new ....;`? and how did you declare the function in the header file? Because when you do use `addItems()` the program outputs the error "function does not take 0 arguements" when I declare as `void addItems(QGraphicsScene *scene);` and says "overload member function not found" or when declared as `void addItems();` "function does not take 1 arguments". I've tried your example a new project just to test it out and will include the full code in an edit. Thanks :) – Alarming Jan 06 '16 at 16:34
  • Both `addItems` function must be declared in header file. `ui = new ...` was just to mention `ui` object creation. Updated my post to clarify all that. – jpo38 Jan 06 '16 at 19:31