I indeed hat forgotten to include one header file into the pro file but this did not solve the problem.
I created two small examples.
The first works like I want it too.
It's basically like I described it in my previous post.
int main( int argc, char **argv )
{
stack.addWidget(new A());
stack.addWidget(new B());
CW *c = qobject_cast<CW*>(stack.currentWidget());
if(c)
c->apply();
else
qDebug("cast not sucessful - pointer is 0 %x",c);
stack.setCurrentIndex(1);
c = dynamic_cast<CW*>(stack.currentWidget());
if(c)
c->apply();
else
qDebug("cast not sucessful - pointer is 0 %x",c);
return 1;
}
int main( int argc, char **argv )
{
QApplication a(argc, argv);
QStackedWidget stack;
stack.addWidget(new A());
stack.addWidget(new B());
CW *c = qobject_cast<CW*>(stack.currentWidget());
if(c)
c->apply();
else
qDebug("cast not sucessful - pointer is 0 %x",c);
stack.setCurrentIndex(1);
c = dynamic_cast<CW*>(stack.currentWidget());
if(c)
c->apply();
else
qDebug("cast not sucessful - pointer is 0 %x",c);
return 1;
}
To copy to clipboard, switch view to plain text mode
CW is the interface (pure virtual) inheriting from QWidget and A and B just implement this interface.
On both platforms (Mac gcc4, Linux gcc3) I get the same result:
apply A called
apply B called
apply A called
apply B called
To copy to clipboard, switch view to plain text mode
So there is no difference if I cast via dynamic_cast or qobject_cast.
Now I move CW, A and B into a Plugin. (The complete is in the attached cast2.zip)
After doing so I get different outputs:
#Linux gcc3
cast not sucessful - pointer is 0 0
cast not sucessful - pointer is 0 0
#Mac gcc4
cast not sucessful - pointer is 0 0
apply B called
#Linux gcc3
cast not sucessful - pointer is 0 0
cast not sucessful - pointer is 0 0
#Mac gcc4
cast not sucessful - pointer is 0 0
apply B called
To copy to clipboard, switch view to plain text mode
On the Mac(gcc4) the dynamic cast still works which doesn't using gcc3 on linux.
qobject_cast fails on both.
Is there a reason why it must fail? If so I would like to know about. And if not, what did I'm wrong?
I attach the sources of both projects.
cast
Building: qmake; make
Running: ./castt #linux
./cast.app/Contents/MacOS/castt #mac
cast2
Building: qmake -recursive; make
Running: cd bin
./castt #linux
./cast.app/Contents/MacOS/castt #mac
Bookmarks