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. QGridLayout with Two Widgets with equal width and height

QGridLayout with Two Widgets with equal width and height

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 4 Posters 1.3k 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 SGaist
    17 Jun 2022, 17:44

    Did you check which dimension you are returning ?

    S Offline
    S Offline
    sitesv
    wrote on 20 Jun 2022, 09:16 last edited by
    #5

    @SGaist said in QGridLayout with Two Widgets with equal width and height:

    Did you check which dimension you are returning ?

    Hi
    That exactly is a question. How to make heightForWidth execution each time as form resizing?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 20 Jun 2022, 20:14 last edited by
      #6

      This Stack Overflow thread covers the subject pretty extensively.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply 11 Jul 2022, 10:30
      0
      • S SGaist
        20 Jun 2022, 20:14

        This Stack Overflow thread covers the subject pretty extensively.

        S Offline
        S Offline
        sitesv
        wrote on 11 Jul 2022, 10:30 last edited by sitesv 7 Nov 2022, 14:06
        #7

        @SGaist said in QGridLayout with Two Widgets with equal width and height:

        This Stack Overflow thread covers the subject pretty extensively.

        Hi! It's not clear for me.

        Some Q's:

        1. Should widget expands with QSizePolicy::Expanding into all available space?
        2. If 'Y', that heightForWidth reimplementation is not enough for square creation and for avoid rectangle creation. Because it provide calculation for only one dimension. Layout could be resized by both dimensions.
        class mySquare : public QWidget
        {
        public:
            explicit mySquare(QWidget *parent = nullptr) : QWidget(parent){
                QSizePolicy p(sizePolicy());
                p.setVerticalPolicy(QSizePolicy::Expanding);
                p.setHorizontalPolicy(QSizePolicy::Expanding);
                p.setWidthForHeight(true);
                setSizePolicy(p);
                setStyleSheet("background-color: rgb(255, 0, 0);");
            }
            ~mySquare(){
                ;
            }
            int heightForWidth(int w) const{
                return w;
            }
            QSize sizeHint() const{
                return QSize(width(), heightForWidth(width()));
            }
        };
        
        class Form : public QWidget
        {
            Q_OBJECT
        
        public:
            explicit Form(QWidget *parent = nullptr);
            ~Form();
        
        private:
            Ui::Form *ui;
        };
        
        1 Reply Last reply
        0
        • S Offline
          S Offline
          sitesv
          wrote on 11 Jul 2022, 15:27 last edited by sitesv 7 Dec 2022, 10:17
          #8

          And:
          Alignment becomes incorrect (not centered, square is placed on the left) if make a reimplementation of qwidget resizeEvent like this:

          virtual void resizeEvent(QResizeEvent *event){
                  int h = event->size().height();
                  int w = event->size().width();
                  if(h < w) w = h;
                 resize(w,w);
          }
          

          But qwidget geometry has correct behaviour in this case.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sitesv
            wrote on 12 Jul 2022, 10:33 last edited by sitesv 7 Dec 2022, 10:38
            #9

            Very strange behaviour while expanding:
            111.PNG

            113.PNG

            Correct behaviour while shrinking:
            112.PNG

            class mySquare : public QWidget
            {
            public:
                explicit mySquare(QWidget *parent = nullptr) : QWidget(parent){
                    QSizePolicy p(sizePolicy());
                    p.setVerticalPolicy(QSizePolicy::Expanding);
                    p.setHorizontalPolicy(QSizePolicy::Preferred);
                    p.setHeightForWidth(true);
                    setSizePolicy(p);
                    setContentsMargins(0,0,0,0);
                    setStyleSheet("background-color: rgb(255, 0, 0);");
                    resize(1000, 1000);
                }
                ~mySquare(){
                    ;
                }
            
                int heightForWidth(int w) const override{
                    return w;
                }
                QSize sizeHint() const override{
                    return QSize(width(), heightForWidth(width()));
                }
            
            protected:
                virtual void resizeEvent(QResizeEvent *event) override{
                    int h = event->size().height();
                    int w = event->size().width();
                    if(h < w) w = h;
                    resize(w, w);
                }
            };
            
            ui->gridLayout->addWidget(new mySquare(this),0,0,Qt::AlignCenter);
            ui->gridLayout->addWidget(new mySquare(this),0,1,Qt::AlignCenter);
            
            1 Reply Last reply
            0
            • J Offline
              J Offline
              JoeCFD
              wrote on 12 Jul 2022, 15:15 last edited by
              #10

              It will be better to check this issue inside designer and you can copy the code generated from ui output.

              S 1 Reply Last reply 13 Jul 2022, 10:15
              0
              • J JoeCFD
                12 Jul 2022, 15:15

                It will be better to check this issue inside designer and you can copy the code generated from ui output.

                S Offline
                S Offline
                sitesv
                wrote on 13 Jul 2022, 10:15 last edited by
                #11

                @JoeCFD Not really understand purpose of your proposal

                J 1 Reply Last reply 13 Jul 2022, 19:13
                0
                • S sitesv
                  17 Jun 2022, 14:53

                  @SGaist said in QGridLayout with Two Widgets with equal width and height:

                  If you want square widgets, you can reimplement heightForWidth.

                  @SGaist
                  Hi.
                  I reimplemented widget class and heightForWIdth function. But widgets have fixed dimensions... How to make squares which dimension depends of size of tab widget?

                  class mySquare : public QWidget
                  {
                  public:
                      explicit mySquare(QWidget *parent = nullptr) : QWidget(parent){
                          QSizePolicy p(sizePolicy());
                          p.setWidthForHeight(true);
                          setSizePolicy(p);
                          setStyleSheet("background-color: rgb(255, 0, 0);");
                      }
                      ~mySquare(){
                          ;
                      }
                      int heightForWidth(int w) const{
                          return w;
                      }
                      QSize sizeHint() const{
                          return QSize(height(), heightForWidth(height())); // ? how make this correct?
                      }
                  };
                  

                  screenshot.PNG

                  O Offline
                  O Offline
                  Octavia
                  wrote on 13 Jul 2022, 18:29 last edited by
                  #12

                  @sitesv in gridlayout the width and height will be equal if you set the sizepolicy of the two widgets as QSizePolicy.Expanding.

                  S 1 Reply Last reply 15 Jul 2022, 13:16
                  0
                  • S sitesv
                    13 Jul 2022, 10:15

                    @JoeCFD Not really understand purpose of your proposal

                    J Offline
                    J Offline
                    JoeCFD
                    wrote on 13 Jul 2022, 19:13 last edited by JoeCFD
                    #13

                    @sitesv You are laying out widets in your classes, not from ui file generated from designer. I often does this on designer to see how widgets are laid out and resize. Then build it in a small case and copy the code in ui_something to my project code.

                    1 Reply Last reply
                    0
                    • O Octavia
                      13 Jul 2022, 18:29

                      @sitesv in gridlayout the width and height will be equal if you set the sizepolicy of the two widgets as QSizePolicy.Expanding.

                      S Offline
                      S Offline
                      sitesv
                      wrote on 15 Jul 2022, 13:16 last edited by
                      #14

                      @Octavia it is not a solution in this case

                      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