1. 程式人生 > >Qt設置窗口的初始大小(使用sizeHint這個虛函數)

Qt設置窗口的初始大小(使用sizeHint這個虛函數)

int() you turn lds 有一個 ase 沒有 widget 重載

我們用qt創建一個窗口,先後顯示它,代碼如下:

class Mywindow : public QMainWindow
{

.....

}


int main( int argc, char** argv )

{

QApplication app( argc, argv );

Mywindow wind;

wind.show();

return app.exec();
}

發現窗口很小,查看它的方法,以及他的父類widget的方法,發現有個方法像是設置其初始大小的,setBaseSize,調用這個方法

setBaseSize( 800, 600 );

運行程序,發現一點效果都沒有。

註意我這裏並沒有使用setFixedSize setMaximumSize,因為雖然這些方法能夠設置初始大小,但是之後就不能用鼠標調整窗口大小了。

後來baidu發現有人用重載

QSize sizeHint() const

的方式來實現。這個函數是QWidget的一個虛函數。


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.

virtual QSize sizeHint () const

QSize Mywindow::sizeHint() const
{
return QSize( 800, 600 );
}

這樣就可以設置窗口的大小偽800x600了。

後來發現還有一個方法就是 resize。在構造函數中直接調用他設置大小就可以。如:

this->resize( QSize( 800, 600 ));

原文鏈接:http://blog.csdn.net/zb872676223/article/details/23190017

Qt設置窗口的初始大小(使用sizeHint這個虛函數)