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>
  • 相关阅读:
    CentOS7.4 + Ambari 2.6.1.5 + HDP 2.6.4.0 安装部署
    分布式业务的异常解决思路
    RPC簡介
    网络I/O模型--07Netty基础
    网络I/O模型--06异步I/O
    网络I/O模型--05多路复用I/O
    网络I/O模型--04非阻塞模式(解除accept()、 read()方法阻塞)的基础上加入多线程技术
    网络I/O模型--03非阻塞模式(ServerSocket与Socket的超时处理)--解除accept()、 read()方法阻塞
    网络I/O模型--02阻塞模式(多线程)
    Android开发(五)——计时器
  • 原文地址:https://www.cnblogs.com/shichuan/p/4497894.html
Copyright © 2011-2022 走看看