Results 1 to 7 of 7

Thread: changing values for one class and showing it ..!

  1. #1
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default changing values for one class and showing it ..!

    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..

    Qt Code:
    1. #include "child1.h"
    2. #include "child2.h"
    3.  
    4. class super
    5. {
    6. private:
    7. int record_clicks;
    8. ..
    9. child1 *myChild1;
    10. child2 *myChild2;
    11. ..
    12. };
    13. class child1
    14. {
    15. ..
    16. private:
    17. int record_clicks;
    18. ..
    19. }
    20. class child2
    21. {
    22. ..
    23. private:
    24. int record_clicks;
    25. ..
    26. }
    To copy to clipboard, switch view to plain text mode 
    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
    Last edited by wysota; 14th August 2008 at 14:21. Reason: missing [code] tags

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: changing values for one class and showing it ..!

    Qt Code:
    1. #include <QDebug>
    2.  
    3. class super
    4. {
    5. public:
    6. static int record_clicks;
    7. };
    8.  
    9. int super::record_clicks = 0;
    10.  
    11. class child1: public super
    12. {
    13. };
    14.  
    15. class child2: public super
    16. {
    17. };
    18.  
    19. int main(int argc, char ** argv)
    20. {
    21. child1 ch1;
    22. child2 ch2;
    23. qDebug() << "ch1: " << ch1.record_clicks;
    24. qDebug() << "ch2: " << ch2.record_clicks;
    25.  
    26. qDebug() << "ch1: " << ++ch1.record_clicks;
    27. qDebug() << "ch2: " << ch2.record_clicks;
    28.  
    29. return 0;
    30. }
    To copy to clipboard, switch view to plain text mode 
    when you change value in child1 then value in child2 is changed too.

  3. #3
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: changing values for one class and showing it ..!

    thanks for the reply..your solution seems to be good but my problem seems to be different....
    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
    Qt Code:
    1. //in superClass
    2.  
    3. connect(myChild1,SIGNAL(recordClicked(int)),this,SLOT(requestRecord(int)));
    4.  
    5. void superClass::requestRecord(int rec_clicks)
    6. {
    7. record_clicks = rec_clicks;
    8. //emit requestedRecord(); //this is useless in this case because i wanted to also emit a signal to other widget to listen to
    9. }
    10.  
    11. void superClass::recognitionAccept() //this method is succesfully called from another class and now i want to pass the recordclick value to the child..
    12. {
    13. ..
    14. myChild2->showAcceptedProgress(record_clicks);
    15. ..
    16. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. // in my child1 class
    2. ..
    3. connect(recordButton,SIGNAL(clicked()),this,SLOT(reactToRecord()));
    4. ..
    5. void child1::reactToRecord()
    6. {
    7.  
    8. record_clicks++;
    9. disableRecordButton();
    10. emit recordClicked(record_clicks);
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //in child2 class
    2.  
    3. void child2::showAcceptedProgress(int rec_clicks)
    4. {
    5. record_clicks = rec_clicks;
    6. ..
    7. update();
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: changing values for one class and showing it ..!

    add signal in child1
    Qt Code:
    1. void clickCounChanged(int)
    To copy to clipboard, switch view to plain text mode 
    and slot in child2
    Qt Code:
    1. void setClickCount(int)
    To copy to clipboard, switch view to plain text mode 
    and then in super class make connection
    Qt Code:
    1. connect(child1, SIGNAL(clickCounChanged(int)), child2, SLOT(setClickCount(int)));
    To copy to clipboard, switch view to plain text mode 
    or can't it be done?

  5. #5
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: changing values for one class and showing it ..!

    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...

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: changing values for one class and showing it ..!

    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.

  7. #7
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: changing values for one class and showing it ..!

    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...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.