vratojr
11th April 2006, 17:40
Hi,even if the following code involves a bit of Qt I post it here because I think it's a more general problem.
The situation is the following:
I have a class that know its position inside an array:
class element{
public:
int position(){return pos;}
private:
int pos
}
A widget that contains it:
class widget : public QWidget{
public:
widget(element *elem):el(elem){};//I also pass parent to the constructor of qwiget
element* element(){return el;}
private:
element* el;
}
The principal widget is a QTabWidget and it contains a vector of elements.
class main : public QTabWidget{
private:
QVector<element> elements;
}
Using some slots, I create some elements and add them to the QVector (and coherently I set theirs pos memebers) and then use them to initialize widgets and add them to the tabwidget.
//inside main
addTab(new widget(&elements[i]);
Now,the problem. If from inside the tabwidget I want to access to *el inside widget (inside a particular tab) I have to use a dynamic cast.
So I use:
widget *w = dynamic_cast<widget*>(QTabWidget::widget(k));
if I then use w->position() to get its position, I sometimes get strange numbers.
It seems that,during the cast the pointer *el inside widget, became dangling...why?
Thanks!
The situation is the following:
I have a class that know its position inside an array:
class element{
public:
int position(){return pos;}
private:
int pos
}
A widget that contains it:
class widget : public QWidget{
public:
widget(element *elem):el(elem){};//I also pass parent to the constructor of qwiget
element* element(){return el;}
private:
element* el;
}
The principal widget is a QTabWidget and it contains a vector of elements.
class main : public QTabWidget{
private:
QVector<element> elements;
}
Using some slots, I create some elements and add them to the QVector (and coherently I set theirs pos memebers) and then use them to initialize widgets and add them to the tabwidget.
//inside main
addTab(new widget(&elements[i]);
Now,the problem. If from inside the tabwidget I want to access to *el inside widget (inside a particular tab) I have to use a dynamic cast.
So I use:
widget *w = dynamic_cast<widget*>(QTabWidget::widget(k));
if I then use w->position() to get its position, I sometimes get strange numbers.
It seems that,during the cast the pointer *el inside widget, became dangling...why?
Thanks!