Access variable in another form.
-
wrote on 12 Feb 2020, 05:38 last edited by
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?
-
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?
@Shivam-Sharma You can't access local variables from outside of a function.
You should reconsider your design... -
wrote on 12 Feb 2020, 09:19 last edited by
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?
-
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?
@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... -
wrote on 12 Feb 2020, 09:48 last edited by
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?
-
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?
@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.
-
wrote on 12 Feb 2020, 11:15 last edited by
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. -
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.@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 ?
1/8