Results 1 to 8 of 8

Thread: Problem with dynamic_cast

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem with dynamic_cast

    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:
    Qt Code:
    1. class element{
    2. public:
    3. int position(){return pos;}
    4. private:
    5. int pos
    6. }
    To copy to clipboard, switch view to plain text mode 
    A widget that contains it:
    Qt Code:
    1. class widget : public QWidget{
    2. public:
    3. widget(element *elem):el(elem){};//I also pass parent to the constructor of qwiget
    4. element* element(){return el;}
    5. private:
    6. element* el;
    7. }
    To copy to clipboard, switch view to plain text mode 

    The principal widget is a QTabWidget and it contains a vector of elements.
    Qt Code:
    1. class main : public QTabWidget{
    2. private:
    3. QVector<element> elements;
    4. }
    To copy to clipboard, switch view to plain text mode 
    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.
    Qt Code:
    1. //inside main
    2. addTab(new widget(&elements[i]);
    To copy to clipboard, switch view to plain text mode 

    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!
    Last edited by vratojr; 11th April 2006 at 17:44.

Similar Threads

  1. Very strange socket programming problem
    By montylee in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2008, 12:05
  2. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  3. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47
  4. problem with paint and erase in frame
    By M.A.M in forum Qt Programming
    Replies: 9
    Last Post: 4th May 2008, 20:17
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.