PDA

View Full Version : problem with a list of pointers



maider
30th November 2009, 09:17
hey, I have done a list of pointers of QWidget. Everything is well done but I want to simplify the last declaration with a for.
My code is the following one:

QList <QWidget*> v;
QVector <int> v_pos(60);
int i_sc =0;

ONE *p1 = new ONE();
v.insert(i_sc,p1);
v_pos[i_sc]= SC_ONE;
i_sc++;

TWO *p1 = new TWO();
v.insert(i_sc,p1);
v_pos[i_sc]= SC_TWO;
i_sc++;

int i;
QList<QWidget*> list;
list.replace(SC_ONE,p1);
list.replace(SC_TWO,p2);

p1->getScreenDirectionlist(&list);
p2->getScreenDirectionlist(&list);

Now, I have changed
"p1->getScreenDirectionlist(&list);" for "v.at(0)->getScreenDirectionlist(&list);"
but It give this error Description Resource Path Location Type
invalid conversion from `QWidget* const' to `ONE*' main.cpp tfp003 83 C/C++ Problem

Do you know ho can i solve it?

thanks

lyuts
30th November 2009, 09:47
What about foreach ?


foreach (QWidget *widget, list) {
...
}

maider
30th November 2009, 10:04
how can I use foreach?what does it do?

lyuts
30th November 2009, 10:06
Here (http://doc.trolltech.com/4.5/containers.html#the-foreach-keyword) is its description.

maider
30th November 2009, 10:16
but is i do with a foreach,i have the same problem.
foreach (QWidget *widget, list) {
v.at(0)->getScreenDirectionlist(&list);
}

What I need is to link my pointers of the widget that I have got, with the whole list in a for. this:p1->getScreenDirectionlist(&list); but with a for to make shorter the code.

lyuts
30th November 2009, 10:26
No, you have to do this


foreach (QWidget *widget, yourWidgetsList) {
widget->getScreenDirectionlist(some_other_list);
}


There is a little bit confusion about v and list. What are they for?

Lykurg
30th November 2009, 12:45
Now, I have changed
"p1->getScreenDirectionlist(&list);" for "v.at(0)->getScreenDirectionlist(&list);"
but It give this error Description Resource Path Location Type
invalid conversion from `QWidget* const' to `ONE*' main.cpp tfp003 83 C/C++ Problem

It's a pure C++ problem. The at operator delivers back a pointer to a widget as you have defined your list v! So you have to cast the pointer to your class. Use qobject_cast for that.