Prevent a QLabel from expanding horizontally with large words - c++

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);

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.

I want to add a label in new widget using the Qt framework

This is my code :
void maquette::on_btn_edit_clicked()
{
QWidget* wdg = new QWidget;
wdg->resize(320, 340);
wdg->setWindowTitle("Modiffier");
QLabel label1("matricule", wdg);
label1.setGeometry(100, 100, 100, 100);
wdg->show();
}
the window shows up but the label didn't show
void maquette::on_btn_edit_clicked()
{
QWidget *wdg = new QWidget;
wdg->resize(320,340);
wdg->setWindowTitle("Modiffier");
QLabel *label1 = new QLabel("matricule",wdg);
label1->setGeometry(100, 100, 100, 100);
wdg->show();
}
You can either add the QLabel using parenting. as mentioned before.
QLabel *label1 = new QLabel("matricule",wdg);
or
QLabel *label1 = new QLabel("matricule");
label1->setParent(wdg);
This will make the widget float inside its parent.
You can also add the QLabel to a layout that has been assigned to the QWidget.
QVBoxLayout* layout = new QVBoxLayout();
wdg->setLayout(layout);
QLabel *label1 = new QLabel("matricule");
layout->addWidget(label1);
This will add the widget to the layout.
The layout will control how the child widgets are laid out.

Why is the QScrollArea restricted in size?

To my custom widget, inherited from QWidget, I have added a QScrollArea like this:
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)//MainWindow is a QWidget
{
auto *scrollArea = new QScrollArea(this);
auto *widget = new QWidget(this);
widget->setStyleSheet("background-color:green");
scrollArea->setWidget(widget);
scrollArea->setWidgetResizable(true);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
QVBoxLayout *parentLayout = new QVBoxLayout(widget);
this->setStyleSheet("background-color:blue");
for(int i=0;i<12;i++){
QHBoxLayout* labelLineEdit = f1();
parentLayout->addStretch(1);
parentLayout->addLayout(labelLineEdit);
}
parentLayout->setContentsMargins(0,0,40,0);
}
QHBoxLayout* MainWindow::f1()
{
QHBoxLayout *layout = new QHBoxLayout;
QLabel *label = new QLabel("Movie");
label->setStyleSheet("background-color:blue;color:white");
label->setMinimumWidth(300);
label->setMaximumWidth(300);
layout->addWidget(label);
QLineEdit *echoLineEdit = new QLineEdit;
echoLineEdit->setMaximumWidth(120);
echoLineEdit->setMaximumHeight(50);
echoLineEdit->setMinimumHeight(50);
echoLineEdit->setStyleSheet("background-color:white");
layout->addWidget(echoLineEdit);
layout->setSpacing(0);
return layout;
}
This produces a window which looks like this:
The problem is, that I want the scrollArea to occupy the entire window, but it does not. It also doesn't get resized when I resize the window.
How could I fix this?
The problem is, that I want the scrollArea to occupy the entire
window, but it does not. It also doesn't get resized when I resize the window.
The reason is that you have not set any kind of layout to manage the positioning of your QScrollArea widget itself, so it is just being left to its own devices (and therefore it just chooses a default size-and-location for itself and stays at that size-and-location).
A simple fix would be to add these lines to the bottom of your MainWindow constructor:
QBoxLayout * mainLayout = new QVBoxLayout(this);
mainLayout->setMargin(0);
mainLayout->addWidget(scrollArea);

How to properly manage text and icon with QToolButton?

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);

QGridLayout with different size of cells

I'm trying to set a QGridLayout with four widget as in the image below:
however what I've managed with QGridLayout as of now is:
I don't see how I can set the size of the row different for column 0 and 1. Maybe QGridLayout is not the right way of doing it but I don't know of any other widget that would do the trick.
Does anyone have any idea how to achieve this?
I would use vertical and horizontal layouts instead of the grid layout. So you need two vertical layouts and horizontal one:
// Left side
QLabel *lbl1 = new QLabel(this);
QTableWidget *t = new QTableWidget(this);
QVBoxLayout *vl1 = new QVBoxLayout;
vl1->addWidget(lbl1);
vl1->addWidget(t);
// Right side
// QImage is not a widget, so it should be a label with image
QLabel *lbl2 = new QLabel(this);
QCustomPlot *pl = new QCustomPlot(this);
QVBoxLayout *vl2 = new QVBoxLayout;
vl2->addWidget(lbl2);
vl2->addWidget(pl);
// Create and set the main layout
QHBoxLayout mainLayout = new QHBoxLayout(this);
mainLayout->addLayout(vl1);
mainLayout->addLayout(vl2);
I don't think grids are the way to go here indeed...
You could try making a horizontal layout of 2 QFrames, in which you set a vertical layout each with the two widgets of that "column"