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. How to get object pointer from a layout
Forum Updated to NodeBB v4.3 + New Features

How to get object pointer from a layout

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 1.6k 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.
  • ivanicyI Offline
    ivanicyI Offline
    ivanicy
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by dheerendra
      #2

      How is this UI displayed ? Instead of this you can use findchild API of qobject

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        How many of these widget are you adding ?

        As @dheerendra suggested, QObject::findChild is likely going to be your friend.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        ivanicyI 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          How many of these widget are you adding ?

          As @dheerendra suggested, QObject::findChild is likely going to be your friend.

          ivanicyI Offline
          ivanicyI Offline
          ivanicy
          wrote on last edited by
          #4

          @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

          1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by
            #5

            show us where are you creating the ui object and how r you calling setupUI method.

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            ivanicyI 1 Reply Last reply
            0
            • dheerendraD dheerendra

              show us where are you creating the ui object and how r you calling setupUI method.

              ivanicyI Offline
              ivanicyI Offline
              ivanicy
              wrote on last edited by
              #6

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

              jsulmJ 1 Reply Last reply
              0
              • ivanicyI ivanicy

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

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

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

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                2
                • dheerendraD Offline
                  dheerendraD Offline
                  dheerendra
                  Qt Champions 2022
                  wrote on last edited by
                  #8

                  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.

                  Dheerendra
                  @Community Service
                  Certified Qt Specialist
                  http://www.pthinks.com

                  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