[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.
-
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:- I've created a class PushButton - successor of QPushButton. Implementation of this widget was made in plugin for Qt Designer.
- In Qt Designer i've selected QDockWidget like a container-form.
- PushButton was placed on the form.
- Form was saved in "widget.ui" file.
- In main application dock widget is loading by
@QWidget *myWidget = loader.load(&file, 0);@
with a qobject_cast 'ing.
- 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);
@