Results 1 to 5 of 5

Thread: Share a variable between forms

  1. #1
    Join Date
    Apr 2008
    Location
    Karaj,Iran
    Posts
    43
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Share a variable between forms

    hey folks,
    I wanted to know what would be the best way to share a variable between all forms of my application? I have
    QHash<QString,QString> configs;
    which configs holds some configuration of my application which multiple forms need to access,what would be the best approach to define configs?
    --
    thanks in adavnce

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Share a variable between forms

    add method to widget which holds this hash and return it, i.e.
    Qt Code:
    1. QHash<QString,QString> MyWidget::config() const
    2. {
    3. return m_config;
    4. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Apr 2008
    Location
    Karaj,Iran
    Posts
    43
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Share a variable between forms

    Quote Originally Posted by spirit View Post
    add method to widget which holds this hash and return it, i.e.
    Qt Code:
    1. QHash<QString,QString> MyWidget::config() const
    2. {
    3. return m_config;
    4. }
    To copy to clipboard, switch view to plain text mode 
    I don't think that would do the job,say I have 2 forms(myWidget and Mywidget2) which are not related at all,so I can't call MyWidget::config() from Mywidget2

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Share a variable between forms

    then make this variable global.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Apr 2008
    Location
    Karaj,Iran
    Posts
    43
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Share a variable between forms

    yeah,that's what I did at first,but using global variables is not recommended,this helped so much
    http://geeklab.wikidot.com/cpp-singleton-pattern
    and this
    http://www.devarticles.com/c/a/Cplus...attern-Part-I/

Similar Threads

  1. nmake error during .pro compiling
    By mattia in forum Installation and Deployment
    Replies: 5
    Last Post: 18th June 2008, 11:15

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.