Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

My program printing default other class variable value (even if its changed)



  • Hello, I have simple program that reads other class variable value in loop (QTimer), but it's always printing default value (program started value).

    Output:

    Hello from QTimer, m_number is now: 0
    Hello from setNumber(), m_number is now: 2
    Hello from setNumber(), m_number is now: 3
    Hello from setNumber(), m_number is now: 4
    Hello from setNumber(), m_number is now: 5
    Hello from QTimer, m_number is now: 0
    Hello from setNumber(), m_number is now: 6
    Hello from setNumber(), m_number is now: 7
    Hello from setNumber(), m_number is now: 8
    Hello from setNumber(), m_number is now: 9
    Hello from QTimer, m_number is now: 0
    Hello from setNumber(), m_number is now: 10
    Hello from setNumber(), m_number is now: 11
    Hello from setNumber(), m_number is now: 12
    Hello from QTimer, m_number is now: 0
    Hello from QTimer, m_number is now: 0
    Hello from setNumber(), m_number is now: 13
    Hello from setNumber(), m_number is now: 14
    Hello from setNumber(), m_number is now: 15
    Hello from setNumber(), m_number is now: 16
    Hello from QTimer, m_number is now: 0
    Hello from setNumber(), m_number is now: 17
    Hello from QTimer, m_number is now: 0
    Hello from QTimer, m_number is now: 0
    

    Project: https://github.com/fire7d/TestProject


  • Lifetime Qt Champion

    @BD9a

    Hi
    well, they are just separate by nature.

    int a;
    int b;

    Would you expect a and b to be in sync ?

    Technically we could say that they don't share the same memory location and hence they have different values.

    Also if an instance (a copy) would always be in sync with other copies. it would be impossible to then have more than one with different values so a lot of different apps would be hard to make.

    If you remove the one in main. it just means the Printer has the only one.
    But yes, you could use the Printer's one for setContextProperty if you make a public access function.


  • Lifetime Qt Champion

    @BD9a Your Printer has its own Config instance which has nothing to do with the one you create in main.



  • @jsulm But what is the diffrence? I cant find any logical argument, can u point out why it's working that way?


  • Lifetime Qt Champion

    @BD9a If you have 2 Config instances and you change one od them then the other is not affected by that change, right?



  • @jsulm Yep


  • Lifetime Qt Champion

    @BD9a
    So all good ?
    If you want the Printer clas to use the Config from main.cpp, you should give
    it as a paramter in constructor ( as a pointer or a ref)



  • @mrjj But why it's working only by that way? I need some sort of technical explaintation, why mine code doesnt work (tech pov). What if I will remove Config instance in main, and only "Printer" class will have it?


  • Lifetime Qt Champion

    @BD9a

    Hi
    well, they are just separate by nature.

    int a;
    int b;

    Would you expect a and b to be in sync ?

    Technically we could say that they don't share the same memory location and hence they have different values.

    Also if an instance (a copy) would always be in sync with other copies. it would be impossible to then have more than one with different values so a lot of different apps would be hard to make.

    If you remove the one in main. it just means the Printer has the only one.
    But yes, you could use the Printer's one for setContextProperty if you make a public access function.


Log in to reply