1. 程式人生 > >自學QT之實現一個使用者資訊視窗

自學QT之實現一個使用者資訊視窗

使用者資訊視窗如果用設計師介面來做當然是非常簡單了,但是為了鍛鍊自己的開發能力和對QT以及C++的深入理解,有必要用程式碼來進行操作。其實,程式碼比設計師介面更加靈活。看到程式碼多,不要害怕,其實程式碼中的大部分單詞只需要輸入開頭就可以自動補全的。

新建一個專案,基於對話方塊,取消介面選項。

標頭檔案如下:

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QTextEdit>
#include <QGridLayout>
#include <QPushButton>

class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);
    ~Dialog();
private:
    //左側部分的程式碼
    QLabel *UserNameLabel;
    QLabel *NameLabel;
    QLabel *SexLabel;
    QLabel *DepartmentLabel;
    QLabel *AgeLabel;
    QLabel *OtherLabel;
    QLineEdit *UserNameLineEdit;
    QLineEdit *NameLineEdit;
    QComboBox *SexComBox;
    QLineEdit *DepartmentEdit;
    QLineEdit *AgeEdit;
    QGridLayout *LeftLayout;
    //右側部分的程式碼
    QLabel *HeadLabel;
    QLabel *HeadIconLabel;
    QPushButton *UpdateHeadButn;
    QHBoxLayout *TopRightLayout;
    QLabel *IntroductionLabel;
    QTextEdit *IntroductionText;
    QVBoxLayout *RightLayout;

    //底部部分
    QPushButton *OKBtn;
    QPushButton *CancelBtn;
    QHBoxLayout *BtnLayout;




};

#endif // DIALOG_H
.cpp檔案如下:
#include "dialog.h"

Dialog::Dialog(QWidget *parent)

    : QDialog(parent)

{

    setWindowTitle("使用者資訊表");

    UserNameLabel=new QLabel("使用者名稱");

    UserNameLineEdit=new QLineEdit;

    NameLabel=new QLabel("姓名");

    NameLineEdit=new QLineEdit;

    SexLabel=new QLabel("性別");

    SexComBox=new QComboBox;

    SexComBox->addItem("男");

    SexComBox->addItem("女");

    DepartmentEdit=new QLineEdit;

    DepartmentLabel=new QLabel("部門選擇");

    AgeLabel=new QLabel("年齡");

    AgeEdit=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(DepartmentEdit,3,1);

    LeftLayout->addWidget(AgeLabel,4,0);

    LeftLayout->addWidget(AgeEdit,4,1);

    LeftLayout->setColumnStretch(0,1);

    LeftLayout->setColumnStretch(1,3);

    //接下來是右側的部分

    HeadLabel=new QLabel("頭像");

    HeadIconLabel =new QLabel;

    QPixmap icon("touxiang.png");

    HeadIconLabel->setPixmap(icon);

    HeadIconLabel->resize(icon.width(),icon.height());

    UpdateHeadButn=new QPushButton("更新");

    TopRightLayout=new QHBoxLayout();

    TopRightLayout->setSpacing(5);

    TopRightLayout->addWidget(HeadLabel);

    TopRightLayout->addWidget(HeadIconLabel);

    TopRightLayout->addWidget(UpdateHeadButn);

    IntroductionLabel=new QLabel("個人說明");

    IntroductionText=new QTextEdit;

    //右側的佈局

    RightLayout=new QVBoxLayout();

    RightLayout->setMargin(10);

    RightLayout->addLayout(TopRightLayout);

    RightLayout->addWidget(IntroductionLabel);

    RightLayout->addWidget(IntroductionText);

    //接下來是底部的佈局

    OKBtn=new QPushButton("確定");

    CancelBtn=new QPushButton("取消");

    BtnLayout=new QHBoxLayout();

    BtnLayout->addStretch();//增加一個佔位符,使得按鈕都顯示在右側

    BtnLayout->addWidget(OKBtn);

    BtnLayout->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(BtnLayout,1,0,1,2);

    mainLayout->setSizeConstraint(QLayout::SetFixedSize);//增加一個約束,控制元件都按照sizeHint()來顯示

}

Dialog::~Dialog()

{

}

執行後的結果: