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. Access variable in another form.
Forum Update on Monday, May 27th 2025

Access variable in another form.

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 687 Views
  • 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.
  • S Offline
    S Offline
    Shivam Sharma
    wrote on last edited by
    #1

    There is a variable : static char t1[512] in a function serialrec() in one form named setup.h

    I want to use this in another form mainwindow.h but I have no idea how to go about. Can someone help?

    jsulmJ 1 Reply Last reply
    0
    • S Shivam Sharma

      There is a variable : static char t1[512] in a function serialrec() in one form named setup.h

      I want to use this in another form mainwindow.h but I have no idea how to go about. Can someone help?

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

      @Shivam-Sharma You can't access local variables from outside of a function.
      You should reconsider your design...

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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Shivam Sharma
        wrote on last edited by
        #3

        Reconsidering the whole design would be changing everything. Is there no way that I declare it public in the .h file and access it or something?

        jsulmJ 1 Reply Last reply
        0
        • S Shivam Sharma

          Reconsidering the whole design would be changing everything. Is there no way that I declare it public in the .h file and access it or something?

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

          @Shivam-Sharma I don't say you have to reconsider whole design of your application.
          Why do you want to access a local variable in a function from somewhere else?! There is really no need to do so.
          "Is there no way that I declare it public in the .h file and access it or something?" - of course there is. Why not add it as member variable in your class? But since you did not provide much information about your use case I can't tell what would be best...

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

          1 Reply Last reply
          2
          • S Offline
            S Offline
            Shivam Sharma
            wrote on last edited by
            #5

            Ok, so t1[512] from the form setup.h has a string that is the GPS String from serial port. I want to use this same string to append it in another form ui that is mainwindow.ui in a text edit box. But not being able to access it only then how will I append it?

            jsulmJ 1 Reply Last reply
            0
            • S Shivam Sharma

              Ok, so t1[512] from the form setup.h has a string that is the GPS String from serial port. I want to use this same string to append it in another form ui that is mainwindow.ui in a text edit box. But not being able to access it only then how will I append it?

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

              @Shivam-Sharma said in Access variable in another form.:

              But not being able to access it only then how will I append it?

              Simply emit a signal where you get that data and connect that signal to a slot in your UI where you then append it. This is how it is done in Qt usually.

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

              1 Reply Last reply
              4
              • S Offline
                S Offline
                Shivam Sharma
                wrote on last edited by
                #7

                Can you give a demo of the statement, if:

                static char t1[512] needs to be accessed from serialrec() function from setup.h form
                in mainwindow.h form.

                jsulmJ 1 Reply Last reply
                0
                • S Shivam Sharma

                  Can you give a demo of the statement, if:

                  static char t1[512] needs to be accessed from serialrec() function from setup.h form
                  in mainwindow.h form.

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

                  @Shivam-Sharma said in Access variable in another form.:

                  Can you give a demo of the statement, if:

                  There are more than enough examples and demo apps for that.
                  Did you check: https://doc.qt.io/qt-5/signalsandslots.html ?

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

                  1 Reply Last reply
                  2

                  • Login

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