June 3, 2011

mmkk15 mmkk15
Lab Rat
6 posts

[Solved] Custom Widget with layout doesn’t resize

 

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

  1. QPlot::QPlot(QWidget *parent) : QWidget(parent)
  2. {
  3.  // Create vertical layout ---------------------------------------------------------------
  4.  m_vLayout = new QVBoxLayout();
  5.  // Set vertical layout margin to zero
  6.  m_vLayout->setMargin(0);
  7.  
  8.  // Create horizontal layout
  9.  m_hLayout = new QHBoxLayout();
  10.  // Set horizontal layout to zero
  11.  m_hLayout->setMargin(0);
  12.  m_hLayout->setSpacing(20);
  13.  
  14.  // Add horizontal layout to vertical layout
  15.  m_vLayout->addLayout(m_hLayout);
  16.  
  17.  // Pause button --------------------------------------------------------------------------
  18.  m_pbPause = new QPushButton("Pause", this);
  19.  m_hLayout->addWidget(m_pbPause);
  20.  connect (m_pbPause, SIGNAL(clicked()), this, SLOT(on_m_pbPause_clicked()));
  21.  
  22.  // Element count button (plot speed) -----------------------------------------------------
  23.  m_pbElementCount = new QPushButton("Plot speed: 0", this);
  24.  m_hLayout->addWidget(m_pbElementCount);
  25.  connect (m_pbElementCount, SIGNAL (clicked()), this, SLOT(on_m_pbElementCount_clicked()));
  26.  
  27.  // Autoscale button ----------------------------------------------------------------------
  28.  m_pbAutoScale = new QPushButton("AutoScale", this);
  29.  m_hLayout->addWidget(m_pbAutoScale);
  30.  connect (m_pbAutoScale, SIGNAL(clicked()), this, SLOT(on_pbAutoscale_clicked()));
  31.  
  32.  // Create new GL plot --------------------------------------------------------------------
  33.  m_glplot = new QGLPlot(this);
  34.  // Add glPlot to vertical layout
  35.  m_vLayout->addWidget(m_glplot);
  36.  
  37.  // Make vertical layout the layout of the QPlot widget
  38.  this->setLayout(m_vLayout);
  39. }

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:
Pic 1
Now I want to change the size of the GLWidget by setting its max. size:

  1. this->setMaximumHeight(30);

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:
Pic 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,

Mike

9 replies

June 3, 2011

Eddy Eddy
Area 51 Engineer
1295 posts

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

 Signature 

Qt Certified Specialist
Qt Ambassador

June 3, 2011

mmkk15 mmkk15
Lab Rat
6 posts

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?

Greetings,
Mike

June 3, 2011

Eddy Eddy
Area 51 Engineer
1295 posts

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.

 Signature 

Qt Certified Specialist
Qt Ambassador

June 3, 2011

mmkk15 mmkk15
Lab Rat
6 posts

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:

  1. this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Minimum);

and

  1. 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:

  1. qDebug () << this->sizeHint();

it says:
QSize(-1, -1)
although I’ve set the minimum height and width before with:

  1. this->setMinimumSize (100, 30);

Thanks again for your post! I really appreciate it!

Greetings,
Mike

June 3, 2011

mmkk15 mmkk15
Lab Rat
6 posts

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!

Greetings,
Mike

June 3, 2011

Eddy Eddy
Area 51 Engineer
1295 posts

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.

 Signature 

Qt Certified Specialist
Qt Ambassador

June 3, 2011

mmkk15 mmkk15
Lab Rat
6 posts

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 ;-)

Greetings,
Mike

June 3, 2011

Eddy Eddy
Area 51 Engineer
1295 posts

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.

 Signature 

Qt Certified Specialist
Qt Ambassador

June 3, 2011

mmkk15 mmkk15
Lab Rat
6 posts

Thanks & done …

Greetings,
Mike

 
  ‹‹ QExtSerialPort readyRead Signal      Weird problem with setupUI call ››

You must log in to post a reply. Not a member yet? Register here!