Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. My program printing default other class variable value (even if its changed)
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 578 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BD9a
    wrote on last edited by BD9a
    #1

    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

    jsulmJ 1 Reply Last reply
    0
    • B BD9a

      @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?

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #8

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

      1 Reply Last reply
      4
      • B BD9a

        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

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        B 1 Reply Last reply
        2
        • jsulmJ jsulm

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

          B Offline
          B Offline
          BD9a
          wrote on last edited by BD9a
          #3

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

          jsulmJ 1 Reply Last reply
          0
          • B BD9a

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

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

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

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            B 1 Reply Last reply
            2
            • jsulmJ jsulm

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

              B Offline
              B Offline
              BD9a
              wrote on last edited by
              #5

              @jsulm Yep

              mrjjM 1 Reply Last reply
              0
              • B BD9a

                @jsulm Yep

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @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)

                B 1 Reply Last reply
                0
                • mrjjM mrjj

                  @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)

                  B Offline
                  B Offline
                  BD9a
                  wrote on last edited by
                  #7

                  @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?

                  mrjjM 1 Reply Last reply
                  0
                  • B BD9a

                    @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?

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

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

                    1 Reply Last reply
                    4

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved