Unsolved How to get object pointer from a layout
-
Hello!!! I am trying to get the pointer of an object from a layout in which I put it:
void function() { MyClass *myClass = new MyClass(); ui->myLayout->addWidget(myClass); } // In other part of the code... MyClass *newPointerClass = ui->myLayout->getPointer();
How can I do this "getPointer()" ??
I hope I have explained clearly
Thank you very much!
-
How is this UI displayed ? Instead of this you can use findchild API of qobject
-
Hi,
How many of these widget are you adding ?
As @dheerendra suggested, QObject::findChild is likely going to be your friend.
-
@SGaist I have only one widget per layout.
Would I have to use a reinterpret_cast or how would that line be?Thanks both of you @dheerendra @SGaist
-
show us where are you creating the ui object and how r you calling setupUI method.
-
@dheerendra
My code is very similar to the one exposed in my first post.A local object created inside a function and this object added to the layout with layout->addWidget method.
In other part of the code, I want to recover this object in another pointer. The layout is created with Qt designer.
-
@ivanicy Please take a look at documentation: http://doc.qt.io/qt-5/qobject.html#findChild
You can do it like this:MyWidget *childWidget = parentWidget->findChild<MyWidget*>("widget_name");
-
ui->myLayout->addWidget(myClass);
Where & how ui object is created ? @jsulm has already given you how to find the child with name of the object.