QGridLayout 3x3 grid resize widgets - c++

I have 9 QWidgets. Sometimes they are invisible. They are all in a 3x3 grid.
[0][3][6]
[1][4][7]
[2][5][8]
Now if for example widget 3 is made invisible, other widgets should should resize:
[ 0 ][ 6 ]
[ 1 ][ 4 ][ 7 ]
[ 2 ][ 5 ][ 8 ]
The same should happen vertically, basically all the empty space should be filled but I'm not sure how to do that. When I set a widget invisible, the empty space is not taken.
QGroupBox *parent = getParent();
QGridLayout *layout = new QGridLayout;
layout->setMargin(0);
layout->setMargin(5);
int w = parent->geometry().width();
int h = parent->geometry().height();
QSize min(w/3-2*layout->spacing(),h/3-2*layout->spacing());
QSize max(w,h);
for (int n = 0; n < 6; ++n)
{
layout->setRowStretch(n,6);
layout->setColumnStretch(n,6);
}
for (size_t n = 0, t = widgets.size(); n < t; ++n)
{
QWidget *widget = widgets[n];
widget->setMinimumSize(min);
widget->setMaximumSize(max);
widget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
layout->addWidget(widget);
}
Thanks in advance for the help.

I have the similar experience. I used such widget to visualize dynamically changed configuration of device.
The required behavior can be achieved by using QVBoxLayout as row and QWidget ad cell (column). In my case I used custom widget derived from QLabel as cell. Everything works from the box without tuning of geometry.
Pseudo code:
// Inside widget's constructor (or createLayout function).
topLayout = new QVBoxLayout ();
aLayout = new QHBoxLayout ();
bLayout = new QHBoxLayout ();
cLayout = new QHBoxLayout ();
topLayout->addLayout (aLayout);
topLayout->addLayout (bLayout);
topLayout->addLayout (cLayout);
setLayout (topLayout);
...
// Add cell to corresponding row.
void add_cell (QWidget * cell)
{
aLayout->addWidget (cell);
}

First, remove the widget from the layout by iterating the layout and removing the item that holds the widget. While doing that you also collect the information about available cells. Then reinsert the widget with proper position and spans.

As far as I know, there is no way to do that automatically. I would suggest you to keep a list of the widgets and reposition them by clearing the grid layout and populating it again each time a widget changes its visibility. I found out that only this way I can solve a similar task that I had (for rearranging the widgets in the grid) without creating a custom layout by subclassing QLayout.

Related

QTableView bigger than its container

I am working on Qt applicaction. There I have QMainWindow. Inside it I have added QTableView. When I run the application I see that I need to scroll to display the whole table and also blank space shows up below it.
I would like main window to resize horizontally in order to use space needed by the table. Also I would like it to resize vertically to not having space unused. How could I achieve that?
This is my code so far:
void MainWindow::initUi() {
setWindowTitle(tr("Main Window"));
QWidget* centralWidget = new QWidget(this);
QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);
QFormLayout *upperLayout = new QFormLayout;
// Default layout appearance of QMacStyle
upperLayout->setRowWrapPolicy(QFormLayout::DontWrapRows);
upperLayout->setFieldGrowthPolicy(QFormLayout::FieldsStayAtSizeHint);
upperLayout->setFormAlignment(Qt::AlignHCenter | Qt::AlignTop);
upperLayout->setLabelAlignment(Qt::AlignLeft);
QVBoxLayout *resultsLayout = new QVBoxLayout;
QTableView* table = new QTableView(centralWidget);
table->verticalHeader()->hide();
QStandardItemModel* model= new QStandardItemModel(4, 4);
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 4; ++column) {
QStandardItem *item = new QStandardItem(QString("row %0, column %1").arg(row).arg(column));
model->setItem(row, column, item);
}
}
table->setModel(model);
QLabel* upperLabel = new QLabel(tr("Label:"), centralWidget);
upperLabel->setAlignment(Qt::AlignLeft);
resultLabel = new QLabel(tr("Result goes here"), centralWidget);
mainLayout->addLayout(resultsLayout);
resultsLayout->addLayout(upperLayout);
resultsLayout->addWidget(table);
upperLayout->addRow(upperLabel, resultLabel);
centralWidget->setLayout(mainLayout);
setCentralWidget(centralWidget);
this->adjustSize();
}
Set the sizeAdjustPolicy of the table to AdjustToContents view, then set the size policy to Fixed in both horizontal and vertical directions.
AdjustToContents might incur a slight performance penalty for dynamic contents in the view, since every data change may change the layout.
The Qt Designer is a really nifty tool to figure layout issues out quickly; the {table,list,tree} widgets behave exactly the same as the views do (because they're the same) and the widgets can be quickly filled with dummy data in Qt Designer.

Widgets with FixedSize change their size at will

I have a QGridLayout inside a center widget, which contains a supposedly fixed-sized widget (referred to as inner widget) that also has a QGridLayout filled with buttons. Inner widget's size is determined by how many buttons are there in the grid, and is supposed to be an exact fit (no spacing between the buttons, FixedSize policy applied in buttons' constructor), and all buttons have their sizes and policies set in the constructor. Now, if I don't put inner widget into a layout of any kind, it works just fine, I get nice square buttons. But if I put inner widget into a grid layout, all buttons suddenly change their sizes, and widget also doesn't seem like keeping its size. Why?
Edit: MyButtonTable:
MyButtonTable::MyButtonTable(QWidget *parent) : QWidget(parent), array()
{
size_x = 2;
size_y = 2;
QGridLayout* layout = new QGridLayout();
for(size_t x = 0; x < size_x; x++) {
this->array.push_back(std::vector<MyRightClickButton*>());
}
for(size_t x = 0; x < size_x; x++) {
for(size_t y = 0; y < size_y; y++) {
this->array[x].push_back(new button_t());
QObject::connect(array[x][y], SIGNAL(rightClicked()), this, SLOT(internalRightClick()));
QObject::connect(array[x][y], SIGNAL(clicked()), this, SLOT(internalClick()));
layout->addWidget(array[x][y], x, y);
}
}
layout->setSpacing(0);
layout->setContentsMargins(0,0,0,0);
this->setLayout(layout);
this->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
this->setMinimumSize(QSize(0,0));
this->resize(QSize(10*size_y,10*size_x));
}
MyRightClickButton(QWidget *parent = 0):QPushButton(parent) {
marked = false;
this->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
this->setMinimumSize(QSize(0,0));
this->resize(QSize(10,10));
}
A layout manager is used to arrange child widgets in them. The arrangement designates each layout manager. Layout managers adjust the size of their child widgets based on the resize policies. Even if you are setting a fixed size for a child widget, using resize() or setGeometry(), they will be resized by the layout manager, if you did not set the resize policy of the child widget.
For example
widget->setFixedSize (100, 100);
widget->setSizePolicy (QSizePolicy::Fixed, QSizePolicy::Fixed);
This is how it should be done.

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
}
}

Spacing between widgets in QHBoxLayout

I'm trying to create a GUI with QtCreator. For this GUI, I need to display several images with different sizes next to each other. These images should be touching each other.
I use a QWidget with a QHBoxLayout, where I add the labels (with different sizes) containing the images.
According to related questions, I should use setSpacing and setContentsMargin to remove these spaces, but that won't work; I tried several times.
Here's the code:
QWidget *widget = new QWidget(ui->tagcloud);
QHBoxLayout * l = new QHBoxLayout(widget);
ui->tagcloud->setWidget(widget);
for(int i=0;i<list.size();++i)
{
QLabel *lab = new QLabel;
QPixmap pic((list[i].imgPath).c_str()); //This fetches the image
int sizeChange = 50 + (2*list[i].percent); //Calculates the size of the image
lab->setFixedSize(QSize(sizeChange, sizeChange));
lab->setPixmap(pic);
lab->setScaledContents(true);
l->addWidget(lab);
l->setSpacing(0);
}
However, when I run this, the spacing remains the same (i.e. definitely not zero).
If I add more labels to the layout, the spacing seems to get smaller.
Can anyone explain or help me? Thanks!
Setting spacing to 0 and adding stretch before and after works for me :
l->addStretch();
for(int i = 0; i < list.size(); ++i)
{
QLabel *lab = new QLabel;
QPixmap pic((list[i].imgPath).c_str()); //This fetches the image
int sizeChange = 50 + (2*list[i].percent); //Calculates the size of the image
lab->setFixedSize(QSize(sizeChange, sizeChange));
lab->setPixmap(pic);
lab->setScaledContents(true);
l->addWidget(lab);
}
l->addStretch();
l->setSpacing(0);
Also this works I think
l->setSizeConstraint(QLayout::SetMaximumSize);

Add widgets to QFileDialog

I need to add a widget (QTableWidget) into QFileDialog's layout. I know that it is QGridLayout with sizes (3,4). The table must be in 3-rd row and span all columns.
QTableWidget* tableWidget = new QTableWidget(this);
QGridLayout *layout = static_cast<QGridLayout*>(QFileDialog::layout());
layout->addWidget(tableWidget, 2, 0, 1, 4);
With this code the original 3-rd row which contains lineEdit and save/open pushButton disappears. How can I add widgets between already existing widgets of QGridLayout so that original widgets remain in the layout.
I strongly recommend you not to rely on QFileDialog's implementation. The layout can be different on different platforms or different versions of Qt. It may be more correct to place your table under the dialog or to the right of it. This can be done easily without altering the layout of the QFileDialog itself. Just create a QVBoxLayout and put QFileDialog and QTableWidget inside it.
However, the question has been asked, and the solution exists. QGridLayout has no functionality such as QBoxLayout::insertItem. So we need to implement this behavior manually. The plan is:
Obtain the list of layout items placed in 3rd and 4th rows.
Calculate new positions of items.
Take elements out of item and add them back at new positions.
Working code:
QFileDialog* f = new QFileDialog();
f->setOption(QFileDialog::DontUseNativeDialog, true); //we need qt layout
QGridLayout *layout = static_cast<QGridLayout*>(f->layout());
QList< QPair<QLayoutItem*, QList<int> > > moved_items;
f->show();
for(int i = 0; i < layout->count(); i++) {
int row, column, rowSpan, columnSpan;
layout->getItemPosition(i, &row, &column, &rowSpan, &columnSpan);
if (row >= 2) {
QList<int> list;
list << (row + 1) << column << rowSpan << columnSpan;
moved_items << qMakePair(layout->takeAt(i), list);
i--; // takeAt has shifted the rest items
}
}
for(int i = 0; i < moved_items.count(); i++) {
layout->addItem(moved_items[i].first,
moved_items[i].second[0],
moved_items[i].second[1],
moved_items[i].second[2],
moved_items[i].second[3]);
}
QTableWidget* tableWidget = new QTableWidget();
layout->addWidget(tableWidget, 2, 0, 1, 4);