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. Passing Widget to annother Widget

Passing Widget to annother Widget

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 540 Views
  • 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.
  • S Offline
    S Offline
    sandro4912
    wrote on last edited by
    #1

    I want to create a QVector of different Widgets and give them to annother Widget class to manage them.

    Is it possible?

    Passing by const reference does not seem to work because copy is disabled.

    Just to give an idea:

    class Bar : public QWidget
    {
        //....
    };
    
    class Foo : public QWidget
    {
        Foo(const QVector<Bar *> &bars, QWidget *parent = nullptr)
            :QWidget{ parent },
            mBars{bars}
        {       
        }
        
    private:
        QVector<Bar *> mBars;
        
    };
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      QVector<Bar *> mBars;
      should work. It must be a pointer.

      Note to show them, you will insert them into other widgets ?

      Watch out for ownership issues as if you let any one but Foo class handle them/use them.

      1 Reply Last reply
      2
      • S Offline
        S Offline
        sandro4912
        wrote on last edited by
        #3

        i found the issue in my real code i wrote std::vector instead of QVector in the constructor. That's why i wondered, that it was not working.

        mrjjM 1 Reply Last reply
        0
        • S sandro4912

          i found the issue in my real code i wrote std::vector instead of QVector in the constructor. That's why i wondered, that it was not working.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @sandro4912
          std::vector<Bar *> should also work but
          & (refs ) will not.
          Maybe that was the actual difference ?

          1 Reply Last reply
          2
          • S Offline
            S Offline
            sandro4912
            wrote on last edited by sandro4912
            #5

            Yes i guess.

            Isn't it that i have two options?

            Foo(const QVector<Bar *> &bars, QWidget *parent = nullptr)
                   :QWidget{ parent },
                   mBars{bars}
               {       
               }
            
            Foo(QVector<Bar *> bars, QWidget *parent = nullptr)
                   :QWidget{ parent },
                   mBars{std::move(bars)}
               {       
               }
            

            And for the Understanding. I allocate the Bar Widgets on the heap with new. They have no parent then.

            When i pass them to Foo they get owned by Foo when i add them to a layout right?

            mrjjM 1 Reply Last reply
            0
            • S sandro4912

              Yes i guess.

              Isn't it that i have two options?

              Foo(const QVector<Bar *> &bars, QWidget *parent = nullptr)
                     :QWidget{ parent },
                     mBars{bars}
                 {       
                 }
              
              Foo(QVector<Bar *> bars, QWidget *parent = nullptr)
                     :QWidget{ parent },
                     mBars{std::move(bars)}
                 {       
                 }
              

              And for the Understanding. I allocate the Bar Widgets on the heap with new. They have no parent then.

              When i pass them to Foo they get owned by Foo when i add them to a layout right?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @sandro4912 said in Passing Widget to annother Widget:

              they get owned by Foo when i add them to a layout right?

              Yes. When used with layouts, the layout/layouts parent will own the widget
              so unless you use TakeAt on the layout to get it back, it will be deleted with parent and you have a dangling pointer in list.

              However, if you use them all in same place, it should be easy to handle.

              S 1 Reply Last reply
              0
              • mrjjM mrjj

                @sandro4912 said in Passing Widget to annother Widget:

                they get owned by Foo when i add them to a layout right?

                Yes. When used with layouts, the layout/layouts parent will own the widget
                so unless you use TakeAt on the layout to get it back, it will be deleted with parent and you have a dangling pointer in list.

                However, if you use them all in same place, it should be easy to handle.

                S Offline
                S Offline
                sandro4912
                wrote on last edited by
                #7

                @mrjj

                I use the approach like this:

                    auto cells = createCells(width, height, countOfMines);
                    randomizeCells(cells);
                    auto newMinefield = new Minefield{ cells, width, height };
                

                After this is run the local Cells is destroyed,

                I want to create the Cells independet from the Minefield so i can run easier unit tests with handmade Cells (not randomized)

                jsulmJ 1 Reply Last reply
                0
                • S sandro4912

                  @mrjj

                  I use the approach like this:

                      auto cells = createCells(width, height, countOfMines);
                      randomizeCells(cells);
                      auto newMinefield = new Minefield{ cells, width, height };
                  

                  After this is run the local Cells is destroyed,

                  I want to create the Cells independet from the Minefield so i can run easier unit tests with handmade Cells (not randomized)

                  jsulmJ Online
                  jsulmJ Online
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @sandro4912 said in Passing Widget to annother Widget:

                  I want to create the Cells independet from the Minefield

                  Maybe I don't see something, but what exactly is the problem?
                  What are these cells?

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

                  1 Reply Last reply
                  1
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi
                    auto newMinefield = new Minefield{ cells, width, height };

                    Seems fine. Will MineField then delete the cells list or will
                    it leave it alone and somebody else will delete the cells?

                    In any case, this should not be an issue if handled correctly.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      sandro4912
                      wrote on last edited by
                      #10

                      Minefield puts the Cells into its layout.

                      Minefield later gets explicit delete with delete Minefield (and then gets replaced with a new Minefield at runtime).

                      The Cells are one field of the Minefield which is in the normal game each time generated with random content.

                      By having the Cells created outside I can create not random Cells for testing.

                      mrjjM 1 Reply Last reply
                      0
                      • S sandro4912

                        Minefield puts the Cells into its layout.

                        Minefield later gets explicit delete with delete Minefield (and then gets replaced with a new Minefield at runtime).

                        The Cells are one field of the Minefield which is in the normal game each time generated with random content.

                        By having the Cells created outside I can create not random Cells for testing.

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @sandro4912

                        Hi
                        Well if the rule is that a cell list will be owned by the minefield
                        once given to it and will die with it i think it will work fine :)

                        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