Results 1 to 5 of 5

Thread: get my widget from QStackedWidget

  1. #1
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default get my widget from QStackedWidget

    Hi,

    I have a QSteckedWidget on my gui and I put my own widgets on it, showing the actual one depending on some choices made on the other parts of the gui. I can set this with QStackedWidget::setCurrentIndex().

    Now I need the reverse: I would like to use some functions that belong to the current widget. here is some pseudo-code:

    Qt Code:
    1. //I have a class like this:
    2. class myC : public QWidget
    3. {
    4. //...
    5. public:
    6. void myfunction(); // this is what I want to reach
    7. }
    8.  
    9. //this is in the mainWindow class somewhere:
    10. //I made the stackedwidget in the code, say with the name myStackedWidget
    11. myStackedWidget->setCurrentIndex(i); //shows the wanted widget correctly
    12.  
    13. //and here I need to access the current widget that is shown
    14.  
    15. myStackedWidhet->currentWidget(i)->myfunction();//does not work
    To copy to clipboard, switch view to plain text mode 

    the error message says:
    "class QWidget has no member named 'myfunction()'"

    This shows that what I can get with this function is a Widget, but not the myC. How can I solve this?
    Szilvi

  2. #2
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: get my widget from QStackedWidget

    try :

    Qt Code:
    1. myC *myWidget = static_cast<myC *>(myStackedWidhet->currentWidget(i));
    2. if ( myWidget )
    3. myWidget->myfunction();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: get my widget from QStackedWidget

    static_cast will always succeed, so the if condition will always be true. Use dynamic_cast

  4. #4
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: get my widget from QStackedWidget

    Thans, it works now. (of course I don't need i int the function currentWidget, but that is of course just a mistype)

    But there are some "but"-s.

    1st - static_cast works also
    2nd - why should I use dynamic_cast instead?
    3rd - what do I do exactly? How does a type cast like this works, why do I need it? I mean this does not look pretty enough compared to the feeling that this should not be a very new problem and I've just simply got used to that in the programming world if a problem should have been a problem for at least 1000 people before me than there is a pretty straightforward solution. This look a little bit strange to me that's all.
    Szilvi

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: get my widget from QStackedWidget

    static_cast will always return a non-zero pointer if given a non-zero pointer: regardless of safety. Your if() is testing the pointer for null as a safety check but this won't fail (nish's point above).

    dynamic_cast will only return non-zero pointer if the given pointer can be safely treated as a pointer to the type you are casting to.

    You need a cast because the QStackedWidget maintains a list of pointers to QWidget and currentWidget() returns these, not a pointer to your type. A pointer to your type is required in order to access methods unique to your type.
    Last edited by ChrisW67; 22nd August 2011 at 08:24.

Similar Threads

  1. Replies: 2
    Last Post: 16th February 2011, 13:59
  2. Shared widget by pages in a QStackedWidget
    By ouekah in forum Newbie
    Replies: 1
    Last Post: 7th March 2010, 11:01
  3. Shared widget by pages in a QStackedWidget
    By ouekah in forum Newbie
    Replies: 1
    Last Post: 7th March 2010, 11:00
  4. Shared widget by pages in a QStackedWidget
    By ouekah in forum Newbie
    Replies: 2
    Last Post: 7th March 2010, 10:45
  5. Replies: 4
    Last Post: 13th August 2007, 15:28

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.