歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Qt調整初始窗口大小

Qt調整初始窗口大小

日期:2017/3/1 11:02:04   编辑:Linux編程

我們用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 ));

Copyright © Linux教程網 All Rights Reserved