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.
QtWS25 Last Chance

Adding a custom widget into an existing layout.

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 2.0k 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 last edited by
    #1

    Hi,

    I'm new to Qt and especially to it's layout and widget structure. I want to make a small game which requires clickable tiles. I use QPushButtons with stylesheets and I arranged them into a QGridLayout.

    Board::Board(int width, int height, QWidget *parent) : QWidget(parent), width(width), height(height)
    {
        setParent(parent);
    
        QGridLayout *mainLayout = new QGridLayout(this);
        mainLayout->setSizeConstraint(QLayout::SetFixedSize);
    
    
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                BoardUnit *bu = new BoardUnit(x, y, this);
                mainLayout->addWidget(bu, y, x);
                bu->fadeIn();
            }
        }
    
        setLayout(mainLayout);
    }
    

    This is basically how I initialize my board. It's based on this tutorial. This is leading to a pretty good result.
    qt1.PNG

    I'm adding the custom Board widget to my window like this:

    Board *b = new Board(8, 5, this);
        layout()->addWidget(b);
    

    The result is what you see in the screenshot. Now my problem is, that I can't get the custom Board (QWidget) to center properly. Since I got an already existing layout with 2 spacers I've tried to simply add it between those two spacers, without any success. This is what the previous screen looks like in the designer:
    qt2.PNG

    So my questions are:
    How do I add the Board widget between those already existing spacers?
    And is this even the best way to solve my problem?
    Is there a possibility to see whats happening in the running window? So like the inspector in Firefox for html/css which shows what objects are where, even if they're invisble to the user? (To get an idea where the spacers are and what they're doing)
    I find the whole layout thing a bit confusing, mostly because I switch between the editor which does it's magic behind the scenes and my dynamic code which has to interact with that auto generated stuff.

    Thanks in advance!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      m1212e
      wrote on last edited by m1212e
      #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
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 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
        1
        • Christian EhrlicherC Christian Ehrlicher

          @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 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
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 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
            3
            • Christian EhrlicherC Christian Ehrlicher

              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 last edited by
              #6

              @Christian-Ehrlicher

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

              mrjjM 1 Reply Last reply
              0
              • M m1212e

                @Christian-Ehrlicher

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

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on 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
                • mrjjM mrjj

                  @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 last edited by
                  #8

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

                  jsulmJ mrjjM 2 Replies Last reply
                  0
                  • M m1212e

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

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 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
                    • jsulmJ jsulm

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

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

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 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

                        • Login

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