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 align widget in Center of the window if it has fixed size
Forum Updated to NodeBB v4.3 + New Features

how to align widget in Center of the window if it has fixed size

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 5.4k 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.
  • Thank YouT Offline
    Thank YouT Offline
    Thank You
    wrote on last edited by
    #1

    As said in question I have a widget named "center" which has some more widget. The maximum and minimum size of that widget is ''300 x 300.''
    So How can we align it in center of that window. I tried using margins but it is specific to only specific size. It has vertical layout. It doesn't work for window resizing. Are there any method to fix it.
    Yes there are many pages for positioning but I can't find the working solution.

    Thank You in Advance

    Let's make QT free or It will go forever

    TRUE AND FALSE <3

    Thank YouT J.HilkJ 2 Replies Last reply
    0
    • Thank YouT Thank You

      @jsulm No it is not working
      Can we use margins in QGridLayout. I have used margin :10 in bottom. But after removing it also It doesn't work. How can I fix it

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

      @Thank-You Do you actually have any other widgets at left/right or /top/bottom side of that central widget? If not you can simply use horizontal and vertical spacers.

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

      Thank YouT 1 Reply Last reply
      3
      • Thank YouT Thank You

        As said in question I have a widget named "center" which has some more widget. The maximum and minimum size of that widget is ''300 x 300.''
        So How can we align it in center of that window. I tried using margins but it is specific to only specific size. It has vertical layout. It doesn't work for window resizing. Are there any method to fix it.
        Yes there are many pages for positioning but I can't find the working solution.

        Thank You in Advance

        Thank YouT Offline
        Thank YouT Offline
        Thank You
        wrote on last edited by
        #2

        I guess You guys can solve this
        @SGaist @jsulm @dheerendra @JonB @mrjj

        Let's make QT free or It will go forever

        TRUE AND FALSE <3

        jsulmJ 1 Reply Last reply
        0
        • Thank YouT Thank You

          I guess You guys can solve this
          @SGaist @jsulm @dheerendra @JonB @mrjj

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

          @Thank-You What about using QGridLayout?

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

          Thank YouT 1 Reply Last reply
          1
          • jsulmJ jsulm

            @Thank-You What about using QGridLayout?

            Thank YouT Offline
            Thank YouT Offline
            Thank You
            wrote on last edited by
            #4

            @jsulm No it is not working
            Can we use margins in QGridLayout. I have used margin :10 in bottom. But after removing it also It doesn't work. How can I fix it

            Let's make QT free or It will go forever

            TRUE AND FALSE <3

            jsulmJ 1 Reply Last reply
            0
            • Thank YouT Thank You

              @jsulm No it is not working
              Can we use margins in QGridLayout. I have used margin :10 in bottom. But after removing it also It doesn't work. How can I fix it

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

              @Thank-You Do you actually have any other widgets at left/right or /top/bottom side of that central widget? If not you can simply use horizontal and vertical spacers.

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

              Thank YouT 1 Reply Last reply
              3
              • Thank YouT Thank You

                As said in question I have a widget named "center" which has some more widget. The maximum and minimum size of that widget is ''300 x 300.''
                So How can we align it in center of that window. I tried using margins but it is specific to only specific size. It has vertical layout. It doesn't work for window resizing. Are there any method to fix it.
                Yes there are many pages for positioning but I can't find the working solution.

                Thank You in Advance

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #6

                @Thank-You

                You have 2 options:

                The better one, use QGridLayout
                the "bad" one override the resize event

                #include <QWidget>
                #include <QResizeEvent>
                
                class hackWidget : public QWidget
                {
                    Q_OBJECT
                    QWidget *center2;
                public:
                    explicit hackWidget(QWidget *parent = nullptr)
                        : QWidget(parent), center2(new QWidget(this))
                    {
                        center2->setFixedSize(300,300);
                        center2->setStyleSheet("background:red;");
                    }
                
                protected:
                    void resizeEvent(QResizeEvent *event) override
                    {
                        const int w = event->size().width();
                        const int h = event->size().height();
                
                        const int x = ( w - center2->width()) / 2;
                        const int y = (h - center2->height()) / 2;
                        center2->move(x,y);
                    }
                };
                
                #include <QGridLayout>
                
                int main (int argc, char *argv[])
                {
                    QApplication app(argc, argv);
                
                
                    QWidget mainWidget1;
                    mainWidget1.resize(500,500);
                    mainWidget1.show();
                
                    auto gLay {new QGridLayout(&mainWidget1)};
                    gLay->addItem(new QSpacerItem(0,0, QSizePolicy::Expanding, QSizePolicy::Expanding),0,0);
                    gLay->addItem(new QSpacerItem(0,0, QSizePolicy::Expanding, QSizePolicy::Expanding),2,2);
                    QWidget center1;
                    center1.setFixedSize(300,300);
                    center1.setStyleSheet("background: green;");
                    gLay->addWidget(&center1,1,1);
                
                    ////
                
                    hackWidget mainWidget2;
                    mainWidget2.resize(500,500);
                    mainWidget2.show();
                
                    return  app.exec();
                }
                
                #include "main.moc"
                

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                1
                • jsulmJ jsulm

                  @Thank-You Do you actually have any other widgets at left/right or /top/bottom side of that central widget? If not you can simply use horizontal and vertical spacers.

                  Thank YouT Offline
                  Thank YouT Offline
                  Thank You
                  wrote on last edited by
                  #7

                  @jsulm The widget "center" has vertical layout. Should I use spacers in all side

                  Let's make QT free or It will go forever

                  TRUE AND FALSE <3

                  jsulmJ 1 Reply Last reply
                  0
                  • Thank YouT Thank You

                    @jsulm The widget "center" has vertical layout. Should I use spacers in all side

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

                    @Thank-You I'm not talking about layouts inside the center widget. You want to center the center widget itself, or do I misunderstand your use case?

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

                    Thank YouT 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @Thank-You I'm not talking about layouts inside the center widget. You want to center the center widget itself, or do I misunderstand your use case?

                      Thank YouT Offline
                      Thank YouT Offline
                      Thank You
                      wrote on last edited by
                      #9

                      @jsulm Oh I was saying about the "center" widget. Now I got your point and I used spacers and it worked
                      Thank you

                      Let's make QT free or It will go forever

                      TRUE AND FALSE <3

                      1 Reply Last reply
                      1

                      • Login

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