I have a QHBoxlayout with children set up as follows:
QHBoxlayout h = new QHBoxlayout();
QLLabel leftLabel = new QLLabel("Left");
QLLabel centerLabel = new QLLabel("Center");
QHBoxlayout rightContent = new QHBoxlayout();
QLLabel r1 = new QLLabel("2");
QLLabel r2 = new QLLabel("3");
rightContent.addWidget(r1);
rightContent.addWidget(r2);
h.addWidget(leftLabel);
h.addWidget(centerLabel);
h.addLayout(rightContent);
This will create a QHBoxlayout with all the children floated on the left. I would like leftLabel to be on the left, centerLabel at the center, and rightContent to the extreme right.
How can I achieve this?
Thank you all in advance.
UPDATE
I would like something like left, center, right below:
Just add spacers between "Left", "Center" and "Right":
QHBoxLayout *h = new QHBoxLayout(&parentWidget);
h->addWidget(leftLabel);
h->addStretch()
h->addWidget(centerLabel);
h->addStretch()
h->addLayout(rightLabel);
Might be helpful to practice in Qt Designer.
Depending on what you want, you could also define the alignment while adding the widgets like so:
auto h = new QHBoxLayout();
h->addWidget(leftLabel, 0, Qt::AlignLeft);
h->addWidget(centerLabel, 0, Qt::AlignCenter);
h->addLayout(rightLabel, 0, Qt::AlignRight);
See here for more Information
Related
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);
I have code snippet looks like this:
nextPageBtn = new QToolButton();
nextPageBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
nextPageBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
nextPageBtn->setIcon(QIcon(":/next.png"));
nextPageBtn->setText("Next");
Currently I have two problems with this.
First:
I want the text is on the left of the icon, but with the code I provide, the text is on the right like this:
Second:
When the window is enlarged, I can not figure out a way to keep the text and icon in the center of the button. It looks like this when the button gets bigger:
Edit:
This is how I manage the layout:
nextPageHLayout = new QHBoxLayout; //This is the layout for QToolButton, it has two spacers and a QToolButton
mainVLayout->addLayout(nextPageHLayout); //mainVLayout is the main layout, and I put the mainVLayout to the central widget, and it also contains a QLabel above the nextPageHLayout
QSpacerItem *leftBtnSpacer = new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Fixed);
nextPageHLayout->addSpacerItem(leftBtnSpacer);
nextPageBtn = new QToolButton(mainWidget);
nextPageHLayout->addWidget(nextPageBtn);
nextPageBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
nextPageBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
nextPageBtn->setIcon(QIcon(":/next.png"));
nextPageBtn->setText("Next");
QSpacerItem *rightBtnSpacer = new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Fixed);
nextPageHLayout->addSpacerItem(rightBtnSpacer);
You must make the following changes:
You should not change the size policy of the QToolButton, that's why it's expanding.
You must change the layoutDirection to Qt::RightToLeft.
QHBoxLayout * nextPageHLayout = new QHBoxLayout; //This is the layout for QToolButton, it has two spacers and a QToolButton
mainVLayout->addLayout(nextPageHLayout); //mainVLayout is the main layout, and it also contains a QLabel above the nextPageHLayout
QSpacerItem *leftBtnSpacer = new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum);
nextPageHLayout->addSpacerItem(leftBtnSpacer);
nextPageBtn = new QToolButton(mainWidget);
nextPageHLayout->addWidget(nextPageBtn);
nextPageBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
nextPageBtn->setIcon(QIcon(":/next.png"));
nextPageBtn->setText("Next");
nextPageBtn->setLayoutDirection(Qt::RightToLeft);
QSpacerItem *rightBtnSpacer = new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum);
nextPageHLayout->addSpacerItem(rightBtnSpacer);
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);
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]
I have custom window class
#define NAME_WIDTH 150
#define NAME_HEIGHT 20
ObjectWindow::ObjectWindow(QWidget * parent)
{
}
void ObjectWindow::SetKey(KeyObject * keyObj)
{
QGridLayout * layout = new QGridLayout(this);
nameField = new QTextEdit(this);
nameField->setText(keyObj->name);
nameField->setGeometry(nameField->geometry().x(), nameField->geometry().y(),
NAME_WIDTH, NAME_HEIGHT);
layout->addWidget(nameField);
QHBoxLayout * picsLayout = new QHBoxLayout(this);
for(std::vector<ImageInstance*>::iterator imgObj = keyObj->images.begin(); imgObj != keyObj->images.end(); imgObj++)
{
QComboBox * folderList = new QComboBox;
picsLayout->addWidget(folderList);
QImage image((*imgObj)->imgPath);
QLabel * picLabel = new QLabel;
picLabel->setPixmap(QPixmap::fromImage(image).scaled(200, 200, Qt::KeepAspectRatio, Qt::SmoothTransformation));
picsLayout->addWidget(picLabel);
}
layout->addLayout(picsLayout, 2, 0);
QPushButton * saveBtn = new QPushButton(this);
saveBtn->setText("Save");
connect(saveBtn, SIGNAL(released()),this, SLOT(Save()));
layout->addWidget(saveBtn);
setLayout(layout);
}
What i need is
small text field to set the name, I don't unerstand why SetGeometry doesn't work
dropdown list above each image. I can create QHVertical layout for each set of image and list, but maybe there is more simple way to do it?
If you just want the user to set the name, a QLineEdit is probably enough.
Then the main advantage of using a QGridLayout is that you don't need to create other layouts. It acts like a grid where you put your widgets, a bit like Excel (and other spreadsheet programs).
Oh and I see that you are not constructing the Widgets in the constructor (which seems to be empty), that's what people usually do because constructing the UI can be expensive and you just want to update it when relevant, not rebuilding the whole UI just to update a field. But without more code I cannot tell when this function is being called.
You can try something like this:
QGridLayout * layout = new QGridLayout(this);
nameField = new QLineEdit(this);
nameField->setText(keyObj->name);
layout->addWidget(nameField, 0, 0, -1, 1); // expand to the right edge
int currentColumn = 0;
for(std::vector<ImageInstance*>::iterator imgObj = keyObj->images.begin(); imgObj != keyObj->images.end(); imgObj++)
{
QComboBox * folderList = new QComboBox;
layout->addWidget(folderList, 1, currentColumn);
QPixmap pixmap((*imgObj)->imgPath);
pixmap = pixmap.scaled(200, 200, Qt::KeepAspectRatio, Qt::SmoothTransformation);
QLabel * picLabel = new QLabel(this);
picLabel->setPixmap(pixmap);
layout->addWidget(picLabel, 2, currentColumn);
++currentColumn;
}
QPushButton * saveBtn = new QPushButton("Save", this);
connect(saveBtn, SIGNAL(released()),this, SLOT(Save()));
layout->addWidget(saveBtn, 3, 0, -1, 1);
setLayout(layout);
But it doesn't seem to be a good idea to add those widgets horizontally like that. What would happen if there are 100 items in this vector? You should investigate in using something like a QScrollArea or modifying the UI to give your client the best way to view and edit those (but without more context it seems difficult to give your more advice).