• Qt学习之路_7(线性布局和网格布局初步探索)


         QtDesigner这个工具用来设计界面比较方便,如果使用之,则比用纯代码写要容易入手,但是有的时候用代码来写界面可以达到灵活的设计,所以也需要掌握之。

      Qt中的布局有垂直布局,水平布局,这2种布局用法一模一样,在android中这种布局叫做叫做线性布局,我这里也暂这么叫着;另外1种为网格布局,即纵横交错;最后1种叫做form布局.下面简单介绍下用纯Qt C++语言进行垂直水平布局和网格布局。

      首先来看布局:

      效果如下:

      

      cpp代码和注释如下:

    #include <QApplication>
    
    #include <QPushButton>
    
    #include <QHBoxLayout>//水平布局的库
    
    #include <QVBoxLayout>//垂直布局的库
    
     
    
     
    
    int main( int argc, char *argv[])
    
    {
    
        QApplication app(argc, argv);
    
     
    
        QWidget *window = new QWidget;//创建主窗口
    
     
    
        //创建3个pushbutton按钮
    
        QPushButton *btn1 = new QPushButton("one");
    
        QPushButton *btn2 = new QPushButton("two");
    
        QPushButton *btn3 = new QPushButton("three");
    
     
    
        //创建一个垂直布局,并将三个pushbutton放入其中
    
        QHBoxLayout *hlayout = new QHBoxLayout;
    
        hlayout->addWidget(btn1);
    
        hlayout->addWidget(btn2);
    
        hlayout->addWidget(btn3);
    
     
    
        //将垂直布局放入主窗口中并显示
    
        window->setLayout(hlayout);
    
        window->show();
    
     
    
        return app.exec();//程序一直在这个地方等待,循环。
    
    }

        然后看看网格布局,效果如下:

      

           

        对应的cpp代码和注释如下:

      

    //网格布局
    
    #include <QApplication>
    
    #include <QtGui>
    
    #include <QtCore>
    
     
    
     
    
    int main( int argc, char *argv[])
    
    {
    
        QApplication app(argc, argv);
    
     
    
        QWidget *window = new QWidget;//创建主窗口
    
        window->setWindowTitle("Grid layout");//设置窗口名字
    
     
    
        //创建网格布局
    
        QGridLayout *grid_layout = new QGridLayout;
    
     
    
        //创建各种需要显示的控件
    
        QLabel *label_name = new QLabel("Name:");
    
        QLabel *label_id = new QLabel("Student ID:");
    
        QLineEdit *edit_name = new QLineEdit;
    
        QLineEdit *edit_id = new QLineEdit;
    
        QPushButton *button_ok = new QPushButton("ok");
    
     
    
        //将需要显示的控件都加入到网格布局中,注意其参数代表的含义
    
        grid_layout->addWidget(label_name, 0, 0);//表示放在第0行第0列的位置上
    
        grid_layout->addWidget(label_id, 1, 0);
    
        grid_layout->addWidget(edit_name, 0, 1);
    
        grid_layout->addWidget(edit_id, 1, 1);
    
        grid_layout->addWidget(button_ok, 2, 0, 1, 2);//最后2个参数表示其体积所占的行数和列数
    
     
    
        //将布局文件加载到窗口中并显示出来
    
        window->setLayout(grid_layout);
    
        window->show();
    
     
    
        return app.exec();//程序一直在这个地方等待,循环。
    
    }

        总结:Qt中的布局和android的布局类似,但是感觉用代码写起来Qt没有android方便,而用图像化操作的话Qt又要方便些,各有所长吧。

     

     

     

    作者:tornadomeet 出处:http://www.cnblogs.com/tornadomeet 欢迎转载或分享,但请务必声明文章出处。 (新浪微博:tornadomeet,欢迎交流!)
  • 相关阅读:
    第三次作业——《原型设计》
    第二次作业《熟悉使用工具》
    跟着《构建之法》学习软件工程(第一次作业)
    纯js代码实现手风琴特效
    HTML5
    为什么做前端要做好SEO
    让div盒子相对父盒子垂直居中的几种方法
    模板artTemplate
    bootstrap兼容问题
    移动常用的类库
  • 原文地址:https://www.cnblogs.com/tornadomeet/p/2615913.html
Copyright © 2020-2023  润新知