【Qt】UserInfo

這是一個靜態的關於用戶信息的界面,首先看一下效果:

 

 

接下來是看代碼:

<span style="font-family:Microsoft YaHei;font-size:18px;">//dialog.h
#include <QLabel>
#include<QLineEdit>
#include<QComboBox>
#include<QTextEdit>
#include<QGridLayout>

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

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 *SexComboBox;//組合框
    QTextEdit *DepartmentTextEdit;
    QLineEdit *AgeLineEdit;
    QGridLayout *LeftLayout;//網格佈局

    //右側
    QLabel *HeadLabel; //右上角部分
    QLabel *HeadIconLabel;
    QPushButton *UpdateHeadBtn;
    QHBoxLayout *TopRightLayout;//水平盒佈局

    QLabel *IntroductionLabel;
    QTextEdit *IntroductionTextEdit;
    QVBoxLayout *RightLayout;//垂直盒佈局

    //底部
    QPushButton *OkBtn;
    QPushButton *CancelBtn;
    QHBoxLayout *ButtomLayout;

};

#endif // DIALOG_H
</span>


dialog.cpp文件

<span style="font-family:Microsoft YaHei;font-size:18px;">#include<QLabel>
#include<QLineEdit>
#include<QComboBox>
#include<QPushButton>
#include<QFrame>
#include<QGridLayout>
#include<QPixmap>
#include<QHBoxLayout>

#include "dialog.h"

Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle(tr("UsrInfo"));

    //左側
    UserNameLabel =new QLabel(tr("用戶名: "));
    UserNameLineEdit =new QLineEdit;

    NameLabel =new QLabel(tr("姓名:"));
    NameLineEdit = new QLineEdit;

    SexLabel = new QLabel(tr("性別:"));
    SexComboBox =new QComboBox;
    SexComboBox->addItem(tr("女"));
    SexComboBox->addItem(tr("男"));

    DepartmentLabel =new QLabel(tr("部門:"));
    DepartmentTextEdit =new QTextEdit;

    AgeLabel =new QLabel(tr("年齡:"));
    AgeLineEdit =new QLineEdit;

    OtherLabel =new QLabel(tr("備註:"));
    OtherLabel ->setFrameStyle(QFrame::WinPanel|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(SexComboBox,2,1);

    LeftLayout->addWidget(DepartmentLabel,3,0);   //部門
    LeftLayout->addWidget(DepartmentTextEdit,3,1);

    LeftLayout->addWidget(AgeLabel,4,0);//年齡
    LeftLayout->addWidget(AgeLineEdit,4,1);

    LeftLayout->addWidget(OtherLabel,5,0,1,2); //其他,Wifdget(控件名,行,列,佔用行數,佔用列數)。

    LeftLayout->setColumnStretch(0,1);//設定兩列分別佔用空間的比例
    LeftLayout->setColumnStretch(1,3);

    //右側
    HeadLabel =new QLabel(tr("頭像:"));
    HeadIconLabel =new QLabel;
    QPixmap icon(":/new/111/312.png");

    HeadIconLabel->setPixmap(icon);
    HeadIconLabel->resize(icon.width(),icon.height());
    UpdateHeadBtn =new QPushButton(tr("更新"));

    TopRightLayout =new QHBoxLayout();//完成右上側頭像的選擇區的佈局
    TopRightLayout ->setSpacing(20);//設定各個控件之間的間距爲20
    TopRightLayout ->addWidget(HeadLabel);
    TopRightLayout ->addWidget(HeadIconLabel);
    TopRightLayout ->addWidget(UpdateHeadBtn);

    IntroductionLabel =new QLabel(tr("個人說明:"));
    IntroductionTextEdit =new QTextEdit;

    RightLayout =new QVBoxLayout();//完成右側佈局
    RightLayout ->setMargin(10);
    RightLayout ->addLayout(TopRightLayout);
    RightLayout ->addWidget(IntroductionLabel);
    RightLayout ->addWidget(IntroductionTextEdit);

    //底部
    OkBtn =new QPushButton(tr("確定"));
    CancelBtn =new QPushButton(tr("取消"));

    ButtomLayout =new QHBoxLayout();//完成下方兩個按鈕的佈局
    ButtomLayout->addStretch();//在按鈕之前插入一個佔位符,使兩個按鈕能夠靠右對齊,並且按鈕大小在調整時不變
    ButtomLayout->addWidget(OkBtn);
    ButtomLayout->addWidget(CancelBtn);

    QGridLayout *mainLayout =new QGridLayout(this);//實現主佈局,指定父窗口
    mainLayout->setMargin(15);//設定對話框的邊距爲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);//設定最優化顯示

    //QHBoxLayout默認採用的是自左向右的方式順序排列插入控件或子佈局,也可以通過調用setDirection()方法設定排列順序(eg:layout->setDirection(QBoxLayout::RightToLeft)),QVBoxLayout默認的是自上而下的方式順序插入控件或子佈局,也可調用setDirection()方法設定
}

Dialog::~Dialog()
{

}
</span>


這裏存在一個問題:

這裏如果的路徑並不是圖片存儲的絕對路徑,教程裏直接是“312.png",這樣是不行的,導致圖片無法加載出來

解決方法:

右鍵第一個圖標,選擇添加新文件,選擇如下:

這個名稱隨意填寫

選擇所要添加到的項目名稱:

 

在這裏首先添加前綴,前綴名可改,然後添加所要添加的文件即圖片,然後再出問題的那行代碼裏添加前綴名即可

 

結果是完美的!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章