Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Access to custom widget's object after dynamic Ui loading
Qt 6.11 is out! See what's new in the release blog

[SOLVED] Access to custom widget's object after dynamic Ui loading

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I've created a custom widget plugin for Qt Designer. Then i put this widget on a form and saved as a "widget.ui" file.
    In the main application:
    @
    QUiLoader loader;
    QFile file("widget.ui");
    file.open(QFile::ReadOnly);
    QWidget *myWidget = loader.load(&file, 0);
    file.close();
    @

    This custom widget can be shown after that . But i need to access some his methods. Is there a way to do that?
    I'm trying to get him by:
    @myWidget->findChild<CustomWidgetClass*>();@

    It has no effect. Null pointer was returned.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      Have you tried:
      @
      CustomWidgetClasss myWidget = qobject_cast<CustomWidgetClass>(loader.load(&file, 0));
      @

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Thank you for your answer! I think i found a solution.
        Back to reality:

        1. I've created a class PushButton - successor of QPushButton. Implementation of this widget was made in plugin for Qt Designer.
        2. In Qt Designer i've selected QDockWidget like a container-form.
        3. PushButton was placed on the form.
        4. Form was saved in "widget.ui" file.
        5. In main application dock widget is loading by
          @QWidget *myWidget = loader.load(&file, 0);@

        with a qobject_cast 'ing.

        1. Then i've tried to find my custom widget like a child of QDockWidget:
          @ myWidget->findChild<PushButton*>();@

        but this method returned a NULL-pointer. Nevertheless, i can get target pointer by:

        @QPushButton ancestor = myWidget->findChild<QPushButton>();@

        Object's casting from QPushButton to PushButton returned NULL also.

        My solution is:
        @if(ancestor->inherits("PushButton"))
        PushButton btn = reinterpret_cast<PushButton>(ancestor);
        @

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved