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 12 Feb 2020, 05:38 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?

    J 1 Reply Last reply 12 Feb 2020, 05:40
    0
    • S Shivam Sharma
      12 Feb 2020, 05:38

      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?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 12 Feb 2020, 05:40 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 12 Feb 2020, 09:19 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?

        J 1 Reply Last reply 12 Feb 2020, 09:34
        0
        • S Shivam Sharma
          12 Feb 2020, 09:19

          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?

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 12 Feb 2020, 09:34 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 12 Feb 2020, 09:48 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?

            J 1 Reply Last reply 12 Feb 2020, 09:58
            0
            • S Shivam Sharma
              12 Feb 2020, 09:48

              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?

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 12 Feb 2020, 09:58 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 12 Feb 2020, 11:15 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.

                J 1 Reply Last reply 12 Feb 2020, 11:28
                0
                • S Shivam Sharma
                  12 Feb 2020, 11:15

                  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.

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 12 Feb 2020, 11:28 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

                  1/8

                  12 Feb 2020, 05:38

                  • Login

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