QBoxLayout misplacing
-
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:
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)
-
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:
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)
@GrassHopper90
Don't you get to see everything if you remove them_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? -
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:
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)
@GrassHopper90 said in QBoxLayout misplacing:
m_layout.setSpacing(0);
m_layout.setContentsMargins(0, 0, 0, 0);What if you start sensibly increasing values?
-
@GrassHopper90
Don't you get to see everything if you remove them_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?@JonB This. You fixed it. But why? Spacing is the space between elements, correct? Why if i set it to zero the elements overlap?
-
@JonB This. You fixed it. But why? Spacing is the space between elements, correct? Why if i set it to zero the elements overlap?
@GrassHopper90 I'd wager that sizeHint() for the buttons doesn't return proper value.
-
@GrassHopper90
Don't you get to see everything if you remove them_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?@JonB Now i have the other test array (horizontal) like this:
On the left there is the horizontal array and on the righ you can see the vertical one that now is correctly placed -
@GrassHopper90 I'd wager that sizeHint() for the buttons doesn't return proper value.
@artwaw I've reimplemented that method to return a small size, and set the layout to ignore the hint.
-
up .