Results 1 to 10 of 10

Thread: Passing variables between forms.

  1. #1
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Passing variables between forms.

    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

  2. #2
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    112
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Passing variables between forms.

    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
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::database(connectionName);
    To copy to clipboard, switch view to plain text mode 
    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
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

  3. #3
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing variables between forms.

    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

  4. #4
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    112
    Thanks
    8
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Passing variables between forms.

    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
    My schedule makes my cry
    My works makes my laugh
    My life makes... oh...no....

  5. #5
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Passing variables between forms.

    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

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing variables between forms.

    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.

  7. #7
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing variables between forms.

    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

  8. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing variables between forms.

    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)

  9. #9
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Passing variables between forms.

    Quote Originally Posted by Splatify View Post
    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?

  10. #10
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: Passing variables between forms.

    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/pas...een_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.
    Last edited by Splatify; 21st February 2011 at 11:17. Reason: updated contents

Similar Threads

  1. Replies: 1
    Last Post: 25th November 2010, 16:02
  2. Multiple Forms and vertical layout in forms
    By eva2002 in forum Qt Programming
    Replies: 0
    Last Post: 13th January 2010, 05:05
  3. Replies: 2
    Last Post: 25th June 2009, 19:47
  4. Replies: 2
    Last Post: 17th October 2006, 19:25
  5. pass variables between 2 forms
    By restiz in forum Qt Programming
    Replies: 7
    Last Post: 17th June 2006, 16:13

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.