Qt 학습-레이아웃 관리자 QLayout 클래스

4563 단어 Meego
요즘은 인터넷에서 공부 노트를 쓸 시간이 별로 없을 것 같아요.공책에 직접 손으로 쓰는 것이 좋겠다.여기에 코드와 캡처만 붙여서 자신의 학습 과정을 기록한다.
자주 사용하는 레이아웃 관리로는 QVBoxLayout, QHBoxLayout, QGridLayout이 있다.
다음은 통합 애플리케이션입니다.
새 Qt Gui 프로그램, 기본 클래스 Dialog
헤더 파일:
#ifndef DIALOG_H
#define DIALOG_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();

private:
    //     
    QLabel *UserLabel;
    QLabel *NameLabel;
    QLabel *SexLabel;
    QLabel *DepartLabel;
    QLabel *AgeLabel;
    QLabel *OtherLabel;
    QLineEdit *UserLineEdit;
    QLineEdit *NameLineEdit;
    QComboBox *SexComboBox;
    QTextEdit *DepartTextEdit;
    QLineEdit *AgeLineEdit;
    QGridLayout *LeftGridLayout;
    //   
    QLabel *HeadLabel;
    QLabel *HeadIconLabel;
    QPushButton *UpdateHeadBtn;
    QHBoxLayout *RightTopHBLayout;
    //   
    QLabel *IntroLabel;
    QTextEdit *IntroTextEdit;
    QVBoxLayout *RightVBLayout;
    //  
    QPushButton *OkBtn;
    QPushButton *CancelBtn;
    QHBoxLayout *ButtomHBLayout;
};

#endif // DIALOG_H

소스 파일:
#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    //××××××××××××   ×××××××××××××××
    setWindowTitle(tr("UserInfo"));
    UserLabel=new QLabel(tr("   :"));
    UserLineEdit=new QLineEdit;
    NameLabel=new QLabel(tr("  :"));
    NameLineEdit=new QLineEdit;
    SexLabel=new QLabel(tr("  :"));
    SexComboBox=new QComboBox;
    SexComboBox->addItem(tr(" "));
    SexComboBox->addItem(tr(" "));
    DepartLabel=new QLabel(tr("  "));
    DepartTextEdit=new QTextEdit;
    AgeLabel=new QLabel(tr("  "));
    AgeLineEdit=new QLineEdit;
    OtherLabel=new QLabel(tr("  "));
    OtherLabel->setFrameStyle(QFrame::Panel|QFrame::Sunken);
    //××××××××××××    ××××××××××××××
    LeftGridLayout=new QGridLayout();
    LeftGridLayout->addWidget(UserLabel,0,0);
    LeftGridLayout->addWidget(UserLineEdit,0,1);

    LeftGridLayout->addWidget(NameLabel,1,0);
    LeftGridLayout->addWidget(NameLineEdit,1,1);

    LeftGridLayout->addWidget(SexLabel,2,0);
    LeftGridLayout->addWidget(SexComboBox,2,1);

    LeftGridLayout->addWidget(DepartLabel,3,0);
    LeftGridLayout->addWidget(DepartTextEdit,3,1);

    LeftGridLayout->addWidget(AgeLabel,4,0);
    LeftGridLayout->addWidget(AgeLineEdit,4,1);

    LeftGridLayout->addWidget(OtherLabel,5,0,1,2);

    LeftGridLayout->setColumnStretch(0,1);
    LeftGridLayout->setColumnStretch(1,3);
    //×××××××××××××     ×××××××××××××××
    HeadLabel=new QLabel(tr("  :"));
    HeadIconLabel=new QLabel;
    QPixmap icon("1.bmp");
    HeadIconLabel->setPixmap(icon);
    HeadIconLabel->resize(icon.width(),icon.height());
    UpdateHeadBtn=new QPushButton(tr("  "));

    RightTopHBLayout=new QHBoxLayout;
    RightTopHBLayout->setSpacing(20);
    RightTopHBLayout->addWidget(HeadLabel);
    RightTopHBLayout->addWidget(HeadIconLabel);
    RightTopHBLayout->addWidget(UpdateHeadBtn);
    //×××××××××××××     ×××××××××××××××
    IntroLabel=new QLabel(tr("    :"));
    IntroTextEdit=new QTextEdit;

    RightVBLayout=new QVBoxLayout();
    RightVBLayout->setMargin(10);
    RightVBLayout->addLayout(RightTopHBLayout);
    RightVBLayout->addWidget(IntroLabel);
    RightVBLayout->addWidget(IntroTextEdit);
    //×××××××××××××    ×××××××××××××××
    OkBtn=new QPushButton(tr("  "));
    CancelBtn=new QPushButton(tr("  "));

    ButtomHBLayout=new QHBoxLayout();
    ButtomHBLayout->addStretch();
    ButtomHBLayout->addWidget(OkBtn);
    ButtomHBLayout->addWidget(CancelBtn);
    //×××××××××××××     ×××××××××××××××
    QGridLayout *mainLayout=new QGridLayout(this);
    mainLayout->setMargin(15);
    mainLayout->setSpacing(10);
    mainLayout->addLayout(LeftGridLayout,0,0);
    mainLayout->addLayout(RightVBLayout,0,1);
    mainLayout->addLayout(ButtomHBLayout,1,0,1,2);
    mainLayout->setSizeConstraint(QLayout::SetFixedSize);

    connect(OkBtn,SIGNAL(clicked()),this,SLOT(accept()));
    connect(CancelBtn,SIGNAL(clicked()),this,SLOT(reject()));
}

Dialog::~Dialog()
{

}

좋은 웹페이지 즐겨찾기