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.
  • T Offline
    T Offline
    Thank You
    wrote on 16 Nov 2020, 04:35 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

    T J 2 Replies Last reply 16 Nov 2020, 04:45
    0
    • T Thank You
      16 Nov 2020, 07:04

      @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

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 16 Nov 2020, 07:08 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

      T 1 Reply Last reply 16 Nov 2020, 07:36
      3
      • T Thank You
        16 Nov 2020, 04:35

        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

        T Offline
        T Offline
        Thank You
        wrote on 16 Nov 2020, 04:45 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

        J 1 Reply Last reply 16 Nov 2020, 06:20
        0
        • T Thank You
          16 Nov 2020, 04:45

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

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 16 Nov 2020, 06:20 last edited by
          #3

          @Thank-You What about using QGridLayout?

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

          T 1 Reply Last reply 16 Nov 2020, 07:04
          1
          • J jsulm
            16 Nov 2020, 06:20

            @Thank-You What about using QGridLayout?

            T Offline
            T Offline
            Thank You
            wrote on 16 Nov 2020, 07:04 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

            J 1 Reply Last reply 16 Nov 2020, 07:08
            0
            • T Thank You
              16 Nov 2020, 07:04

              @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

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 16 Nov 2020, 07:08 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

              T 1 Reply Last reply 16 Nov 2020, 07:36
              3
              • T Thank You
                16 Nov 2020, 04:35

                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 Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 16 Nov 2020, 07:20 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
                • J jsulm
                  16 Nov 2020, 07:08

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

                  T Offline
                  T Offline
                  Thank You
                  wrote on 16 Nov 2020, 07:36 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

                  J 1 Reply Last reply 16 Nov 2020, 07:38
                  0
                  • T Thank You
                    16 Nov 2020, 07:36

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

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 16 Nov 2020, 07:38 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

                    T 1 Reply Last reply 16 Nov 2020, 07:47
                    0
                    • J jsulm
                      16 Nov 2020, 07:38

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

                      T Offline
                      T Offline
                      Thank You
                      wrote on 16 Nov 2020, 07:47 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

                      1/9

                      16 Nov 2020, 04:35

                      • Login

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