PDA

View Full Version : Passing variables between forms.



Splatify
20th February 2011, 13:04
Ok i have been reading 100s of articles and posts on this and still cannot seem to get this to work!!

I have a login form where a database is queried. The username and the password are stored in variables. How do i use these variable on another form.

Thanks for your time and trouble :)

kornicameister
20th February 2011, 13:29
that's simple
once you established the connection you can obtain these information wherever you want
creating QSqlDatabase object and using appropriate getters of it, take a look on QSqlDatabase documentation and you will know which one to use :)

tip, create an QSqlDatabase object like this

QSqlDatabase db = QSqlDatabase::database(connectionName);
only thinq you have to keep somewhere is connectionName, personally I also have static structure to hold some vital information where connectionName is one of them

Splatify
20th February 2011, 14:17
What if i just want to store the username in a string variable and pass this between the forms? I have tried various things but cannot seem to get it to work. Thanks

kornicameister
20th February 2011, 15:42
than you can either define setter in such form or introduce it into constructor
than from within main class (singleton) of your application You could set what you want in the form.

or

If by passing between forms you mean passing it between already existing variable than personally I would introduce something like asking signal and answer signal

Splatify
20th February 2011, 17:50
Could you give me an example of how to do this?

say i have a login form and a mainwindow, how would i use the username from the login form on the main window?

Thanks very much :)

squidge
20th February 2011, 19:00
There isn't a single way of solving this (like most problems)

Have a public method on your main window which accepts a username and password?

Or perhaps the login form could be given a structure which it fills in and returns.

Or you could use a singleton which contains such information and everyone accesses.

Splatify
20th February 2011, 19:08
Hi squidge... could you show me an example of how to do this? If not, could your direct me to an article of how to do this.

I don't care if the code is messy, etc... i just want to get this to work. I'll tidy it up later :)

Thanks so much

squidge
20th February 2011, 22:27
What bit are you struggling with?

I assume you already know how to create a public method, thats basic C++.
The contents of that method - assigning parameters to class member variables, is also basic C++.

So the only part left is the actual call, and so you simply instantiate a copy of the login form from within your main window (either allocate it on the stack and call a function which doesn't return until the login dialog is closed, or use the 'new' operator to allocate from the heap)

nish
21st February 2011, 07:41
Hi squidge... could you show me an example of how to do this? If not, could your direct me to an article of how to do this.

I don't care if the code is messy, etc... i just want to get this to work. I'll tidy it up later :)

Thanks so much

This is not a Qt problem, its a C++ problem. How do you pass data around classes?

Splatify
21st February 2011, 11:12
Ok I have successfully managed to do this now. Thanks so much for all your help. It really is appreciated :)

Edit:

Here is a link:
http://www.codeproject.com/KB/cs/pass_data_between_forms.aspx
I found which helped explain things in a fair amount of detail. I decided to use the first approach as it was the easiest and involved changing less of my code. In future I think i will plan a bit more before starting a project :)

Thanks once again.