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

  • 相关阅读:
    linux卸载rpm包
    Centos6.3手动rpm安装gcc,c++
    阿里云服务器挂载分区
    linux下svn目录管理
    mac搭建cordova的android环境
    mac下搭建cordova开发环境
    程序员除了写代码还可以做点啥
    php之soap使用
    linux中找不到/etc/sysconfig/iptables
    const 位置不一样导致的变化
  • 原文地址:https://www.cnblogs.com/findumars/p/7257501.html
Copyright © 2011-2022 走看看