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. Defining QString to be used in several classes
Forum Updated to NodeBB v4.3 + New Features

Defining QString to be used in several classes

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 1.9k Views 3 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    How can I define a QString variable to be able to use it in several classes?
    Thank you.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Best design is with signals and slots.

      The QString class lives in one class and when changed, the owner class emits the string to the other classes.
      They can then keep a copy.

      Globals variables are not a good idea.

      G 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi
        Best design is with signals and slots.

        The QString class lives in one class and when changed, the owner class emits the string to the other classes.
        They can then keep a copy.

        Globals variables are not a good idea.

        G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        @mrjj
        If I assign a value to a variable in the program, is there a signal I can use to send the value to the other class?

        D mrjjM 2 Replies Last reply
        0
        • G gabor53

          @mrjj
          If I assign a value to a variable in the program, is there a signal I can use to send the value to the other class?

          D Offline
          D Offline
          DRoscoe
          wrote on last edited by
          #4

          @gabor53 you need to define one

          1 Reply Last reply
          0
          • G gabor53

            @mrjj
            If I assign a value to a variable in the program, is there a signal I can use to send the value to the other class?

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

            @gabor53
            No really, you should just define a new signal.
            If you use a setter function , you can emit there.
            pseudo code:

            class MyStrOwner  : QObject {
            Q_OBJECT
            private:
            QString Name; // let just say its name
            signals;
            void NameChanged( QString );
            public;
            void setName( QString  NewName) {  
              Name=NewName;
              emit NameChanged(Name);
            }
            }
            

            Then if u say anywhere

            MyStrOwner oneclass;
            oneclass.setName("hello");

            it will auto emit

            G W 2 Replies Last reply
            1
            • mrjjM mrjj

              @gabor53
              No really, you should just define a new signal.
              If you use a setter function , you can emit there.
              pseudo code:

              class MyStrOwner  : QObject {
              Q_OBJECT
              private:
              QString Name; // let just say its name
              signals;
              void NameChanged( QString );
              public;
              void setName( QString  NewName) {  
                Name=NewName;
                emit NameChanged(Name);
              }
              }
              

              Then if u say anywhere

              MyStrOwner oneclass;
              oneclass.setName("hello");

              it will auto emit

              G Offline
              G Offline
              gabor53
              wrote on last edited by
              #6

              @mrjj
              Thank you!

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by SGaist
                #7

                Hi,

                Just a fix to @mrjj code. In the setName slot:

                void setName( QString  newName) {  
                    if (newName == name) {
                        return;
                    }
                  name = newName;
                  emit nameChanged(name);
                }
                

                Otherwise you can trigger a signal storm if you connect the nameChanged signal to an object that has a connection to the setName slot following the same approach.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • mrjjM mrjj

                  @gabor53
                  No really, you should just define a new signal.
                  If you use a setter function , you can emit there.
                  pseudo code:

                  class MyStrOwner  : QObject {
                  Q_OBJECT
                  private:
                  QString Name; // let just say its name
                  signals;
                  void NameChanged( QString );
                  public;
                  void setName( QString  NewName) {  
                    Name=NewName;
                    emit NameChanged(Name);
                  }
                  }
                  

                  Then if u say anywhere

                  MyStrOwner oneclass;
                  oneclass.setName("hello");

                  it will auto emit

                  W Offline
                  W Offline
                  Wurgl
                  wrote on last edited by
                  #8

                  @mrjj what about QSharedData or similar from that QShared* classes. Well, the signal might still be needed for detecting a change, but you do not need to copy the data …

                  mrjjM 1 Reply Last reply
                  2
                  • W Wurgl

                    @mrjj what about QSharedData or similar from that QShared* classes. Well, the signal might still be needed for detecting a change, but you do not need to copy the data …

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

                    @Wurgl said in Defining QString to be used in several classes:

                    QSharedData

                    You are right. That would be the right way if programmer
                    want to share and update same data.

                    I just saw "be able to use it in several classes?" and though
                    about global variables and how not to do it :)

                    @SGaist
                    Thank you. :)

                    1 Reply Last reply
                    1

                    • Login

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