布局参数

 

之前讲过的布局参数:

size hint 推荐大小

  •   sizeHint 
  • [ From Qt Doc : This property holds the recommended size for the widget. If the value of this property is an invalid size, no size is recommended. The default implementation of sizeHint() returns an invalid size if there is no layout for this widget, and returns the layout's preferred size otherwise. ] 这个属性所保存的 QSize 类型的值是一个被推荐给窗口或其它组件(为了方便下面统称为widget)的尺寸,也就是说一个 widget 该有多大,它的一个参考来源就是这个 sizeHint 属性的值,而这个值由 sizeHint() 函数来确定。但是 widget 的大小的确定还有其它因素作用,下面会讲到。现在只需知道 sizeHint() 会返回一个被推荐的尺寸。那么这个尺寸的取值是怎样的呢?当它是一个无效值的时候(sizeHint().isValid() 返回 false,QSize 中 width 或者 height 有一个为复数就会是无效的),什么作用也没有;当它是一个有效值的时候,它就成了 widget 大小的一个参考。Qt 中对 sizeHint() 的默认实现是这样的:当 widget 没有布局(layout),返回无效值;否则返回其 layout 的首选尺寸( preferred size )。
  •  1: QWidget *widget = new QWidget;
       2: widget->show();
       3: qDebug() << widget->width() << "," << widget->height();
       4: qDebug() << widget->sizeHint().width() << "," << widget->sizeHint().height();
       1: //output:
       2: //1009 , 520
       3: //-1 , –1
    输出结果中第二行:sizeHint() 返回的是一个无效的 QSize,因为 widget 没有布局。
       1: QWidget *widget = new QWidget;
       2: QHBoxLayout *layout = new QHBoxLayout;
       3: QPushButton *button = new QPushButton("Ggicci");
       4: layout->addWidget(button);
       5: widget->setLayout(layout);
       6: widget->show();
       7: qDebug() << widget->width() << "," << widget->height();
       8: qDebug() << widget->sizeHint().width() << "," << widget->sizeHint().height();
       9: qDebug() << button->width() << "," << button->height();
     
       1: //output:
       2: //112 , 45 
       3: //97 , 45 
       4: //90 , 23
    输出结果中第一行:widget 的实际尺寸 (112, 45);
    输出结果中第二行:sizeHint() 返回 layout 的首选尺寸(97,45)供 widget 参考;
    输出结果中第三行:中间 button 的实际大小;
    从输出结果中可以证明以上说过的两点:
    1) 在 widget 有 layout 的情况下,其 sizeHint() 函数返回的是有效值作为其自身实际尺寸的参考;
    2) sizeHint() 返回的值并不一定会作为 widget 的实际尺寸,因为 widget 的尺寸的决定还有其它因素作用;

size policy 策略

新增:

stretch factor 拉伸因子

maximum size /minimum size 上下限

http://www.tuicool.com/articles/uyaYn2

 

posted @ 2016-04-06 20:22  wuye  阅读(157)  评论(0)    收藏  举报