Sizes of windows in a simple program
-
Hi all,
Please take a look at this simple code:
#include <QApplication> #include <QHBoxLayout> #include <QSlider> #include <QSpinBox> int main(int argc, char* argv[]) { QApplication app(argc, argv); QWidget* windo = new QWidget; windo -> setWindowTitle("Enter Your Age"); QSpinBox* spinBox = new QSpinBox; QSlider* slider = new QSlider(Qt::Horizontal); spinBox -> setRange(0, 130); slider -> setRange(0, 130); QObject::connect(spinBox, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int))); QObject::connect(slider, SIGNAL(valueChanged(int)), spinBox, SLOT(setValue(int))); spinBox -> setValue(35); QHBoxLayout* layout = new QHBoxLayout; layout -> addWidget(spinBox); layout -> addWidget(slider); windo -> setLayout(layout); windo -> show(); return app.exec(); }
The lines are obvious as for what they do. But my question is, what factor in general does determine the size of the window in these programs please?
For instance, if you look the output window, you see that the word "Age" is covered. If we have a longer window that word will be shown correctly.
-
Hi all,
Please take a look at this simple code:
#include <QApplication> #include <QHBoxLayout> #include <QSlider> #include <QSpinBox> int main(int argc, char* argv[]) { QApplication app(argc, argv); QWidget* windo = new QWidget; windo -> setWindowTitle("Enter Your Age"); QSpinBox* spinBox = new QSpinBox; QSlider* slider = new QSlider(Qt::Horizontal); spinBox -> setRange(0, 130); slider -> setRange(0, 130); QObject::connect(spinBox, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int))); QObject::connect(slider, SIGNAL(valueChanged(int)), spinBox, SLOT(setValue(int))); spinBox -> setValue(35); QHBoxLayout* layout = new QHBoxLayout; layout -> addWidget(spinBox); layout -> addWidget(slider); windo -> setLayout(layout); windo -> show(); return app.exec(); }
The lines are obvious as for what they do. But my question is, what factor in general does determine the size of the window in these programs please?
For instance, if you look the output window, you see that the word "Age" is covered. If we have a longer window that word will be shown correctly.
hi, @tomy in your case the layout manages the size of the window.
By calculating the needed size, it consideres constrains such as full screen or nor, screen limitations etc and sizeHint min/max Sizes and SizePolicies of all it children and resizes acordingly.
-
Hi all,
Please take a look at this simple code:
#include <QApplication> #include <QHBoxLayout> #include <QSlider> #include <QSpinBox> int main(int argc, char* argv[]) { QApplication app(argc, argv); QWidget* windo = new QWidget; windo -> setWindowTitle("Enter Your Age"); QSpinBox* spinBox = new QSpinBox; QSlider* slider = new QSlider(Qt::Horizontal); spinBox -> setRange(0, 130); slider -> setRange(0, 130); QObject::connect(spinBox, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int))); QObject::connect(slider, SIGNAL(valueChanged(int)), spinBox, SLOT(setValue(int))); spinBox -> setValue(35); QHBoxLayout* layout = new QHBoxLayout; layout -> addWidget(spinBox); layout -> addWidget(slider); windo -> setLayout(layout); windo -> show(); return app.exec(); }
The lines are obvious as for what they do. But my question is, what factor in general does determine the size of the window in these programs please?
For instance, if you look the output window, you see that the word "Age" is covered. If we have a longer window that word will be shown correctly.
@tomy If you don't set the size explicitly it will be set so that the content is properly sized or (if you use designer) to what it is in designer. The title-bar of the window isn't drawn by Qt, it is up to OS or window manager used.
-
-
Thanks to both of you.
So the best way to set size is via thesizeHint()
function, yeah?Previously I had a page on Docs in which there were the names of all functions plus the way they can be used. I can't find that page now. Do you use it please?
@tomy said in Sizes of windows in a simple program:
Thanks to both of you.
So the best way to set size is via thesizeHint()
function, yeah?Previously I had a page on Docs in which there were the names of all functions plus the way they can be used. I can't find that page now. Do you use it please?
sizeHint is usually the way to go.
That page is still there:
-
@tomy said in Sizes of windows in a simple program:
Thanks to both of you.
So the best way to set size is via thesizeHint()
function, yeah?Previously I had a page on Docs in which there were the names of all functions plus the way they can be used. I can't find that page now. Do you use it please?
sizeHint is usually the way to go.
That page is still there:
-
Hi,
Do you mean the QWidget documentation ?
-
Hi,
Do you mean the QWidget documentation ?
@SGaist
Yeah, thanks.
How did you get to that point, I mean, the steps, please?
Whatever I searched, I couldn't find the way.Updated
I used this function to the code, but still no changes!windo->setMinimumSize(60, 20);
The length of the window won't change!
-
The easiest is to go to the "all C++ classes" page and search there.
-
Look on the left side of the page. Under
Reference
. -
Yes it's "All Qt C++ Classes" that I meant. It's just the intermediate step to go further and find the widget you want using a class "CTRL+F" search on the web page.
-
I can't find the page J.Hilk showed by the image and need to use your link to that page. That's it; that's Docs.
Anyway, I used the setMinimumSize function, as shown above, to the code but still no changes!
-
I can't find the page J.Hilk showed by the image and need to use your link to that page. That's it; that's Docs.
Anyway, I used the setMinimumSize function, as shown above, to the code but still no changes!
@tomy
you're using QtCreator right ? just writeQWidget w;
place the curser inside the QWidget text block and press F1, you get the same page inside QtCreator you also get on the webpage
Otherwise here are the google coordinates:
- Search: QWidget
- Link Number: 1
- Link Text: QWidget Class | Qt Widgets 5.12 - Qt Documentation
to your question, 60;20 is super tiny have you tried it with something big, for example (200,200) to see a difference ?
-
@jsulm
I thought when we are on Docs, everything inside that would be reachable through obvious steps.place the curser inside the QWidget text block and press F1
It's the best, at least for the point where I am now. I try using it rather than Docs web page from now on.
I needed length to be longer, hence:
window->setMinimumSize(200, 40);
Even this couldn't alter the length until 200 was replaced by 250.
Now it's fine.Thanks to all of you.