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. QBoxLayout misplacing
Forum Updated to NodeBB v4.3 + New Features

QBoxLayout misplacing

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 472 Views 2 Watching
  • 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.
  • G Offline
    G Offline
    GrassHopper90
    wrote on last edited by GrassHopper90
    #1

    Hello everyone

    I'm writing a class (ButtonArray) that inherits QWidget and uses a QBoxLayout to display a series of buttons.

    what i've wrote:

    //=========================================================
    void UGButtonArray::paintEvent(QPaintEvent* event)//just for debug
    {
        QPainter painter(this);
        painter.fillRect(rect(), QColor(255, 0, 0, 20));
        QWidget::paintEvent(event);
    }
    //=========================================================
    UGButtonArray::UGButtonArray(QWidget* parent)
        : QWidget{parent},
          m_layout(QBoxLayout::Direction::LeftToRight, this)
    {
        setLayout(&m_layout);
        m_layout.setSpacing(0);
        m_layout.setContentsMargins(0, 0, 0, 0);
    }
    //=========================================================
    void UGButtonArray::setArrayDir(ArrayDirection dir)
    {
        if (dir == AD_Horizontal)
            m_layout.setDirection(QBoxLayout::Direction::LeftToRight);
        else
            m_layout.setDirection(QBoxLayout::Direction::TopToBottom);
    }
    //=========================================================
    void UGButtonArray::addButton(const UltraEntry& entry)
    {
        auto b = new UGButton(this);
        b->setVisible(!entry.hidden);
        b->setSizePolicy(QSizePolicy(QSizePolicy::Policy::Ignored, QSizePolicy::Policy::Ignored));
        b->setText(entry.entryText);
        b->drawBorder();
        b->setBorderRadius(5);
    
        m_layout.addWidget(b);
        m_buttons.push_back({b, entry});
    }
    //=========================================================
    

    The array of button is a QVector of button, that's another class i wrote that inherits QPushButton and simply display a button with some other features. I don't paste it for now because it's a long piece of code.

    What i see:
    Screenshot 2024-07-19 at 19.26.06.png

    As you can see in the picture, the three buttons don't have the same height. I mean, they HAVE the same height but it's like they have been placed from the first (Button 1) to the last (Button 3) overlapping (the 2 goes partially on top of 1 and the same with 3 and 2). I am sure of this because i rounded the corners and i cannot see the rounded lower corners of button 1 and 2.

    Is there something i missed? (ofc there is :D)

    JonBJ artwawA 2 Replies Last reply
    0
    • G GrassHopper90

      Hello everyone

      I'm writing a class (ButtonArray) that inherits QWidget and uses a QBoxLayout to display a series of buttons.

      what i've wrote:

      //=========================================================
      void UGButtonArray::paintEvent(QPaintEvent* event)//just for debug
      {
          QPainter painter(this);
          painter.fillRect(rect(), QColor(255, 0, 0, 20));
          QWidget::paintEvent(event);
      }
      //=========================================================
      UGButtonArray::UGButtonArray(QWidget* parent)
          : QWidget{parent},
            m_layout(QBoxLayout::Direction::LeftToRight, this)
      {
          setLayout(&m_layout);
          m_layout.setSpacing(0);
          m_layout.setContentsMargins(0, 0, 0, 0);
      }
      //=========================================================
      void UGButtonArray::setArrayDir(ArrayDirection dir)
      {
          if (dir == AD_Horizontal)
              m_layout.setDirection(QBoxLayout::Direction::LeftToRight);
          else
              m_layout.setDirection(QBoxLayout::Direction::TopToBottom);
      }
      //=========================================================
      void UGButtonArray::addButton(const UltraEntry& entry)
      {
          auto b = new UGButton(this);
          b->setVisible(!entry.hidden);
          b->setSizePolicy(QSizePolicy(QSizePolicy::Policy::Ignored, QSizePolicy::Policy::Ignored));
          b->setText(entry.entryText);
          b->drawBorder();
          b->setBorderRadius(5);
      
          m_layout.addWidget(b);
          m_buttons.push_back({b, entry});
      }
      //=========================================================
      

      The array of button is a QVector of button, that's another class i wrote that inherits QPushButton and simply display a button with some other features. I don't paste it for now because it's a long piece of code.

      What i see:
      Screenshot 2024-07-19 at 19.26.06.png

      As you can see in the picture, the three buttons don't have the same height. I mean, they HAVE the same height but it's like they have been placed from the first (Button 1) to the last (Button 3) overlapping (the 2 goes partially on top of 1 and the same with 3 and 2). I am sure of this because i rounded the corners and i cannot see the rounded lower corners of button 1 and 2.

      Is there something i missed? (ofc there is :D)

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @GrassHopper90
      Don't you get to see everything if you remove the m_layout.setSpacing(0);? Are you saying you don't expect any overlap with 0 spacing? I assume this does not only happen when go for rounded corners?

      G 2 Replies Last reply
      2
      • G GrassHopper90

        Hello everyone

        I'm writing a class (ButtonArray) that inherits QWidget and uses a QBoxLayout to display a series of buttons.

        what i've wrote:

        //=========================================================
        void UGButtonArray::paintEvent(QPaintEvent* event)//just for debug
        {
            QPainter painter(this);
            painter.fillRect(rect(), QColor(255, 0, 0, 20));
            QWidget::paintEvent(event);
        }
        //=========================================================
        UGButtonArray::UGButtonArray(QWidget* parent)
            : QWidget{parent},
              m_layout(QBoxLayout::Direction::LeftToRight, this)
        {
            setLayout(&m_layout);
            m_layout.setSpacing(0);
            m_layout.setContentsMargins(0, 0, 0, 0);
        }
        //=========================================================
        void UGButtonArray::setArrayDir(ArrayDirection dir)
        {
            if (dir == AD_Horizontal)
                m_layout.setDirection(QBoxLayout::Direction::LeftToRight);
            else
                m_layout.setDirection(QBoxLayout::Direction::TopToBottom);
        }
        //=========================================================
        void UGButtonArray::addButton(const UltraEntry& entry)
        {
            auto b = new UGButton(this);
            b->setVisible(!entry.hidden);
            b->setSizePolicy(QSizePolicy(QSizePolicy::Policy::Ignored, QSizePolicy::Policy::Ignored));
            b->setText(entry.entryText);
            b->drawBorder();
            b->setBorderRadius(5);
        
            m_layout.addWidget(b);
            m_buttons.push_back({b, entry});
        }
        //=========================================================
        

        The array of button is a QVector of button, that's another class i wrote that inherits QPushButton and simply display a button with some other features. I don't paste it for now because it's a long piece of code.

        What i see:
        Screenshot 2024-07-19 at 19.26.06.png

        As you can see in the picture, the three buttons don't have the same height. I mean, they HAVE the same height but it's like they have been placed from the first (Button 1) to the last (Button 3) overlapping (the 2 goes partially on top of 1 and the same with 3 and 2). I am sure of this because i rounded the corners and i cannot see the rounded lower corners of button 1 and 2.

        Is there something i missed? (ofc there is :D)

        artwawA Offline
        artwawA Offline
        artwaw
        wrote on last edited by
        #3

        @GrassHopper90 said in QBoxLayout misplacing:

        m_layout.setSpacing(0);
        m_layout.setContentsMargins(0, 0, 0, 0);

        What if you start sensibly increasing values?

        For more information please re-read.

        Kind Regards,
        Artur

        1 Reply Last reply
        0
        • JonBJ JonB

          @GrassHopper90
          Don't you get to see everything if you remove the m_layout.setSpacing(0);? Are you saying you don't expect any overlap with 0 spacing? I assume this does not only happen when go for rounded corners?

          G Offline
          G Offline
          GrassHopper90
          wrote on last edited by GrassHopper90
          #4

          @JonB This. You fixed it. But why? Spacing is the space between elements, correct? Why if i set it to zero the elements overlap?

          artwawA 1 Reply Last reply
          0
          • G GrassHopper90

            @JonB This. You fixed it. But why? Spacing is the space between elements, correct? Why if i set it to zero the elements overlap?

            artwawA Offline
            artwawA Offline
            artwaw
            wrote on last edited by
            #5

            @GrassHopper90 I'd wager that sizeHint() for the buttons doesn't return proper value.

            For more information please re-read.

            Kind Regards,
            Artur

            G 1 Reply Last reply
            0
            • JonBJ JonB

              @GrassHopper90
              Don't you get to see everything if you remove the m_layout.setSpacing(0);? Are you saying you don't expect any overlap with 0 spacing? I assume this does not only happen when go for rounded corners?

              G Offline
              G Offline
              GrassHopper90
              wrote on last edited by
              #6

              @JonB Now i have the other test array (horizontal) like this:
              Screenshot 2024-07-19 at 20.06.37.png
              On the left there is the horizontal array and on the righ you can see the vertical one that now is correctly placed

              1 Reply Last reply
              0
              • artwawA artwaw

                @GrassHopper90 I'd wager that sizeHint() for the buttons doesn't return proper value.

                G Offline
                G Offline
                GrassHopper90
                wrote on last edited by
                #7

                @artwaw I've reimplemented that method to return a small size, and set the layout to ignore the hint.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  GrassHopper90
                  wrote on last edited by
                  #8

                  up .

                  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