如何从另一个函数更改QGraphicsScene?

时间:2015-12-29 18:02:10

标签: c++ qt qgraphicsview qgraphicsscene

我的程序有一个名为SimulatorGV的QGraphicsView,它有一个名为Simulation的QGraphicsScene。在主函数中,我绘制了一些矩形和椭圆。我希望能够在另一个函数中绘制相同的场景。

本质上,如何从另一个函数向场景添加形状? (当按下用户界面上的按钮时调用它)

到目前为止,我已尝试将QGraphicsView传递给其他函数?下面是主要的ui函数(不相关的语句被删除):

{
    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); 

}

在头文件中,我在私有槽中声明了这个函数:

void DrawingSimulation(QGraphicsView *SimulationGV);

应该是这样吗?

void DrawingSimulation(QGraphicsScene *Simulation);

我在另一个函数中调用了这个函数:

DrawingSimulation(ui->SimulatorGV);

应该是这样吗?

DrawingSimulation(ui->SimulatorGV->Simulation);

或吗

DrawingSimulation(ui->Simulation);

这是我希望能够在场景中绘制的函数:

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是我的ui表单中的QGraphicsView的名称。 RASP是项目的名称。 ParticleLocation[i*6+3]是x坐标,[i*6+4]是y坐标。

我是否正确将QGraphicsView传递给varibale而不是QGraphicsScene Simulation

我是否正确传递了它?

在DrawingSimulation函数中,我是否正确添加了椭圆?

编辑: 本质上,如何从另一个函数向场景添加形状? (当按下用户界面上的按钮时调用它)

按下按钮时,会调用此功能:

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

     mainfunction();
    }

然后在mainfunction():

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

现在调用我想在DrawingSimulation()(MyClass :: MyClass)中的原始场景上绘制的函数RASP::RASP()

我之前的尝试是有一个布尔函数,按钮设置为真,然后是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);
            }
        }
}
}

然后在按钮单击功能设置SimulationRun =为true。

1 个答案:

答案 0 :(得分:1)

让我们保持简单。

如果你有:

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); 
}

如果这样可行,那么这也会起作用:

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); 
}

然后,如果这样可行,您现在知道如何修改&#34;另一个函数&#34;中的secene。

最后,您还应该:

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);
        }
    }
}

请注意,DrawingSimulation()也可以是slot (在您的头文件中使用public slots:声明它。然后,如果您connect它在您的GUI clicked()的{​​{1}} signal中,当点击按钮并添加椭圆时,将调用它。

像这样:

QPushButton