QT - Adding widgets to horizontal layout - c++

i have an horizontal layout and i am adding widgets by using
ui->horizontalLayout->addWidget(label);
But adding strategy is not what i want. For example, when i add items, it first puts the start, then puts at the end and keep putting from end.
But, what i want is that, when i add widget to layout, it should be put next to the previous widget. like that ,
is it possible to do that?

Add a stretcher to it after you have added all the widgets.
ui->horizontalLayout->addStretch();
will do.

You can add a spacer item, either on the outside of your horizontal layout, or inside your horizontal layout. If you add the spacer item inside the horizontal layout, you have to make sure you add your label widgets before the spacer item. Use the insertWidget() function to add your labels in this case.
Or you can add the spaceritem to the end of your horizontal layout after you've added your label widgets.
Here is an example:
QHBoxLayout *hlayout = new QHBoxLayout;
ui->verticalLayout->addLayout(hlayout);
QSpacerItem *item = new QSpacerItem(1,1, QSizePolicy::Expanding, QSizePolicy::Fixed);
hlayout->addSpacerItem(item);
for(int i = 0; i < 10; i++)
{
QLabel *label = new QLabel("NOTE");
hlayout->insertWidget(i, label);
}

setAlignment(Qt::AlignLeft); should do the trick. Also you can use this with: setSpacing(0);setContentsMargins(0, 0, 0, 0); to remove extra space between widgets.

Related

How do I prevent QListView in QGraphicsScene from drawing outside scroll area when scaling with QT_SCALE_FACTOR

When I display a QListWidget or QListView in a QGraphicsScene the text for each item is drawn outside of the widget boundaries if scroll bars are necessary. This only occurs when I use QT_SCALE_FACTOR to scale the application. Is there a way to prevent the list widget/list view items from being displayed outside the scroll area without explicitly setting a width for the items? If I set the width I lose some of the text. I have also tried setting the GraphicsItemFlag QGraphicsItem::ItemClipsToShape. This keeps the overhang restricted to the width of the outer widget but I want to prevent overhang entirely.
QListWidget in QGraphicsScene
QWidget *w = new QWidget();
QVBoxLayout *l = new QVBoxLayout(w);
lw = new QListWidget();
l->addWidget(lw);
QGraphicsProxyWidget *pw = scene()->addWidget(w);
pw->resize(200,300);
for(int i = 0; i < 25; i++) {
QListWidgetItem *litem = new QListWidgetItem("Text text text text text text text");
lw->insertItem(0, litem);
}
Setting the cache mode to QGraphicsItem::DeviceCoordinateCache prevents the listview/listwidget text from displaying outside the boundaries of the widget. The text does become a little blurry but the user can still read it. Thank you harism for your response!
pw->setCacheMode(QGraphicsItem::DeviceCoordinateCache);

How can I align Widgets left horizontally using Qt Creator's Designer

I want to create new QPushButtons and add them to my horizontal layout by pressing a "create Button"-button. I also want them to align left, so every new button should be right after the last added with a little spacing in between.
But here is what I get when I start my application and add create three buttons
First of all, I don't like my "create new Button"-Button to be centric. When I create one Button, both of them align left. But when I click a second time and a third time, the buttons are created with large space in between. I tried using spacer, but they only helped with the alignment problem of the createButton. Is there no simple way to just add buttons one button after the other like a horizontal stack?
This is my code i am using to generate buttons:
QPushButton *newCategory = new QPushButton(ui->category);
newCategory->setGeometry(0,0,140,60);
newCategory->setMinimumSize(140,60);
newCategory->setMaximumSize(140,60);
newCategory->setText("Test");
ui->horizontalLayout->addWidget(newCategory,0,Qt::AlignLeft);
You should use QBoxLayout::addStretch to push your buttons to the left.
An example:
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
btnLayout = new QHBoxLayout(this);
QPushButton *createBtn = new QPushButton("Create button");
btnLayout->addWidget(createBtn);
btnLayout->addStretch(1);
connect(createBtn, SIGNAL(clicked()), this, SLOT(addButton()));
}
void Widget::addButton()
{
// btnLayout->count() is equal to number of added buttons plus
// one QSpacerItem implicitly added by QBoxLayout::addStretch
int pos = btnLayout->count() - 1;
QPushButton *btn = new QPushButton;
btn->setText(QString("Button #%1").arg(pos));
btnLayout->insertWidget(pos, btn);
}

QT GridLayout add Stacked QLabel

I am creating an image gallery, i've implemented the reading in of Files and showing them in a resizable scroll-Area. We've decided to add meta-tags / Buttons and i am searching for a convenient way not to change too much but add this little features.
Any suggestion how i can achieve this? Can i add two Qlabels to each other? I tried to stuck two labels in a new layout and push this to the scrollWidgetLayout, but then i have only one Thumbnail.
//Create new ThumbNail-Object
thumbNail = new Thumbnail(ui->scrollArea);
scrollWidgetLayout->addWidget(thumbNail);
In the picture you can see what i have already and what i need (yellow).
You create a widget that acts like a container and put the labels inside it. Set a layout to this widget, I used QVBoxLayout. A better design would be to create a custom widget by subclassing QWidget, but I just used QFrame to make the example quick and simple.
centralWidget()->setLayout(new QVBoxLayout);
QScrollArea *area = new QScrollArea(this);
area->setWidgetResizable(true);
area->setWidget(new QWidget);
QGridLayout *grid = new QGridLayout;
area->widget()->setLayout(grid);
centralWidget()->layout()->addWidget(area);
for(int row = 0; row < 2; row++)
{
for(int column = 0; column < 5; column++)
{
QFrame *container = new QFrame; // this is your widget.. you can also subclass QWidget to make a custom widget.. might be better design
container->setStyleSheet("QFrame{border: 1px solid black;}"); // just to see the shapes better.. you don't need this
container->setLayout(new QVBoxLayout); // a layout for your widget.. again, if you subclass QWidget do this in its constructor
container->layout()->addWidget(new QLabel("TOP")); // the top label.. in your case where you show the icon
container->layout()->addWidget(new QLabel("BOTTOM")); // the bottom label.. in your case where you show the tag
grid->addWidget(container, row, column); // add the widget to the grid
}
}

Hiding a vertical layout programmatically?

I wanted to know if its possible to hide a vertical layout. I currently have a a horizontal layout with two vertical layouts.I wanted to hide one of the vertical layouts(with all its content) on button click. Any suggestions on how I could do that.
As #jmk said, you need to use a QWidget. I'll just add that it's very easy to turn an existing horizontal or vertical layout into a widget from Qt Designer by right-clicking on it and selecting Morph Into->QWidget:
The layout is entirely preserved, but now you can show/hide the layout box because it's an ordinary widget with that layout.
Instead of inserting vertical layouts directly into your top-level horizontal layout, use container widgets to easily control visibility:
// Create your left and right widgets
QWidget* leftWidget = new QWidget();
QVBoxLayout* leftLayout = new QVBoxLayout(leftWidget);
QWidget* rightWidget = new QWidget();
QVBoxLayout* rightLayout = new QVBoxLayout(rightWidget);
// Populate your vertical layouts here ...
QHBoxLayout* horizontalLayout = new QHBoxLayout(parentWidget);
horizontalLayout->addWidget(leftWidget);
horizontalLayout->addWidget(rightWidget);
Then, you can simply hide or show leftWidget or rightWidget to effectively control the visibility of everything in the vertical layouts that you have, without having to hide/show each individual widget.
My suggestion:
// l is the layout pointer
for (int i = 0; i != l->count(); ++i) {
QWidget* w = qobject_cast<QWidget*>(l->itemAt(i));
if (w != 0) {
w->setVisible(false); // hides the widget
}
else {
// do some recursive things with the layout
}
}
(Hope it works ;))
The widget is basically invisible.

QScrollBar + QScrollAera in QTabWidget

My question is simple : how can I set a QScrollBar in my QScrollArea. I have tested a lot of things but nothing work ... Maybe it's a problem to set a QScrollArea in a QTabWidget ? Here is the code :
void GamesWindow::createTabSucces()
{
std::string nameImg;
_succesPage = new QWidget(_tab);
_tab->addTab(_succesPage, " Succes ");
scrollArea = new QScrollArea(_succesPage);
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setFixedSize(500,500);
/* Integration of QScrollBar */
for (int i = 0; i < 45; i++)
{
nameImg = "img/allAchiv/";
nameImg += intToString(i + 1);
nameImg += ".jpg";
_imgSucc[i] = new QLabel(scrollArea);
_imgSucc[i]->setPixmap(QPixmap(QString(nameImg.c_str())));
_imgSucc[i]->setGeometry((14 + (85 * (i % 5))), 46 + ((i / 5) * 85), 60, 60);
}
}
In fact, I add pictures in a tab where is created a QScrollArea (like 8-9 lines of pictures) but only 5 are visible, the others are hide, because they are at the bottom, out of the defined zone.
Any idea ? Thank's.
You must:
add a layout to your page (QVBoxLayout)
add the scroll area to that page layout
add a layout to the viewport() widget in the scroll area (QVBoxLayout)
add your QLabels to that viewport layout
This way you won't need to call setGeometry on each label
You need to set a widget to your scroll area and add your pictures to the widget's layout. Check QScrollArea::setWidget(QWidget *widget)
I'm almost sure that you can't add scroll to a tab widget but my idea is just to try make more tabs that can be shown and see if the slider comes up by default.