Results 1 to 3 of 3

Thread: Dynamically Loading UI With Unspecified Slots

  1. #1
    Join Date
    Nov 2008
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Dynamically Loading UI With Unspecified Slots

    Hi. I want to dynamically load a .ui file. I know to use QUILoader.

    Supposing the widget the .ui file describes new signals and slots. If this was a compile-time problem, I could generate the ui_foo.h and implement the slot in code.

    Since this is all dynamic, when I load the .ui file, the widget will have slots with no definitions. I have constructed a simple example:

    Qt Code:
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <ui version="4.0">
    3. <class>Form</class>
    4. <widget class="QWidget" name="my_widget">
    5. <widget class="QPushButton" name="pushButton">
    6. </widget>
    7. </widget>
    8. <resources/>
    9. <connections>
    10. <connection>
    11. <sender>pushButton</sender>
    12. <signal>clicked()</signal>
    13. <receiver>Form</receiver>
    14. <slot>my_slot()</slot>
    15. </connection>
    16. </connections>
    17. <slots>
    18. <slot>my_slot()</slot>
    19. </slots>
    20. </ui>
    To copy to clipboard, switch view to plain text mode 

    Now suppose I do something like this:
    Qt Code:
    1. QUiLoader loader;
    2. QFile file("my_widget.ui");
    3. file.open(QFile::ReadOnly);=
    4. QWidget * my_widget = loader.load(&file, this);
    To copy to clipboard, switch view to plain text mode 

    What happens when I click the button? my_slot() isn't defined.

    I want to intercept calls to my_slot but I have no idea how. Something clever with QMetaObject maybe?

  2. #2
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: Dynamically Loading UI With Unspecified Slots

    Instead of unpacking the ui to a plain QWidget (my_widget in your case), unpack it to a subclass of QWidget that has a slot named my_slot.
    It's nice to be important but it's more important to be nice.

  3. #3
    Join Date
    Nov 2008
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dynamically Loading UI With Unspecified Slots

    The unpacking code doesn't have the subclass definition, that's the whole point, it's all going into scripting. The constraint is that the top-level class ONLY in the .ui file may be an arbitrary new class, everything which it includes must be available to the unpacker.

    Essentially, the .ui is guiding the arrangement of known classes and I need to hook into the new signals and slots.

Similar Threads

  1. how to dynamically add labels
    By mohanakrishnan in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2009, 11:19
  2. Dynamically Add QCheckBox
    By ToddAtWSU in forum Qt Programming
    Replies: 0
    Last Post: 22nd January 2007, 20:00
  3. Dynamically Loading a QMainWindow?
    By natron in forum Newbie
    Replies: 10
    Last Post: 21st July 2006, 02:15
  4. Dynamically resizing in QT 3
    By kroenecker in forum Qt Programming
    Replies: 3
    Last Post: 9th January 2006, 19:37

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.