Qt5: QPushButon size setting - c++

it's a bit frustrating, but I can't find proper way to set QPushButton size. From what I read I suppose that this code should work:
// SEND BUTTON
sendButton = new QPushButton(this);
sendButton->setText("Send");
sendButton->setMinimumSize(QSize(0,0));
sendButton->setMaximumSize(QSize(10000,10000));
sendButton->setGeometry(QRect(QPoint(30, 100),QSize(10, 20)));
connect(sendButton, &QPushButton::released, this, &MainWindow::handleSendButton);
// CENTRAL WIDGET
QWidget* centralWidget = new QWidget(this);
centralWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
// LAYOUT
QGridLayout* layout = new QGridLayout(centralWidget);
layout->addWidget(sendButton,0,0,0,0);
But the button is always expanded to the whole application window. Could you please help?

The policies that the Widgets have by default is QSizePolicy::Preferred, but in your case you should use QSizePolicy::Fixed.
QSizePolicy::Preferred: The sizeHint() is best, but the widget can be
shrunk and still be useful. The widget can be expanded, but there is
no advantage to it being larger than sizeHint() (the default QWidget
policy).
QSizePolicy::Fixed: The QWidget::sizeHint() is the only acceptable
alternative, so the widget can never grow or shrink (e.g. the vertical
direction of a push button).
In addition you must establish the alignment to be centered, so that from the above you get the following:
sendButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
layout->addWidget(sendButton,0,0,0,0, Qt::AlignCenter);

Related

Is there anyway to add a widget and set its geometry on a qvboxlayout?

When a qboxlayout is created, any widget added to it would in its layout, and I create a widget and set its geomoetry, then add to the widget, the geometry doesn't work.
This is code:
QVBoxLayout *layout = new QVBoxLayout(centralWidget);
QWidget *widget1 = new QWidget();
QWidget *widget2 = new QWidget();
layout->addWidget(widget1);
layout->addWidget(widget2);
QWidget *widget3 = new QWidget();
widget3->setGeometry(0, 0, 100, 100);
layout->addWidget(widget3);
This is what I want to realize
I'm not sure to understand your question exactly, but if you want to define to size of your widget in the layout, you can maybe try widget3->setFixedSize(..). This should set both the maximum and minimum size of the widget.
However, the position will still be managed by the layout.
To have a hand on the position, you can use the Qt::Alignment flags in the addWidget() function.
Otherwise, if you really want to use the geometry directly, you should not add the widget to the layout, but simply create the widget3 with the centralWidget as parent.

Qt window resize event after resize is done

I have a QtChart in a QDialog and I use a simple QWidget to show it on the screen. I need to resize this hart whenever the dialog window is resized by user.
This is how I add the chart to the dialog (in constructor):
// Setup chart view to show the chart
mChartView = new QChartView(mChart, ui->widget);
mChartView->setParent(this);
mChartView->resize(ui->widget->size());
mChartView->setRenderHint(QPainter::Antialiasing);
I have overrided the resizeEvent of the QDialog in my own dialog:
void CurveDialog::resizeEvent(QResizeEvent *event)
{
mChartView->resize(ui->widget->size());
}
This works, and the chart gets resized...but the problem is it is terribly slow! because it will resize for all the steps that user drags the window corner to resize it!
How can I do the resize only when there resize is done? I wanted to use a timer but this looks like a dirty hack! any better ideas?
Qt provides a layout system to manage the geometries of child widgets within a widget. A layout will arrange the size and the position of each child to ensure that it will take all the available space.
The layout will automatically resize the child widgets when the parent is resized:
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
QDialog* dialog = new QDialog();
QVBoxLayout* layoutDialog = new QVBoxLayout(dialog);
QWidget* widget = new QWidget();
QVBoxLayout* layoutWidget = new QVBoxLayout(widget);
layoutDialog->addWidget(widget);
layoutWidget->addWidget(chartView);
dialog->exec();

Change QT layout background

Since QVBoxLayout has no a setStylesheet method, I thought this would made the trick:
QWidget *window = new QWidget(this);
window->setStyleSheet("background-image:url(:/images/sky.jpg);font-size:18px;");
QVBoxLayout * layout = new QVBoxLayout(window);
layout->addWidget(widg1);
layout->addWidget(widg2);
setLayout(layout);
Sadly, only a small rectangle of background image appears, not covering entire window. How could I do it?
You can set stylesheet to the central widget of your main window. In the example you can put window into some other layout.

How to make window look symmetric after adding buttons to taskbar?

I have QMainWindow and I want to populate statusbar with buttons.
I achieve this by using the code:
QWidget *widget = new QWidget();
QPushButton *leftBut = new QPushButton("left");
QPushButton *rightBut = new QPushButton("right");
QHBoxLayout *layout = new QHBoxLayout(widget);
layout->addWidget(leftBut, 1, 0);
layout->addWidget(rightBut, 1, 0);
statusBar()->addWidget(widget,1);
so, I have this: MainWindow with buttons in statusbar
But you can see that right border of layout are far from window corner, not like on the left side.
What I want is to make window look symmetric (move right border right or make borders invisible or something else).
What was needed is this string after those code that I wrote in question:
statusBar()->setSizeGripEnabled(false);
In some reason statusbar contains "size grip" that is situated in bottom-right corner of statusbar and is enabled by default. That wast the thing that prevent widget to fill whole space.

In QT, How to define the geometry for the Layout in a custom Widget

In a custom QWidget (say MyWidget) I include a layout to manage children widgets:
MyWidget window;
QPushButton button;
QVBoxLayout layout(window);
layout.addWidget(button);
window.show();
I would like the layout to be in a specific position and size, by default, QWidget set it to the whole geometry.
How can I set the geometry the layout will consider for managing his space?
As an indirect question:
Which function the layout use to set children geometries?
QPushButton *button = new QPushButton;
verticalLayout->addSpacing(300);
verticalLayout->addWidget(button);
button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
button->setMinimumSize(500, 200);
button->setMaximumSize(500, 200);
If you need both vertical and horizontal spacing - use QGridLayout.
The QLayout use as margins the summary of both:
The parent widget contents margins.
The layout contents margins.
The solution is to set the contents margins to the required value in the custom widget constructor. And when creating the layout, to set his contents margins to 0.
this->setContentsMargins(left, top, right, bottom);
// ...
layout->setContentsMargins(0,0,0,0);
The Layout setGeometry is called by QWidget resize event, and set children widgets size according with it. As all functions are not virtual, it is not possible (or at least difficult) to modify in this behavior.