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>
  • 相关阅读:
    [转]Maven 初学+http://mvnrepository.com/
    比较IDEA与Eclipse
    [web] 使用Promise封装fetch实现网络超时,终止请求的功能
    [web] 理解和使用Promise.all和Promise.race
    [Web] How to Test React and MobX with Jest
    [Web 测试] Jest单元测试的几个指标
    [Web] 取消Promise
    [安全分析] 安全分析中的威胁情报(一)
    [Web] 深入理解现代浏览器
    [Web] HTML5新特性history pushState/replaceState解决浏览器刷新缓存
  • 原文地址:https://www.cnblogs.com/shichuan/p/4497894.html
Copyright © 2011-2022 走看看