Hi i am new to object oriented programming. here I intent to make three animation plots
subwindow_movie *child_Movie = new subwindow_movie;
child_Movie->setGeometry(20,100,620,560);
child_Movie->setplot(0);
child_Movie->show();
subwindow_movie *child_Movie1 = new subwindow_movie;
child_Movie1->setGeometry(680,100,620,560);
child_Movie1->setplot(1);
child_Movie1->show();
subwindow_movie *child_Movie2 = new subwindow_movie;
child_Movie2->setGeometry(325,350,620,560);
child_Movie2->setplot(2);
child_Movie2->show();
but the problem is that they all share same value of setplot
i,e when the third subwindow_movie
is created child_movie0
and child_movie1
both setplot
values become 2
; how to get rid of that... below is the set value function inside the class
#include "subwindow_movie.h"
#include "ui_subwindow_movie.h"
#include "plot.h"
#include <qapplication.h>
#include <qmainwindow.h>
int movie;
subwindow_movie::subwindow_movie(QWidget *parent) :
QDialog(parent),
ui(new Ui::subwindow_movie)
{
ui->setupUi(this);
}
subwindow_movie::~subwindow_movie()
{
delete ui;
}
void subwindow_movie::setplot(int value)
{
if (value ==0)
{ d_plot0 = new Plot( this );
movie=0;}
else if (value ==1)
{ d_plot1 = new Plot( this );
movie=1;}
else if (value ==2)
{ d_plot2 = new Plot( this );
movie=2;}
}
void subwindow_movie::on_movie_slider_valueChanged(int value)
{
if (movie ==0)
{ d_plot0->setAlpha(value);
}
else if (movie ==1)
{ d_plot1->setAlpha(value);
}
else if (movie ==2)
{ d_plot2->setAlpha(value);
}
}
the real problem is int movie
which changes for with the creation of new child_movie
causes the child_movie0
to run movie2
. i dont want the movie
variable changed for the child_movie0
with the creation of child_movie1
.
With
int movie;
there is only one instance of the movie
variable (it is a global variable). Each time you set its value, the previous value is overwritten.
Make movie
a member variable instead:
class subwindow_movie {
int movie;
public:
...
}
Then, movie
is available as separate instance for each subwindow_movie
object you are creating. Inside the subwindow_movie
methods, you can still access the variable as before.
Essentially, you should never use global variables - see also http://www.learncpp.com/cpp-tutorial/42-global-variables: Why global variables are evil