Qt設置窗口的初始大小(使用sizeHint這個虛函數,或者在構造函數里使用resize函數)


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM