Results 1 to 10 of 10

Thread: Shared Object class between multiple Widgets

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2009
    Posts
    17
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Shared Object class between multiple Widgets

    HI

    I'm trying to creat an Object that will contains some data and that will be avaliable to multiple Widgets. Also, if one Widget changes some data in the Object, next time some other Widget reads the data from Object, it will have the new chaged data.

    For example.

    Lets say we have
    Qt Code:
    1. QWidget widgetA
    2. QWidget widgetB
    3. // more QWidgets are in my program, but for example purpose, we will use two widgets
    4. // Both widgets are sepereate dialogs, widgets, mainWindows and have theier own classes and *.h and *.cpp files
    5.  
    6. // And some QObject that also has its own class and and *.h and *.cpp file
    7. QObject sharedData
    8. // And in the sharedData, lets say we have some public items (declared in the header file):
    9. QString string
    10. QString stringTwo
    11. int number
    To copy to clipboard, switch view to plain text mode 

    What I wnat to do, is when widgetA changes some part of the sharedData Object,
    Qt Code:
    1. // in the widgetA, some sharedData items are chaged like
    2. sharedData->string = "apple";
    3. sharedData->num = 10;
    To copy to clipboard, switch view to plain text mode 

    I need to be able that widgetB would read the changes made by widgetA
    Qt Code:
    1. // lets say
    2. qDebug()<<sharedData->string;
    3. //will return "apple" and not an emtpy string
    To copy to clipboard, switch view to plain text mode 


    My question is how do i set up my object and how do I implament this object withing widgets. I know, I have to use pointers, but I have problem in initial setup.

    Thanks to all. Hope to here from some one.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Shared Object class between multiple Widgets

    If you can't handle pointer (or it gets to complicated) you can use the singleton approach. (See our wiki)

  3. #3
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Shared Object class between multiple Widgets

    Use QExplicitlySharedDataPointer and QSharedData

  4. The following user says thank you to lni for this useful post:

    vladozar (5th March 2012)

  5. #4
    Join Date
    Apr 2009
    Posts
    17
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Shared Object class between multiple Widgets

    Use QExplicitlySharedDataPointer and QSharedData
    Thanks for the answer, but I have read this pages, but still have trouble figuring out how to actually use it. It is still unclear to me how to actuall implament it. Is it possible to get an actuall working example of how this works? Are there any demos or example of it?
    thanks

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

    Default Re: Shared Object class between multiple Widgets

    Why don't you just pass a pointer to the object to all those widgets as already suggested?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #6
    Join Date
    Apr 2009
    Posts
    17
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Shared Object class between multiple Widgets

    my question is how to pass it? any code examples?

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

    Default Re: Shared Object class between multiple Widgets

    Qt Code:
    1. class X {
    2. public:
    3. void setData(Data *ptr) {
    4. m_data = ptr;
    5. }
    6. private:
    7. Data *m_data;
    8. };
    To copy to clipboard, switch view to plain text mode 

    Or use the singleton pattern as already advised.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #8
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Shared Object class between multiple Widgets

    I don't understand problem?
    Why you don't just use signal slot mechanism?
    setters as a slots and somthingChanged signals should do the trick.
    You have some QObject storing all shared values with setters getters and change signals and just connecting appropriative signa-slots between this object and other widgets.
    Data sharing is not needed!
    Standard pattern of setter with change signal:
    Qt Code:
    1. void CommonDataQObject::setSomeString(const QString &a) {
    2. if (this->mSomestring!=a) {
    3. this->mSomestring=a;
    4. emit someStringChaned(a); // emit signal which should be connected to other widgets
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Shared Object class between multiple Widgets

    The problem is lack of C++ skills.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #10
    Join Date
    Mar 2012
    Location
    Lesotho
    Posts
    33
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Shared Object class between multiple Widgets

    i think you need to start with OOP.That can solve your problem

Similar Threads

  1. Shared Object In Qt and Linking to main application
    By Ashutosh2k1 in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2011, 10:36
  2. libQtWebKit.so.4: cannot open shared object file
    By Qt Coder in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2010, 13:27
  3. KDE shared object file is missing
    By SimbadSubZero in forum Qt Programming
    Replies: 0
    Last Post: 13th December 2009, 14:43
  4. Replies: 3
    Last Post: 16th May 2007, 11:07
  5. Replies: 11
    Last Post: 7th July 2006, 13:09

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
  •  
Qt is a trademark of The Qt Company.