Qt-interest Archive, February 2008
How to pass a QValueList?
Message 1 in thread
Hi,
This is a piece code sample of my program
class B{
//....
func_b();
//...
};
class A{
//...
B b;
QValueList<QLable> label_valuelist;
func_a();
//...
};
In func_a(), some QValueList::append() ,remove() operations will be called to
change label_valuelist; and b.func_b() will be called. In B::func_b()
label_valuelist
will also be changed;
So how can I use label_valuelist in class B?
my thought is :
In class B,add
QValueList<QLabel> label_valuelist2;
setValueList(QValueList<QLabel> l)
{
label_valuelist2=l;
}
And in A::func_a(), add
b.setValueList(label_valuelist);
But it seems not work.
So how to do this?
Thanks
--
[ signature omitted ]
Message 2 in thread
On Sunday 24 February 2008 05:08:25 pm jiang jefix wrote:
> Hi,
> This is a piece code sample of my program
>
> class B{
> //....
> func_b();
> //...
> };
>
> class A{
> //...
> B b;
> QValueList<QLable> label_valuelist;
> func_a();
> //...
> };
>
> In func_a(), some QValueList::append() ,remove() operations will be called
> to change label_valuelist; and b.func_b() will be called. In B::func_b()
> label_valuelist
> will also be changed;
> So how can I use label_valuelist in class B?
If you want to use the same label_valuelist, why not just expose it in class
A?
class A
{
public:
QValueList<QLabel> *getLabelValueList() { return &label_valuelist; };
// and all the member functions of A...
private:
B b;
QValueList<QLabel> label_valuelist;
}
However if A has_a B, and B needs to operate on a member of A, then you
probably have confused class structure.
Maybe a move member refactoring to put the label_valuelist into B, then you
can either do a move method refactoring to put the getLabelValueList() method
into B, or better still, add some more meaningful methods to B and invoke
those instead.
It is a bit hard to tell - you haven't really described what you are trying to
do.
Brad
--
[ signature omitted ]
Message 3 in thread
On Sunday 24 February 2008 07:08:25 jiang jefix wrote:
Use a pointer or a reference in B.
// if that's a QLabel list they will usually be pointers anyway, so use
pointers.
typedef QValueList<QLabel*> LabelList;
class B{
LabelList* m_pLabelList;
public:
B(LabelList* pList): m_pLabelList(pList){}
...
};
class A{
...
LabelList m_labelList;
B m_b;
};
A::A(): m_labelList(), m_b(&m_labelList) { // some compilers might complain
here.
}
or use setters and so on.
Main point being: use pointers or references here. You don't want to copy
lists across a function call. So your setValue function should look like:
B::setList(LabelList*); // usually in case the list may not exist. You'll
check the member for 0. Usually used if the parameter is optional or if you
wish to share the list between classes.
B::setList(LabelList const& list); // when you pass the list as an input
parameter. The & is to keep the compiler from making a copy of the list on
the stack, the const is to indicate the called function will not modify your
list. You do not expect the contents oft he LabelList to have changed, BUT
the individual objects pointed to may have.
B::setList(LabeList& list); // used when list is inout or out. You'd expect
the contents of list to have changed after this call.
What you did is pass a copy of the list from A to B. So whatever modification
were done in B's function, the list in A was never modified because b got a
copy of A's list, not the list itself.
> Hi,
> This is a piece code sample of my program
>
> class B{
> //....
> func_b();
> //...
> };
>
> class A{
> //...
> B b;
> QValueList<QLable> label_valuelist;
> func_a();
> //...
> };
>
> In func_a(), some QValueList::append() ,remove() operations will be called
> to change label_valuelist; and b.func_b() will be called. In B::func_b()
> label_valuelist
> will also be changed;
> So how can I use label_valuelist in class B?
>
> my thought is :
> In class B,add
> QValueList<QLabel> label_valuelist2;
> setValueList(QValueList<QLabel> l)
> {
> label_valuelist2=l;
> }
> And in A::func_a(), add
> b.setValueList(label_valuelist);
>
> But it seems not work.
> So how to do this?
> Thanks
--
[ signature omitted ]