PDA

View Full Version : Shared Object class between multiple Widgets



vladozar
18th February 2012, 20:13
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

QWidget widgetA
QWidget widgetB
// more QWidgets are in my program, but for example purpose, we will use two widgets
// Both widgets are sepereate dialogs, widgets, mainWindows and have theier own classes and *.h and *.cpp files

// And some QObject that also has its own class and and *.h and *.cpp file
QObject sharedData
// And in the sharedData, lets say we have some public items (declared in the header file):
QString string
QString stringTwo
int number


What I wnat to do, is when widgetA changes some part of the sharedData Object,

// in the widgetA, some sharedData items are chaged like
sharedData->string = "apple";
sharedData->num = 10;


I need to be able that widgetB would read the changes made by widgetA


// lets say
qDebug()<<sharedData->string;
//will return "apple" and not an emtpy string


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

Thanks to all. Hope to here from some one.:)

Lykurg
18th February 2012, 22:10
If you can't handle pointer (or it gets to complicated) you can use the singleton approach. (See our wiki)

lni
19th February 2012, 09:37
Use QExplicitlySharedDataPointer and QSharedData

vladozar
5th March 2012, 19:55
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

wysota
5th March 2012, 20:09
Why don't you just pass a pointer to the object to all those widgets as already suggested?

vladozar
5th March 2012, 20:12
my question is how to pass it? any code examples?

wysota
6th March 2012, 00:03
class X {
public:
void setData(Data *ptr) {
m_data = ptr;
}
private:
Data *m_data;
};

Or use the singleton (http://lmgtfy.com?q=singleton) pattern as already advised.

MarekR22
6th March 2012, 08:20
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:
void CommonDataQObject::setSomeString(const QString &a) {
if (this->mSomestring!=a) {
this->mSomestring=a;
emit someStringChaned(a); // emit signal which should be connected to other widgets
}
}

wysota
6th March 2012, 09:49
The problem is lack of C++ skills.

kito
6th March 2012, 10:30
i think you need to start with OOP.That can solve your problem