Results 1 to 5 of 5

Thread: casting complex widget pointers

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default casting complex widget pointers

    Hello,

    I've got a class facading two classes, each with its own UI. I don't know at compile time which of these UIs will be shown.

    How can I make a function that returns a pointer to on of the UIs:
    Qt Code:
    1. QWidget* facade::widget()
    2. {
    3. if (suchAndSuc)
    4. return new class1UI;
    5. else
    6. return class2UI;
    7. }
    To copy to clipboard, switch view to plain text mode 

    or is this the wrong approach?

    I'm trying to keep everything as modular as possible - each class should bring its own UI with it - but I don't know which classes will be used. So how do I build a form with widgets without knowing their type?
    Could just make sure that all the UIs have a common type as parent and pass this as a pointer? i.e. CommonUI* widget(){return new class1UI;}

    thanks
    K

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: casting complex widget pointers

    If you'll just be passing QWidget pointers, you should be safe (that's what Designer does). Then you can use qobject_cast to cast the result to more complex types if you need them.

  3. The following user says thank you to wysota for this useful post:

    TheKedge (9th February 2007)

  4. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: casting complex widget pointers

    well, I've got something like:
    Qt Code:
    1. class UI_one : public QWidget
    2. {
    3. private:
    4. QLabel* l;
    5. QTextEdit* txt;
    6. //some signals and slots
    7. }
    8.  
    9. class UI_two : public QWidget
    10. {
    11. //...
    12. }
    13.  
    14. //facade implementation
    15. QWidget* facadeUI::showUI
    16. {
    17. if(condition)
    18. //need to pass pointer to class UI_one
    19. return (QWidget*)new UI_one;//is cast like this ok?
    20. else
    21. //need to pass pointer to class UI_two
    22. return (QWidget*)new UI_one;//???
    23. }
    24.  
    25. //building the user interface
    26. CompleteUI::show()
    27. {
    28. QWidget* someWidget = facade->showUI()
    29. someWidget ->show();//shows on its own
    30. //or
    31. someLayout->addWidget(someWidget );//how it as part of something else
    32. }
    To copy to clipboard, switch view to plain text mode 

    is that what you mean? - can I down-cast for the pass and still use them properly?
    thanks
    K

  5. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: casting complex widget pointers

    ok, I've done it, and it works fine.
    I didn't find it obvious that casting a a pointer to something derived from a widget to a widget pointer would work.

    thanks for the help,
    K

  6. #5
    Join Date
    Jan 2006
    Location
    Mountain View, CA
    Posts
    279
    Thanked 42 Times in 37 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: casting complex widget pointers

    You don't need those casts either.

    Qt Code:
    1. QWidget* facadeUI::showUI
    2. {
    3. if(condition)
    4. //need to pass pointer to class UI_one
    5. return new UI_one;
    6. else
    7. //need to pass pointer to class UI_two
    8. return new UI_two;
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

    Also make sure you have a guard so that you don't create new objects everytime showUI is called (unless that is what you intended).
    Save yourself some pain. Learn C++ before learning Qt.

  7. The following user says thank you to Chicken Blood Machine for this useful post:

    TheKedge (9th February 2007)

Similar Threads

  1. Pin/Unpin Dock Widget
    By charlesD in forum Newbie
    Replies: 1
    Last Post: 21st June 2006, 06:57
  2. Replies: 4
    Last Post: 24th March 2006, 22:50
  3. [Qt 4.1.0] Split a widget on demand
    By Townk in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 14:16

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.