zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    SpringBoot分布式篇Ⅷ --- 整合SpringCloud
    SpringBoot分布式篇Ⅶ --- 整合Dubbo
    java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
    小学数学题
    GoLang GRPC使用
    GoLang Socket 聊天实例
    golang Redis运用
    go mod 运用
    Golang Socket编程小实例
    GoLang协程和管道
  • 原文地址:https://www.cnblogs.com/findumars/p/7257501.html
Copyright © 2011-2022 走看看