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. Adding a custom widget into an existing layout.
Forum Update on Monday, May 27th 2025

Adding a custom widget into an existing layout.

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 2.1k 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.
  • M Offline
    M Offline
    m1212e
    wrote on 1 Feb 2020, 15:18 last edited by m1212e 2 Jan 2020, 15:18
    #2

    Sooo, I got it. I think this is actually the proper way to do it, right? At least it's staying centered now. But the other qestions remain? Is this the way to do it?

    static_cast<QGridLayout *>(ui->gridLayout)->addWidget(b, 0, 1, Qt::AlignCenter);
    

    Thanks anyway for reading?

    1 Reply Last reply
    1
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 1 Feb 2020, 15:24 last edited by
      #3

      @m1212e said in Adding a custom widget into an existing layout.:

      static_cast<QGridLayout *>(ui->gridLayout)

      Why do you need a cast here?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      M 1 Reply Last reply 2 Feb 2020, 12:22
      1
      • C Christian Ehrlicher
        1 Feb 2020, 15:24

        @m1212e said in Adding a custom widget into an existing layout.:

        static_cast<QGridLayout *>(ui->gridLayout)

        Why do you need a cast here?

        M Offline
        M Offline
        m1212e
        wrote on 2 Feb 2020, 12:22 last edited by
        #4

        @Christian-Ehrlicher
        You're abolutely right :D
        It's just an artifact from previous attempts.
        Thanks!

        1 Reply Last reply
        0
        • C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 2 Feb 2020, 12:37 last edited by
          #5

          When you won't add your widget by code but directly in the designer you can take a look at promoting widgets.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          M 1 Reply Last reply 2 Feb 2020, 14:21
          3
          • C Christian Ehrlicher
            2 Feb 2020, 12:37

            When you won't add your widget by code but directly in the designer you can take a look at promoting widgets.

            M Offline
            M Offline
            m1212e
            wrote on 2 Feb 2020, 14:21 last edited by
            #6

            @Christian-Ehrlicher

            Already did that, but thanks. Unfortunatly I want to add them in my code.

            M 1 Reply Last reply 2 Feb 2020, 23:05
            0
            • M m1212e
              2 Feb 2020, 14:21

              @Christian-Ehrlicher

              Already did that, but thanks. Unfortunatly I want to add them in my code.

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 2 Feb 2020, 23:05 last edited by
              #7

              @m1212e
              Hi
              Just as a note. when i want to insert dynamically into a Designer created
              ui, i often just add some test widgets and then go to the generated
              code (in the setupUI function) and see how its inserted. This is especially useful for complex layouts.

              M 1 Reply Last reply 3 Feb 2020, 12:22
              3
              • M mrjj
                2 Feb 2020, 23:05

                @m1212e
                Hi
                Just as a note. when i want to insert dynamically into a Designer created
                ui, i often just add some test widgets and then go to the generated
                code (in the setupUI function) and see how its inserted. This is especially useful for complex layouts.

                M Offline
                M Offline
                m1212e
                wrote on 3 Feb 2020, 12:22 last edited by
                #8

                @mrjj
                That's a good idea, thanks. Is there any way to edit the auto generated code?

                J M 2 Replies Last reply 3 Feb 2020, 12:25
                0
                • M m1212e
                  3 Feb 2020, 12:22

                  @mrjj
                  That's a good idea, thanks. Is there any way to edit the auto generated code?

                  J Online
                  J Online
                  jsulm
                  Lifetime Qt Champion
                  wrote on 3 Feb 2020, 12:25 last edited by
                  #9

                  @m1212e said in Adding a custom widget into an existing layout.:

                  Is there any way to edit the auto generated code?

                  You should not as next time you build your project auto generated code will be overwritten :-)

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

                  M 1 Reply Last reply 3 Feb 2020, 12:26
                  3
                  • J jsulm
                    3 Feb 2020, 12:25

                    @m1212e said in Adding a custom widget into an existing layout.:

                    Is there any way to edit the auto generated code?

                    You should not as next time you build your project auto generated code will be overwritten :-)

                    M Offline
                    M Offline
                    m1212e
                    wrote on 3 Feb 2020, 12:26 last edited by
                    #10

                    @jsulm
                    Well, I expected that, probably I'm better off just outsourcing that into my own files.
                    Thanks for the help guys!

                    1 Reply Last reply
                    0
                    • M m1212e
                      3 Feb 2020, 12:22

                      @mrjj
                      That's a good idea, thanks. Is there any way to edit the auto generated code?

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 3 Feb 2020, 12:32 last edited by
                      #11

                      @m1212e said in Adding a custom widget into an existing layout.:

                      That's a good idea, thanks. Is there any way to edit the auto generated code?

                      Hi
                      You must copy the code and make it your own.
                      But its very handy to visually build something and then get the code to create it dynamically.

                      1 Reply Last reply
                      0

                      11/11

                      3 Feb 2020, 12:32

                      • Login

                      • Login or register to search.
                      11 out of 11
                      • First post
                        11/11
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved