zoukankan      html  css  js  c++  java
  • 使用布局

    通常,子Widget是通过使用布局对象在窗口中进行排列的,而不是通过指定位置和大小进行排列的。在此,构造一个并排排列的标签和行编辑框Widget:

    QLabel *label = new QLabel(tr("Name:"));

    QLineEdit *lineEdit = new QLineEdit();

    QHBoxLayout *layout = new QHBoxLayout();

    layout->addWidget(label);

    layout->addWidget(lineEdit);

    window->setLayout(layout);

    由于Widget可包含其他Widget,所以布局可用来提供按不同层次分组的Widget。这里,要在显示查询结果的表视图上方、窗口顶部的行编辑框旁,显示一个标签:
    QLabel *queryLabel = new QLabel(tr("Query:"));
    QLineEdit *queryEdit = new QLineEdit();
    QTableView *resultView = new QTableView();
     
    QHBoxLayout *queryLayout = new QHBoxLayout();
    queryLayout->addWidget(queryLabel);
    queryLayout->addWidget(queryEdit);
     
    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->addLayout(queryLayout);
    mainLayout->addWidget(resultView);
    window->setLayout(mainLayout);
    Qt提供了QHBoxLayout类、QVBoxLayout类及QGridLayout类等的基本布局管理,分别是水平排列布局、垂直排列布局和网格排列布局。它们之间的继承关系如图:
     
     
    addWidget()方法用于向布局中加入需要布局的控件,addWidget()的函数原型如下:
    void addWidget
    (
        QWidget *widget,                   //需要插入的控件对象
        int  fromRow,                          //插入的行
        int  fromColumn,                   //插入的列
        int  rowSpan,                          //表示占用的行数
        int  columnSpan,                   //表示占用的列数
        Qt::Alignment  alignment=0          //描述各个控件的对齐方式
    )
    addLayout ()方法用于向布局中加入需要布局的子布局,addLayout ()的函数原型如下:
    void addLayout
    (
        QLayout *layout,                      //表示需要插入的子布局对象
        int row,                                  //插入的起始行
        int column,                          //插入的起始列
        int rowSpan,                          //表示占用的行数
        int columnSpan,                       //表示占用的列数
        Qt::Alignment alignment=0              //指定对齐方式
    )
    新建Qt  应用,基类选择“QDialog”,取消“创建界面”复选框的选中状态。
    打开“dialog.h”头文件,在头文件中声明对话框中的各个控件。添加代码所示。
    添加如下的头文件:

    #include <QLabel>

    #include <QLineEdit>

    #include <QComboBox>

    #include <QTextEdit>

    #include <QGridLayout>

    “dialog.h”头文件,在类中声明对话框中的各个控件。添加代码所示。
    private:
        QLabel *UserNameLabel; QLineEdit *UserNameLineEdit;
        QLabel *NameLabel; QLineEdit *NameLineEdit;
        QLabel *SexLabel; QComboBox *SexComboBox;
        QLabel *DepartmentLabel; QTextEdit *DepartmentTextEdit;
        QLabel *AgeLabel; QLineEdit *AgeLineEdit;
        QLabel *OtherLabel; QGridLayout *LeftLayout;
    
        /*********右侧*********/
        QLabel *HeadLabel; QLabel *HeadIconLabel;
        QPushButton *UpdateHeadBtn; QHBoxLayout *TopRightLayout;
        QLabel *IntroductionLabel; QTextEdit *IntroductionTextEdit;
        QVBoxLayout *RightLayout;
    
        /*--------------------- 底部 --------------------*/
        QPushButton *OkBtn; QPushButton *CancelBtn;
        QHBoxLayout *ButtomLayout;
    “dialog.cpp”文件,在类Dialog的构造函数中添加代码。
    在“dialog.cpp”文件的开始部分加入以下头文件:
    #include<QLabel>
    #include<QLineEdit>
    #include<QComboBox>
    #include<QPushButton>
    #include<QFrame>
    #include<QGridLayout>
    #include<QPixmap>
    #include<QHBoxLayout>
  • 相关阅读:
    转: 分布式系统编程,你到哪一级了?
    window屏幕朝向的调整 Alt + Ctrl + 上下左右箭头
    win10的安装与下载
    Zookeeper的学习材料
    配置文件的格式选型
    转: YAML 语言教程 from(阮一峰)
    Eclipse的 JSON Edit插件
    转: 如何为你的开源项目选择一个合适的开源协议?
    在Eclipse中使用SVN插件subclipse的教程
    我们在呼唤上帝还是在召唤恶魔——警惕人工智能
  • 原文地址:https://www.cnblogs.com/shichuan/p/4497894.html
Copyright © 2011-2022 走看看