PDA

View Full Version : changing values for one class and showing it ..!


salmanmanekia
14th August 2008, 12:13
hi all,

this is a pure c++ class , i am not good with pointers and it seems somehow pointers should be used,...pls see the problem below..

i have three classes named super ,child1 and child2..

#include "child1.h"
#include "child2.h"

class super
{
private:
int record_clicks;
..
child1 *myChild1;
child2 *myChild2;
..
};
class child1
{
..
private:
int record_clicks;
..
}
class child2
{
..
private:
int record_clicks;
..
}
the problem i am facing is that that when i change the value in child1->recordClick i wanted to see the changed value in child2->recordClick, i make a function for that which passes the value of recordClick to TrainingUI which then passes this value to child2,but now if i wanted to change the value in child2->recordClick and show it on child1->recordClick ...how can i do that i mean how to change recordClicks value in both direction..i hope u undertsand

spirit
14th August 2008, 13:04
#include <QDebug>

class super
{
public:
static int record_clicks;
};

int super::record_clicks = 0;

class child1: public super
{
};

class child2: public super
{
};

int main(int argc, char ** argv)
{
child1 ch1;
child2 ch2;
qDebug() << "ch1: " << ch1.record_clicks;
qDebug() << "ch2: " << ch2.record_clicks;

qDebug() << "ch1: " << ++ch1.record_clicks;
qDebug() << "ch2: " << ch2.record_clicks;

return 0;
}

when you change value in child1 then value in child2 is changed too.

salmanmanekia
14th August 2008, 13:33
thanks for the reply..your solution seems to be good but my problem seems to be different..:D..
hmm..i will put some more code to define the problem..
basically in my superClass i am using a connect statetmemt to see if the rec_button value has increased in myChild1 and it emits the change to myChild2 ..static doesnt seem to work ,it doesnt allows to change..
pls see the class 'child1' first ...then the 'super' class

//in superClass

connect(myChild1,SIGNAL(recordClicked(int)),this,S LOT(requestRecord(int)));

void superClass::requestRecord(int rec_clicks)
{
record_clicks = rec_clicks;
//emit requestedRecord(); //this is useless in this case because i wanted to also emit a signal to other widget to listen to
}

void superClass::recognitionAccept() //this method is succesfully called from another class and now i want to pass the recordclick value to the child..
{
..
myChild2->showAcceptedProgress(record_clicks);
..
}

// in my child1 class
..
connect(recordButton,SIGNAL(clicked()),this,SLOT(r eactToRecord()));
..
void child1::reactToRecord()
{

record_clicks++;
disableRecordButton();
emit recordClicked(record_clicks);

}


//in child2 class

void child2::showAcceptedProgress(int rec_clicks)
{
record_clicks = rec_clicks;
..
update();

}

spirit
14th August 2008, 13:45
add signal in child1
void clickCounChanged(int)
and slot in child2

void setClickCount(int)

and then in super class make connection

connect(child1, SIGNAL(clickCounChanged(int)), child2, SLOT(setClickCount(int)));

or can't it be done?

salmanmanekia
14th August 2008, 13:52
this seems ok,but it doesnt seem effecient as i have to go two way round in changing my values...so i have to introduce couple of connect statement with signals and slot...its not preferable...:(

spirit
14th August 2008, 14:07
you have signal recordClicked(int) in chil1, I don't understand why you cant add slot to chil2 for setting new value and connect them in superclass.

salmanmanekia
14th August 2008, 14:13
with using connect statement it is possible but i am just guessing what happens if u work in pure c++ ..hope u understand me...i am just confused in general about the concept about how to change a value in one class and to show it in the other class...