QWidget *widget = new QWidget; widget->show(); qDebug() << widget->width() << "," << widget->height(); qDebug() << widget->sizeHint().width() << "," << widget->sizeHint().height(); //output: //1009 , 520 //-1 , –1
输出结果中第二行:sizeHint() 返回的是一个无效的 QSize,由于 widget 没有布局。函数
例2:
QWidget *widget = new QWidget; QHBoxLayout *layout = new QHBoxLayout; QPushButton *button = new QPushButton("Ggicci"); layout->addWidget(button); widget->setLayout(layout); widget->show(); qDebug() << widget->width() << "," << widget->height(); qDebug() << widget->sizeHint().width() << "," << widget->sizeHint().height(); 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 的尺寸的决定还有其它因素做用;