QVBoxLayout how to add spacer row - c++

Is it possible to add a spacing row to a QVBoxLayout? I tried using a QPushButton and then hiding, but this doesn't work.
I want the layout to contain 5 buttons with a spacing row between buttons 4 and 5.

You don't need to handle the QSpacerItem yourself (the documentation lists the methods you should use instrad of creating the QSpacerItem).
If you want to add a space with a specific size, you can use QVBoxLayout::addSpacing():
QWidget* w = new QWidget();
QVBoxLayout* layout = new QVBoxLayout(w);
layout->addWidget(new QPushButton("first"));
layout->addWidget(new QPushButton("second"));
layout->addWidget(new QPushButton("third"));
layout->addWidget(new QPushButton("fourth"));
layout->addSpacing(50);
layout->addWidget(new QPushButton("fifth"));
w->show();
It will be a minimum space of 50 pixel between fourth and fifth:
If you want to put the button fifth at the bottom and keep the others at the top, use QVBoxLayout::addStretch():
QWidget* w = new QWidget();
QVBoxLayout* layout = new QVBoxLayout(w);
layout->addWidget(new QPushButton("first"));
layout->addWidget(new QPushButton("second"));
layout->addWidget(new QPushButton("third"));
layout->addWidget(new QPushButton("fourth"));
layout->addStretch(1);
layout->addWidget(new QPushButton("fifth"));
w->show();

Try adding QSpacerItem.
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(new QPushButton("first"));
layout->addWidget(new QPushButton("second"));
layout->addWidget(new QPushButton("third"));
layout->addWidget(new QPushButton("fourth"));
layout->addItem(new QSpacerItem(0, 0, QSizePolicy::Fixed, QSizePolicy::Expanding));
layout->addWidget(new QPushButton("fifth"));

Related

Why HBoxLayout added in a QVBoxLayout turned out to display horizontally, but not vertically?

I want to make a QLable and a QComboBox display in one line (horizontal, let's name it as h_combination_1), and there are some similar layouts like this (e.g. h_combination_2, h_combination_3, and etc). Then I want to manage this combinations (h_combination_1..3) display vertially. But at last these combinations display horizontally, how strange it is! Could someone help me with this?
Here is the code:
void ThemeWidget::initLayout()
{
[..]
QHBoxLayout* themeLayout = new QHBoxLayout;
QLabel* themeLabel = new QLabel("Theme");
themeLayout->addWidget(themeLabel);
themeLayout->addWidget(cbox_theme);
QHBoxLayout* legendLayout = new QHBoxLayout;
QLabel* legendLabel = new QLabel("Legend");
themeLayout->addWidget(legendLabel);
themeLayout->addWidget(cbox_legend);
QHBoxLayout* animationLayout = new QHBoxLayout;
QLabel* animationLabel = new QLabel("Animation");
themeLayout->addWidget(animationLabel);
themeLayout->addWidget(cbox_animation);
antialiasCheckBox = new QCheckBox("Antialiasing");
[..]
QVBoxLayout* parametersLayout = new QVBoxLayout;
parametersLayout->addLayout(themeLayout);
parametersLayout->addLayout(legendLayout);
parametersLayout->addLayout(animationLayout);
parametersLayout->addWidget(antialiasCheckBox);
QWidget* paramsBox = new QWidget();
paramsBox->setLayout(parametersLayout);
gridLayout = new QGridLayout;
QHBoxLayout* layout = new QHBoxLayout;
layout->addLayout(gridLayout);
layout->addWidget(paramsBox);
this->setLayout(layout);
}
Here is a screenshot:
In the begining, I've just add the layout parametersLayout to layout, without binding it to a QWidget.
For example:
void ThemeWidget::initLayout()
{
[..]
QVBoxLayout* parametersLayout = new QVBoxLayout;
parametersLayout->addLayout(themeLayout);
parametersLayout->addLayout(legendLayout);
parametersLayout->addLayout(animationLayout);
parametersLayout->addWidget(antialiasCheckBox);
gridLayout = new QGridLayout;
QHBoxLayout* layout = new QHBoxLayout;
layout->addLayout(gridLayout);
layout->addWidget(parametersLayout);
this->setLayout(layout);
}
But it turned out just as same as binding parametersLayout to a QWidget.
The layoutItems in parametersLayout should be vertical, but there were not!
So I changed into the method I discribed in the details above, and the results was still the same.

QComboBox not aligning to right

I have a QHBoxLayout and i want to add some children to the right, and other to the left:
label = new QLabel(...);
layout->addItem(label);
layout->setAlignment(label, Qt::AlignLeft);
select = new QComboBox(...);
layout->addItem(select);
layout->setAlignment(select, Qt::AlignRight);
However select is not been aligned to the right... any idea?
Neither with QCheckBox and QLabel, but works properly using QSlider
QHBoxLayout * layout = new QHBoxLayout(this);
QLabel * label = new QLabel("this is label");
layout->addWidget(label, Qt::AlignLeft);
layout->setAlignment(label, Qt::AlignLeft);
QComboBox * select = new QComboBox(this);
layout->addWidget(select);
layout->setAlignment(select, Qt::AlignRight);

QT dynamically generate label, LineEdit, Button

I want to do serial communication in the QT inside, according to the number of serial ports to dynamically generate label, LineEdit, Button, and these three buttons can pull down the scroll bar when the size of the interface, how to do well, I write this below is dead of.
The effect of encapsulation into a method
The interface was washed last
Define QGridLayout insude QScrollArea. And add your new widgets into
that layout in code. QGridLayout::addWidget
Define table where you will show several widgets in table cells. That real complicated way.
void BaseUi::BaseScrollArea()
{
QScrollArea *pArea = new QScrollArea();
QWidget *pWidget = new QWidget();
pWidget->setStyleSheet("QWidget" "{background:white;}");
m_vbox_layout = new QVBoxLayout();
m_vbox_layout->addSpacerItem(new QSpacerItem(100, 30,
QSizePolicy::Expanding, QSizePolicy::Expanding));
pWidget->setLayout(m_vbox_layout);
pArea->setWidget(pWidget);
pArea->setWidgetResizable(true);
m_main_layout = new QVBoxLayout();
m_main_layout->addWidget(pArea);
}
void BaseUi::addAutoRecordUi(QString lab_neme, QString ledit_name)
{
QWidget *page = new QWidget;
QGridLayout *layout = new QGridLayout(page);
QLabel *label = new QLabel;
label->setText(lab_neme);
label->setFont(font());
QLineEdit *ledit = new QLineEdit;
ledit->setText(ledit_name);
ledit->setFont(font());
layout->addWidget(label, 0, 1);
layout->addWidget(ledit, 0, 2);
page->setLayout(layout);
m_vbox_layout->insertWidget(m_vbox_layout->count()-1, page);
}
void BaseUi::addMulRecordUi(QString lab_neme, QString ledit_name, QString
but_name)
{
QWidget *page = new QWidget;
QGridLayout *layout = new QGridLayout(page);
QLabel *label = new QLabel;
label->setText(lab_neme);
label->setFont(font());
QLineEdit *ledit = new QLineEdit;
ledit->setText(ledit_name);
ledit->setFont(font());
QPushButton *but = new QPushButton(but_name);
but->setFont(font());
layout->addWidget(label, 0, 1);
layout->addWidget(ledit, 0, 2);
layout->addWidget(but, 0, 3);
page->setLayout(layout);
m_vbox_layout->insertWidget(m_vbox_layout->count()-1, page);
}

Prevent a QLabel from expanding horizontally with large words

I currently have something like this
QLabel* l = new QLabel(this);
l->setTextFormat(Qt::RichText);
l->set_IsSelf(IsSelf);
l->setWordWrap(true);
l->setText("Thissssssssssssssssssssssssssssssss");
l->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
l->setMaximumWidth(40);
Now I realize that width is very small and thats ok. What I want is to display all the content and make it expand vertically.
You should insert your label in a layout which it's sizeconstraint is set to QLayout::SetMinimumSize and set the vertical sizepolicy of your label to QSizePolicy::MinimumExpanding :
QVBoxLayout *layout = new QVBoxLayout(this);
layout->setSizeConstraint(QLayout::SetMinimumSize);
QLabel* l = new QLabel;
l->setWordWrap(true);
l->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
layout->addWidget(l);

check QCheckBox in QVboxLayout of QStackedWidget

i have a QstackedWidget and i add QHboxLayout and QVBoxLayout to it, in my vertical layout i add 3 QCheckBox, and i add this layout with two spacer for left and right side to it, then i push this new layout with two spacer for top and bottom in horizontal layout, now i want to check if this QCheckBoxes are checked or not, but i don't know how to check this.
here is my code :
QVBoxLayout *center = new QVBoxLayout();
QVBoxLayout *main = new QVBoxLayout();
QHBoxLayout *middle = new QHBoxLayout();
QSpacerItem *topspacer = new QSpacerItem(14, 24, QSizePolicy::Fixed, QSizePolicy::Fixed);
QSpacerItem *buttom = new QSpacerItem(4, 4, QSizePolicy::Expanding, QSizePolicy::Expanding);
QSpacerItem *left = new QSpacerItem(4, 4, QSizePolicy::Expanding, QSizePolicy::Expanding);
QSpacerItem *right = new QSpacerItem(4, 4, QSizePolicy::Expanding, QSizePolicy::Expanding);
middle->addItem(left);
middle->addLayout(center);
middle->addItem(right);
center->addWidget(checkBoxEvent(QString::fromStdString("Analyze events")));
center->addWidget(checkBoxEvent(QString::fromStdString("Edit Tracks")));
center->addWidget(checkBoxEvent(QString::fromStdString("Analyze players")));
main->addItem(topspacer);
main->addLayout(middle);
main->addItem(buttom);
Qwidget *m_page = new Qwidget();
m_page->setLayout(main);
m_stackedWidget->addWidget(m_page);
i tried this code for accessing to elements but i get segmentation fault:
for(int i = 0; i < m_stackedWidget->widget(2)->layout()->count(); i++)
if(dynamic_cast<QPushButton*>( m_stackedWidget->widget(2)->layout()->itemAt(i) )->isChecked())
this items are added to m_stackedWidget[1]