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



  • 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.


  • Moderators

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



  • 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);
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.