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.
  • SGaistS SGaist

    Did you check which dimension you are returning ?

    sitesvS Offline
    sitesvS Offline
    sitesv
    wrote on 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
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 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

      sitesvS 1 Reply Last reply
      0
      • SGaistS SGaist

        This Stack Overflow thread covers the subject pretty extensively.

        sitesvS Offline
        sitesvS Offline
        sitesv
        wrote on last edited by sitesv
        #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
        • sitesvS Offline
          sitesvS Offline
          sitesv
          wrote on last edited by sitesv
          #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
          • sitesvS Offline
            sitesvS Offline
            sitesv
            wrote on last edited by sitesv
            #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
            • JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by
              #10

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

              sitesvS 1 Reply Last reply
              0
              • JoeCFDJ JoeCFD

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

                sitesvS Offline
                sitesvS Offline
                sitesv
                wrote on last edited by
                #11

                @JoeCFD Not really understand purpose of your proposal

                JoeCFDJ 1 Reply Last reply
                0
                • sitesvS sitesv

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

                  sitesvS 1 Reply Last reply
                  0
                  • sitesvS sitesv

                    @JoeCFD Not really understand purpose of your proposal

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on 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

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

                      sitesvS Offline
                      sitesvS Offline
                      sitesv
                      wrote on 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