3.1 Qt布局管理

1. QSplitter 分割窗口

 

 

 

 

代码:

QSplitter *splitterMain=new QSplitter(Qt::Horizontal,0);
QTextEdit *textLeft=new QTextEdit(QObject::tr("Left Widget"),splitterMain);
textLeft->setAlignment(Qt::AlignCenter);//文字对齐方式
QSplitter *splitterRight=new QSplitter(Qt::Vertical,splitterMain);
splitterRight->setOpaqueResize(false);//分隔条在拖拽时是否实时更新显示
QTextEdit *textUp=new QTextEdit(QObject::tr("Top Widget"),splitterRight);
textUp->setAlignment(Qt::AlignCenter);
QTextEdit *textBottom=new QTextEdit(QObject::tr("Bottom Widget"),splitterRight);
textBottom->setAlignment(Qt::AlignCenter);
//设定可伸缩空间,第1个参数用于指定设置的控件序号,从0开始编号
//第2个参数为大于0的值,表示此控件为可伸缩控件
splitterMain->setStretchFactor(1,1);
splitterMain->setWindowTitle(QObject::tr("Splitter"));
splitterMain->show();

 

2. 停靠窗口QDockWidget类

 

 

 

 代码:

setWindowTitle("DockWindows");
QTextEdit *te=new QTextEdit(this);
te->setText("Main Windows");
te->setAlignment(Qt::AlignCenter);//居中
setCentralWidget(te);//将此编辑框设为主窗口的中央窗体
QDockWidget *dock=new QDockWidget("DockWindow1",this);
dock->setFeatures(QDockWidget::DockWidgetMovable);//可移动
dock->setAllowedAreas(Qt::LeftDockWidgetArea|Qt::RightDockWidgetArea);//窗体可停靠区域的位置
QTextEdit *te1=new QTextEdit();
te1->setText("Window1,The dock widget can be moved between docks by the user");
dock->setWidget(te1);
addDockWidget(Qt::RightDockWidgetArea,dock);

dock=new QDockWidget("DockWindow2",this);
dock->setFeatures(QDockWidget::DockWidgetClosable|QDockWidget::DockWidgetFloatable);//可关闭,可浮动
QTextEdit *te2=new QTextEdit();
te2->setText(tr("Window2, The dock can be detached from the mainwindow,""and floated as an independent window, and can be closed"));
dock->setWidget(te2);
addDockWidget(Qt::RightDockWidgetArea,dock);

dock=new QDockWidget("DockWindow3",this);
dock->setFeatures(QDockWidget::AllDockWidgetFeatures);//全部特性
QTextEdit *te3=new QTextEdit();
te3->setText(tr("Window3, The dock can be closed, moved, and floated"));
dock->setWidget(te3);
addDockWidget(Qt::RightDockWidgetArea,dock);

 

3. 堆栈窗体QStackedWidget

 

 

代码:

setWindowTitle("StackedWidget");
list=new QListWidget(this);
list->insertItem(0,"Window1");
list->insertItem(1,"Window2");
list->insertItem(2,"Window3");
label1=new QLabel("WindowTest1");
label2=new QLabel("WindowTest2");
label3=new QLabel("WindowTest3");
stack=new QStackedWidget(this);
stack->addWidget(label1);
stack->addWidget(label2);
stack->addWidget(label3);
QHBoxLayout *mainLayout=new QHBoxLayout(this);

mainLayout->setMargin(5);
mainLayout->setSpacing(5);
mainLayout->addWidget(list);
mainLayout->addWidget(stack,0,Qt::AlignCenter);
mainLayout->setStretchFactor(list,1);
mainLayout->setStretchFactor(stack,3);
connect(list,SIGNAL(currentRowChanged(int)),stack,SLOT(setCurrentIndex(int)));

 

4.基本布局(QLayout)

 

代码:

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QLabel>
#include <QSplitter>
#include <QLineEdit>
#include <QComboBox>
#include <QTextEdit>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QPixmap>

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();
    void setDialog();
private:
    //左侧
    QLabel *userNameLabel;
    QLabel *nameLabel;
    QLabel *sexLabel;
    QLabel *departmentLabel;
    QLabel *ageLable;
    QLabel *otherLabel;
    QLineEdit *usernameLineEdit;
    QLineEdit *nameLineEdit;
    QComboBox *sexCombox;
    QTextEdit *departmentTextEdit;
    QLineEdit *ageLineEdit;
    QGridLayout *leftLayout;
    //右侧
    QLabel *headLabel;
    QLabel *headIconLabel;
    QPushButton *updateHeadBtn;
    QHBoxLayout *topRightLayout;
    QLabel *introductionLabel;
    QTextEdit *introductionLineEdit;
    QVBoxLayout *rightLayout;
    //底部
    QPushButton *okBtn;
    QPushButton *cancelBtn;
    QHBoxLayout *buttomLayout;
};

#endif // DIALOG_H

dialog.cpp

#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    setDialog();
}

Dialog::~Dialog()
{

}
void Dialog::setDialog(){
    userNameLabel=new QLabel("用户名:");
    usernameLineEdit=new QLineEdit;
    nameLabel=new QLabel("姓名:");
    nameLineEdit=new QLineEdit;
    sexLabel=new QLabel("性别:");
    sexCombox=new QComboBox;
    sexCombox->addItem("");
    sexCombox->addItem("");
    departmentLabel=new QLabel("部门:");
    departmentTextEdit=new QTextEdit;
    ageLable=new QLabel("年龄:");
    ageLineEdit=new QLineEdit;
    otherLabel=new QLabel("备注:");
    otherLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);//控件风格
    leftLayout=new QGridLayout();
    leftLayout->addWidget(userNameLabel,0,0);
    leftLayout->addWidget(usernameLineEdit,0,1);
    leftLayout->addWidget(nameLabel,1,0);
    leftLayout->addWidget(nameLineEdit,1,1);
    leftLayout->addWidget(sexLabel,2,0);
    leftLayout->addWidget(sexCombox,2,1);
    leftLayout->addWidget(departmentLabel,3,0);
    leftLayout->addWidget(departmentTextEdit,3,1);
    leftLayout->addWidget(ageLable,4,0);
    leftLayout->addWidget(ageLineEdit,4,1);
    leftLayout->addWidget(otherLabel,5,0,1,2);
    //设定两列分别占用的空间比例
    leftLayout->setColumnStretch(0,1);
    leftLayout->setColumnStretch(1,3);
    //右侧
    headLabel=new QLabel("头像:");
    headIconLabel=new QLabel;
    QPixmap icon(":/image/123.jpg");
    headIconLabel->setPixmap(icon);
    headIconLabel->resize(icon.width(),icon.height());
    updateHeadBtn=new QPushButton("更新");

    topRightLayout=new QHBoxLayout();
    topRightLayout->setSpacing(20);
    topRightLayout->addWidget(headLabel);
    topRightLayout->addWidget(headIconLabel);
    topRightLayout->addWidget(updateHeadBtn);
    introductionLabel=new QLabel("个人说明");
    introductionLineEdit=new QTextEdit;
    rightLayout=new QVBoxLayout();
    rightLayout->setMargin(10);
    rightLayout->addLayout(topRightLayout);
    rightLayout->addWidget(introductionLabel);
    rightLayout->addWidget(introductionLineEdit);
    okBtn=new QPushButton("确定");
    cancelBtn=new QPushButton("取消");
    buttomLayout=new QHBoxLayout();
    buttomLayout->addStretch();//在按钮之前插入一个占位符,使两个按钮能够靠右对齐
    buttomLayout->addWidget(okBtn);
    buttomLayout->addWidget(cancelBtn);
    QGridLayout *mainLayout=new QGridLayout(this);
    mainLayout->setMargin(15);
    mainLayout->setSpacing(10);
    mainLayout->addLayout(leftLayout,0,0);
    mainLayout->addLayout(rightLayout,0,1);
    mainLayout->addLayout(buttomLayout,1,0,1,2);
    mainLayout->setSizeConstraint(QLayout::SetFixedSize);//设定最优化显示,并且使用户无法改变对话框大小

}

 

 

 

posted @ 2020-02-13 11:33  啸傲风月  阅读(235)  评论(0编辑  收藏  举报