Results 1 to 3 of 3

Thread: Sharing data between objects

  1. #1
    Join Date
    Jan 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Sharing data between objects

    This is probably a simple c++ question, but it's driving me up a wall.

    Let say I have two variables:
    Qt Code:
    1. bool running;
    2. QList<double> * data;
    To copy to clipboard, switch view to plain text mode 
    The first simply tracks if a timer is running, and the second is a pointer to a reasonably large list of numbers. I want to be able to access these vars from every QFrame, QwtPlot, QWidget, etc in my program. As of now, I'm passing each through the constructor of each QObject. Previously I've set up nasty networks of signals and slots that bounce around between the objects to find one where the variable is in scope. There has got to be a better way.

    Is this the sort of thing that is allowable for declaring globally in main.cpp? There are so many warnings about that being bad...What is the Qt way of sharing these variables throughout my program?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Sharing data between objects

    You can declare a global variable or implement a singleton class for holding it.

  3. #3
    Join Date
    Jan 2006
    Posts
    46
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sharing data between objects

    I would recommend a singleton class.
    I think global variables are a bad practice, but nothing more. I don't think they're as nasty as their detractors can say, but they're, certainly, not the neater solution either. I, personally, try to not to use them.

    Cheers
    Kandalf
    There's no place like ~

Similar Threads

  1. urgent!!!!!......sharing data
    By alphajoseph in forum Newbie
    Replies: 9
    Last Post: 16th September 2010, 11:42
  2. Sharing data between threads
    By bbui210 in forum Qt Programming
    Replies: 15
    Last Post: 19th October 2008, 17:56
  3. Sharing data across threads
    By jphn_crichton in forum Qt Programming
    Replies: 11
    Last Post: 5th May 2008, 18:29
  4. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53
  5. Help me to use QtSharedMemory to share the data objects
    By gtthang in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 11:50

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.