QGridLayout Widget disappearing after using QAlignment - qt

I have the following:
QGridLayout *layout = new GridLayout();
layout->addWidget(rightBuf, row, 0);
layout->addWidget(i, row, 1);
layout->addWidget(leftBuf, row, 2);
Which displays fiine. However, when I add the alignment to the second line, ie:
layout->addWidget(i, row, 1, Qt::AlignTop);
The widget disappears from the screen. I think I have tried about just everything to get it to align top, including putting it into a QVBoxLayout and then adding it to the QGridLayout. Is there anything missing here?

Related

How to calculate QTextEdit height accurately by its contents to get rid of vertical scrollbars?

I need a QTextEdit widget which height grows or shrinks to the size of the inner content. I didn't find built-in solution and decided to make my own with connections to &QTextEdit::textChanged signals that will update size of the widget at runtime. In the code snippet I flattened my custom component to raw QTextEdit for simplicity. The problem is that calculation via QFontMetrics is inaccurate, a vertical scrollbar appears with only some pixels to scroll
I want to get rid of any vertical scrollbars in these text widgets.
However, I don't need to get rid of horizontal scrollbars and here is the second problem arises. Seemingly, horizontal scrolls occupy space in the viewport of the text widget and its internal height shrinks by the height of the scrollbar itself
void TestWindow::setupUi(QWidget *parent)
{
QVBoxLayout *v_box0 = new QVBoxLayout(parent);
v_box0->setContentsMargins(0, 0, 0, 0);
QVBoxLayout *v_scroll_layout = new QVBoxLayout();
v_scroll_layout->setContentsMargins(0, 0, 0, 0);
v_scroll_layout->setSpacing(0);
v_scroll_layout->setAlignment(::Qt::AlignTop);
QWidget *scroll_content = new QWidget();
scroll_content->setLayout(v_scroll_layout);
QScrollArea *v_scroll = new QScrollArea();
v_scroll->setWidgetResizable(true);
v_scroll->setWidget(scroll_content);
v_box0->addWidget(v_scroll);
QSize size;
QTextEdit *edit0 = new QTextEdit();
edit0->setLineWrapMode(QTextEdit::NoWrap);
edit0->setText("Hello World 1\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11");
edit0->setFont(viewer_font);
size = edit0->fontMetrics().size(0, edit0->document()->toPlainText());
edit0->document()->setDocumentMargin(0);
edit0->setMinimumHeight(size.height());
QTextEdit *edit1 = new QTextEdit();
edit1->setLineWrapMode(QTextEdit::NoWrap);
edit1->setText("New item");
edit1->setFont(viewer_font);
size = edit1->fontMetrics().size(0, edit1->document()->toPlainText());
edit1->document()->setDocumentMargin(0);
edit1->setMinimumHeight(size.height());
v_scroll_layout->addWidget(edit0);
v_scroll_layout->addWidget(edit1);
setFixedHeight(300);
}

How to control widgets position insid QStackedLayout

I've created a QStackedLayout in which I added 2 buttons, but I want to shift one button to the right corner insted of the left corner as you can see in the picture below:
What I've done so far:
QStackedLayout *stackedLayout = new QStackedLayout;
stackedLayout->addWidget(ui->pushButton_2);
stackedLayout->addWidget(ui->pushButton);
stackedLayout->setStackingMode(QStackedLayout::StackAll);
Place the QPushbutton in a container (i.e. QWidget) that allows it to be centered with an additional layout.
If I understand properly, you want a QPushButton above an other. The one above must be smaller and at the top right corner?
QVBoxLayout * vlayout_PB_under = new QVBoxLayout();
QHBoxLayout * hlayout_PB_under = new QHBoxLayout();
QPushButton * PB_x = new QPushButton("X",this);
QPushButton * PB_test = new QPushButton("test",this);
PB_x ->setFixedSize(20,20);
PB_test ->setFixedSize(200,200);
PB_test ->setLayout(vlayout_PB_under );
vlayout_PB_under ->addLayout(hlayout_PB_under);
hlayout_PB_under->addWidget(PB_x );
vlayout_PB_under ->setAlignment(Qt::AlignRight);
hlayout_PB_under->setAlignment(Qt::AlignTop );
The purpose of a QStackLayout is to allow switching easily the visible widget at a same position, not to display at the same time multiple widget...
Read the documentation : "where only one widget is visible at a time"

How to autofit widget size and position to a cell in QTreeWidget?

I pragrammaticaly create a QTreeWidget.
Then I pragrammaticaly add some items.
Then I add two QLabel widgets to two items (QTreeWidgetItem) by
myTree->setItemWidget(item1, 0, myLabel1);
myTree->setItemWidget(item2, 0, myLabel2);
And then I try to resize the row of the item pragrammaticaly.
If I use an
item1->setSizeHint(0, QSize(myWidth, myHeight) );
the row chaged. But myLabel1 is not.
If I use an
item1->setSizeHint(0, QSize(myWidth, myHeight) );
myLabel->resize(myWidth, myHeight);
everething is ok but the row of myLabel2 mis adjusting to label by position.
Can I do something to auto-adjusting a widget (by size and position) to a cell of QTreeWidget?
P.S. After any resizing of tree (resize by width or expand/collapse node) widgets updates correctly.
In view of the fact that autofit start after resizing QTreeWidget, there is some method inside that resize widgets in cells.
So I opened QTreeWidget description (https://doc.qt.io/qt-5/qtreewidget.html) and red all of Public Functions and Public Slots. When I did not find any useful function I looked at a parent class (QTreeView). And found
myTree->resizeColumnToContent(0);
Call resizeColumnToContent after resizing any row in a QTreeWidget and widgets will be always fit to cells.
P.S. I am the OP.
you can try adding/updating a widget as an item, for example:
QWidget* wdg = new QWidget;
QPushButton* btIcon = new QPushButton();
QLabel* lb = new QLabel();
QHBoxLayout* layout = new QHBoxLayout(wdg);
layout->addWidget(btIcon);
layout->addWidget(lb);
layout->setAlignment( Qt::AlignCenter );
wdg->setLayout(layout);
myTree->setItemWidget(item1, 0, wdg);
Or just fill the tree with widgets

Space Buttons equally in Layout, but with an empty slot

I'm trying to create a panel of buttons that will have 4 buttons, a space, and another button, all of equal space, like this:
I have tried to use Spacers, but it seems like those require a specific height and weight, and I would like this layout to be dynamic enough to appear correctly on any resolution, so a fixed size Spacer would not work.
I have tried to following code, but this just squishes the first 4 buttons to the top and the last one to the bottom, and doesn't space them out evenly.
QVBoxLayout *layout = new QVBoxLayout;
layout->setMargin(15);
layout->setSpacing(15);
layout->addWidget(button1, 1);
layout->addWidget(button2, 1);
layout->addWidget(button3, 1);
layout->addWidget(button4, 1);
layout->addWidget(button5, 2, Qt::AlignBottom);
layout->addStretch();
buttonPnl->setLayout(layout);
I also tried using a QGridLayout and specifying the height of each row, but this looks the same as the previous example.
QGridLayout *gridLayout = new QGridLayout;
gridLayout->setMargin(15);
gridLayout->setSpacing(15);
gridLayout->addWidget(button1, 0, 0);
gridLayout->addWidget(button2, 1, 0);
gridLayout->addWidget(button3, 2, 0);
gridLayout->addWidget(button4, 3, 0);
gridLayout->addWidget(button5, 5, 0);
gridLayout->setRowStretch(0, 1);
gridLayout->setRowStretch(1, 1);
gridLayout->setRowStretch(2, 1);
gridLayout->setRowStretch(3, 1);
gridLayout->setRowStretch(4, 1);
gridLayout->setRowStretch(5, 1);
How can I create a dynamic layout that will display my buttons correctly at any reasonable resolution?
It's a little bit ``hacky-slash'' but... the easiest way to get the desired behaviour is probably to define a spacer class that inherits from QPushButton but has an empty paintEvent definition...
class spacer: public QPushButton {
using super = QPushButton;
public:
using super::super;
protected:
virtual void paintEvent (QPaintEvent *event) override
{
}
};
Then just make sure you instantiate it with a text string that's in keeping with the other buttons so that it has a suitable return value from sizeHint(). So (based on your own example)...
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(button1);
layout->addWidget(button2);
layout->addWidget(button3);
layout->addWidget(button4);
/*
* Add a spacer using the text from button4 as a reference.
*/
layout->addWidget(new spacer(button4->text()));
layout->addWidget(button5);
layout->addStretch();
buttonPnl->setLayout(layout);
This gives me something like...

QHBoxLayout inside QFormLayout not left aligned

I have following chunk of simple code:
UeDisplaySettingsWidget::UeDisplaySettingsWidget(QWidget *parent)
: QWidget(parent)
{
this->setLayout(new QFormLayout());
this->ueSetDisplayDimmerSelectorLayout(new QHBoxLayout());
this->ueSetDisplayDimmerSelectorLabel(new QLabel(tr("Display fade level:"),
this));
this->ueInitDisplayDimmerLevelSelector();
this->ueInitDisplayDimmerTimeoutSelector();
connect(this->ueDisplayDimmerSelector(),
&QSlider::valueChanged,
this,
&UeDisplaySettingsWidget::ueSlotDisplayDimerSelectorValueChanged);
this->ueDisplayDimmerSelectorLayout()->addWidget(this->ueDisplayDimmerSelectorLabel());
this->ueDisplayDimmerSelectorLayout()->addWidget(this->ueDisplayDimmerSelector());
this->ueDisplayDimmerSelectorLayout()->addWidget(this->ueDisplayDimmerCurrentLevelIndicator());
dynamic_cast<QFormLayout*>(this->layout())->addItem(this->ueDisplayDimmerSelectorLayout());
dynamic_cast<QFormLayout*>(this->layout())->addRow(tr("Display fade timeout:"),
this->ueDisplayDimmerTimeoutSelector());
} // constructor
which produces following output:
Now, why QHBoxLayout returned by this->ueDisplayDimmerSelectorLayout() and containing QLabel with text Display fade level:, QSlider and QLCDNumber is not aligned with lower row of QFormLayout?
QFormLayout is basically a grid layout with two columns where you have labels in the left column and whatever in the right.
For some reason you but that fade out label this->ueDisplayDimmerSelectorLabel() in the horizontal layout and use QFormLayout::addItem(QLayoutItem*) for your first row and QFormLayout::addRow(const QString &, QWidget*)) for the second row. Thats why your DisplayDimmerSlectorLabel is together with its widget in the second column. Wtihout having it tested it should look something like this:
connect(this->ueDisplayDimmerSelector(),
&QSlider::valueChanged,
this,
&UeDisplaySettingsWidget::ueSlotDisplayDimerSelectorValueChanged);
this->ueDisplayDimmerSelectorLayout()->addWidget(this->ueDisplayDimmerSelector());
this->ueDisplayDimmerSelectorLayout()->addWidget(this->ueDisplayDimmerCurrentLevelIndicator());
dynamic_cast<QFormLayout*>(this->layout())->addRow(tr("Display fade level:"), this->ueDisplayDimmerSelectorLayout());
dynamic_cast<QFormLayout*>(this->layout())->addRow(tr("Display fade timeout:"),
this->ueDisplayDimmerTimeoutSelector());

Resources