Important: Please read the Qt Code of Conduct -

[Solved] Custom Widget with layout doesn’t resize

  • Hi there,
    I've written a custom widget with the following code:

    @QPlot::QPlot(QWidget *parent) : QWidget(parent)
    // Create vertical layout ---------------------------------------------------------------
    m_vLayout = new QVBoxLayout();
    // Set vertical layout margin to zero

    // Create horizontal layout
    m_hLayout = new QHBoxLayout();
    // Set horizontal layout to zero

    // Add horizontal layout to vertical layout

    // Pause button --------------------------------------------------------------------------
    m_pbPause = new QPushButton("Pause", this);
    connect (m_pbPause, SIGNAL(clicked()), this, SLOT(on_m_pbPause_clicked()));

    // Element count button (plot speed) -----------------------------------------------------
    m_pbElementCount = new QPushButton("Plot speed: 0", this);
    connect (m_pbElementCount, SIGNAL (clicked()), this, SLOT(on_m_pbElementCount_clicked()));

    // Autoscale button ----------------------------------------------------------------------
    m_pbAutoScale = new QPushButton("AutoScale", this);
    connect (m_pbAutoScale, SIGNAL(clicked()), this, SLOT(on_pbAutoscale_clicked()));

    // Create new GL plot --------------------------------------------------------------------
    m_glplot = new QGLPlot(this);
    // Add glPlot to vertical layout

    // Make vertical layout the layout of the QPlot widget

    it comprises a vertical layout, in which a horizontal layout is added, which holds three buttons. Then a custom GLWidget for plotting data is in the "second" line of the vertical layout.
    In the designer I've got an tab with two widget which have been promoted to the custom QPlot widget. The tab itself has a vertical layout. So far everything works fine:
    ! 1)!
    Now I want to change the size of the GLWidget by setting its max. size:
    This also works but, the layout is not adjusting to the new size of the widget. I wanna use that to give the other plot more room, and thus resulting in a bigger size.
    Here's a pic of the problem:
    ! 2)!

    I hope anyone can give me a hint where to search for. I've already played with the sizePolicy setting, but no luck. UpdateGeometry() also didn't do the trick ...

    Thanks in advance and best regards,


  • You should use sizehint instead of size everytime you use widgets in a layout.

  • Hi Eddy,
    thanks for your reply. I'm not exactly sure what you mean. The manual says "sizeHint: This property holds the recommended size for the widget." But sizeHint itself is const. But can be changed by the functions: setMinimumSize, setMaximumHeight, setMaximumWidth. Did I miss a function?


  • what you need is layoutstretch

    In my opinion it is easier to experiment with this in Qt Designer. There you see all the properties of your vertical layout. And you can preview what you try without compiling everytime.

    Setmaximumheight is meant for widgets that are not in a layout.

  • Hi Eddy,
    I know what layoutstretch does and you're 100% right. Thats what I need. The question now is:

    Can I control the "parent" layout in which my widget is in?

    What I want is, that if I double click on the plot, it shall shrink to a lower height ("minimize" ), giving the other widgets in the layout more "room".

    I've been playing with this:
    @this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);@


    @this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);@

    and this seems to work almost as wanted. But I cannot control the max height of the widget. When I debug the sizeHint of my custom Widget:

    @qDebug () << this->sizeHint();@

    it says:
    QSize(-1, -1)
    although I've set the minimum height and width before with:
    this->setMinimumSize (100, 30);

    Thanks again for your post! I really appreciate it!


  • I think I've got it. Within the designer I set the minimum height of the custom widget to 60 for example. In combination with the setSizePolicy functions that did the trick ...

    Thanks again Eddy!


  • My pleasure, but basically you solved it. I was just thinking out loud without Qt Creator, using my mobile.

    Glad you made it. Your program looks cool.

    By the way do you know qwt? It's great for making graphs like yours.

  • Thanks. I've look at qwt and it's really cool! I think I've started doing my own plot widget, just because of the fun of it ;-)


  • In that case have fun.

    By the way, could you edit your title and put [Solved] at the beginning. Just a convention in this forum to let everybody know the status of this topic.

  • Thanks & done ...


Log in to reply