Important: Please read the Qt Code of Conduct -

Destination of sizeHint()

  • Hi to all! I have tryed to use sizeHint() only for understanding how it work and wrote a simple code (see below). As i understood documentation, this function is used by the Layout classes for change size of main window according to size of child widged, added in layer. I have implemented sizeHint() in class "my". In this class i draw a red rectangle 100x100.

    #include "my.h"
    #include <QPainter>
    #include <QSize>

    my::my(QWidget *parent):QWidget(parent) {


    void my::paintEvent(QPaintEvent *) {
    painter = new QPainter(this);
    painter->drawRect(0, 0, 100, 100);

    QSize my::sizeHint() {
    return QSize(100, 100);

    In code below i add this widget in a layer of main widget.

    CharList::CharList(QWidget *parent) : QWidget(parent) {
    test = new my; //child widget.
    QVBoxLayout *lay1 = new QVBoxLayout;

    As i understood correctly, QVBoxLayout shoud resize main widget (CharList) according to size my-wiget. But it's don't work. When i run the programm, the window whith the minimum size is appear... Can the anybody help me?

    [edit: added missing coding tags @ SGaist]

  • Hi,

    Take a look at the Basic Drawing Example.
    I'm not sure if it will solve your issue, but @ painter = new QPainter(this);@

    has to be @QPainter painter(this);@

    And perhaps you should start with

    and end with


Log in to reply