Blackbelt
Blackbelt

Reputation: 157487

QPushButton Geometry

Inside a QWidget I put a QHBoxLayoutwith, two QPushButtons and a QLabel. I can change the geometry of the QWidget that works as container for the layout but can not change the size of the QPushButton: here my code

void
QTitleBar::resizeEvent( QResizeEvent * event)
{
 QSize size = event->size();
 int widgetHeight = size.rheight();
 int widgetWidth = size.rwidth();

 std::cout << "resizeEvent h:" <<
  widgetHeight  
  << " w: " << widgetWidth << std::endl;

 int layoutH = (int)((float)widgetHeight*(float)0.20);


 std::cout << "resizeEvent h:" <<
  layoutH  
  << " w: " << widgetWidth << std::endl;

 mapButton->setGeometry(0, 0, 120, layoutH);
 titleWidget->setGeometry(0, 0,  widgetWidth, layoutH);

// title->setGeometry(130, 0, widgetWidth - (2*130), layoutH);
// closeButton->setGeometry(widgetWidth - (2*130), 0,  130, layoutH);

 //closeButton->setGeometry(0, 0,  widgetWidth, layoutH);

}

is there something wrong?

Upvotes: 0

Views: 5815

Answers (2)

maxik
maxik

Reputation: 1123

[Some advice for further readers...]

As you noticed using the button's geometry property is not the right way, because it is controlled by the containing QLayout. See here for Qt layout handling or writing an own implementation of QLayout.

Also note that setting the geometry of a QWidget may result in an infinite loop!

Setting the properties height and width of a QWidget directly works if and only if the QWidget is not contained in any QLayout. Otherwise it will be resized inside the dimension of mimimumHeight and maximumHeight which applies to width as pointed out. (Note that the size property only bundles these two.) You may influence this behaviour by modifying the QWidgets sizePolicy property.

So stick to ScarCode's answer using

QWidget.setMinimumSize(QSize)
QWidget.setMaximumSize(QSize)

As for completness I like to point out that the size of QPushButton may also be changed using Qt style sheets:

QPushButton {
    min-width: 0px;
    max-width: 10000px;
}

Upvotes: 1

ScarCode
ScarCode

Reputation: 3094

   mapButton->setMinimumSize(QSize(0,0));
   mapButton->setMaximumSize(QSize(10000,10000));

Now you can set your QPushButton to any size (practically). I hope this would solve the problem, as titlewidget is getting resized and relocated as in code.

Upvotes: 1

Related Questions