Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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.

    0_1545125786409_Capture.PNG


  • Moderators

    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.


  • Qt Champions 2019

    @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 the sizeHint() 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?


  • Moderators

    @tomy said in Sizes of windows in a simple program:

    Thanks to both of you.
    So the best way to set size is via the sizeHint() 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:
    0_1545208411124_bdaf311d-bfb3-4b63-b924-3c90edf0423a-image.png



  • @J.Hilk
    Thanks so much.
    It's more than one hour I'm seeking that address on the Docs but failed to find it. I only find this. Sorry.
    Would you please tell me how you reached that address.


  • Lifetime Qt Champion

    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!


  • Lifetime Qt Champion

    The easiest is to go to the "all C++ classes" page and search there.



  • @SGaist
    I even can't find such a phrase either. My bad (or bad Doc).
    From this point, what item do you click so that it takes you to "all C++ classes"?
    I think you don't mean All Qt C++ Classes.


  • Lifetime Qt Champion

    Look on the left side of the page. Under Reference.



  • @SGaist
    Sorry, there's no "all C++ classes" on my page but "All Qt C++ Classes", which doesn't go to this page.
    Still it's mysterious how you could get access to the last mentioned page from the homepage of Docs!


  • Lifetime Qt Champion

    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!


  • Qt Champions 2019

    @tomy Why don't you simply enter "QWidget" (or whatever you're looking for) in Google?


  • Moderators

    @tomy
    you're using QtCreator right ? just write

    QWidget 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.

    @J.Hilk

    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.


Log in to reply