首页 > 代码库 > 【Qt5开发及实例】7、综合:修改用户资料

【Qt5开发及实例】7、综合:修改用户资料

给个目标实现以下:

技术分享技术分享技术分享


代码展示,

首先创建一个项目,不要界面,选择基类是QDialog这个,后面名字的话随便改改

我的类名是:Content。

baseinfo.h

#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>    //多选行
#include <QTextEdit>
#include <QGridLayout>    //网格布局
#include <QPushButton>

class BaseInfo : public QWidget
{
  Q_OBJECT

public:
  BaseInfo(QWidget *parent=0);
private:
  //左边的布局使用的控件
  QLabel *UserNameLabel;    //用户名的label
  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;   //垂直布局

};


contact.h

//添加的头文件
#include <QLabel>
#include <QGridLayout>
#include <QLineEdit>
#include <QCheckBox>
class Contact : public QWidget
{
    Q_OBJECT
public:
    Contact(QWidget *parent=0);
private:
    QLabel *EmailLabel;
    QLineEdit *EmailLineEdit;
    QLabel *AddrLabel;
    QLineEdit *AddrLineEdit;
    QLabel *CodeLabel;
    QLineEdit *CodeLineEdit;
    QLabel *MoviTelLabel;
    QLineEdit *MoviTelLineEdit;
    QCheckBox *MoviTelCheckBook;
    QLabel *ProTelLabel;
    QLineEdit *ProTelLineEdit;
    QGridLayout *mainLayout;
};


content.h

#ifndef CONTENT_H
#define CONTENT_H

#include <QStackedWidget>
#include <QPushButton>
#include "baseinfo.h"
#include "contact.h"
#include "detail.h"

class Content : public QFrame
{
  Q_OBJECT
public:
  Content(QWidget *parent = 0);
  QStackedWidget *stack;
  QPushButton *AmendBtn;
  QPushButton *CloseBtn;

  BaseInfo  *baseInfo;   //参考前面的章节
  Contact *contact;
  Detail *detail;


};

#endif // CONTENT_H

detail.h

#ifndef DETAIL_H
#define DETAIL_H

//添加的头文件
#include <QLabel>
#include <QComboBox>
#include <QLineEdit>
#include <QTextEdit>
#include <QGridLayout>
class Detail : public QWidget
{
    Q_OBJECT
public:
    Detail(QWidget *parent=0);
private:
    QLabel *NationalLabel;
    QComboBox *NationalComboBox;
    QLabel *ProvinceLabel;
    QComboBox *ProvinceComboBox;
    QLabel *CityLabel;
    QLineEdit *CityLineEdit;
    QLabel *IntroductLabel;
    QTextEdit *IntroductTextEdit;
    QGridLayout *mainLayout;
};



#endif // DETAIL_H

baseinfo.cpp

#include "baseinfo.h"

BaseInfo::BaseInfo(QWidget *parent) : QWidget(parent)
{
  /******左侧******/
  UserNameLabel = new QLabel(tr("用户名:"));
  UserNameLineEdit = new QLineEdit;

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

  SexLabel = new QLabel(tr("性别"));
  SexComboBox = new QComboBox;
  SexComboBox->addItem(tr("women"));
  SexComboBox->addItem(tr("man"));

  DepartmentLabel = new QLabel(tr("部门"));
  DepartmentTextEdit = new QTextEdit;     //文本输入的大框子

  AgeLabel = new QLabel(tr("部门"));
  AgeLineEdit = new QLineEdit;

  OtherLabel = new QLabel(tr("年龄"));
  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(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);
  LeftLayout->setColumnStretch(0, 1);   //就是一个弹簧
  LeftLayout->setColumnStretch(1, 3);

  /**右侧**/
  HeadLabel = new QLabel(tr("头像: "));
  HeadIconLabel = new QLabel;
  QPixmap icon("312.png");
  HeadIconLabel->setPixmap(icon);   //设置图片
  HeadIconLabel->resize(icon.width(), icon.height()); //设置图片的加进去的宽和高
  UpdateHeadBtn = new QPushButton(tr("更新"));

  TopRightLayout = new QHBoxLayout();
  TopRightLayout->setSpacing(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);

  /*******************************************/
  QGridLayout *mainLayout = new QGridLayout(this);
  mainLayout->setMargin(15);
  mainLayout->setSpacing(10);
  mainLayout->addLayout(LeftLayout, 0, 0);
  mainLayout->addLayout(RightLayout, 0, 1);
  mainLayout->setSizeConstraint(QLayout::SetFixedSize);


}

contact.cpp

#include "contact.h"

Contact::Contact(QWidget *parent) : QWidget(parent)
{
  EmailLabel = new QLabel(tr("电子邮件:"));
  EmailLineEdit = new QLineEdit;

  AddrLabel = new QLabel(tr("联系地址:"));
  AddrLineEdit = new QLineEdit;

  CodeLabel = new QLabel(tr("邮政编码:"));
  CodeLineEdit = new QLineEdit;


  MoviTelLabel =new QLabel(tr("移动电话: "));
  MoviTelLineEdit =new QLineEdit;
  MoviTelCheckBook =new QCheckBox(tr("接收留言"));

  ProTelLabel =new QLabel(tr("办公电话: "));
  ProTelLineEdit =new QLineEdit;

  mainLayout =new QGridLayout(this);
  mainLayout->setMargin(15);
  mainLayout->setSpacing(10);
  mainLayout->addWidget(EmailLabel,0,0);
  mainLayout->addWidget(EmailLineEdit,0,1);
  mainLayout->addWidget(AddrLabel,1,0);
  mainLayout->addWidget(AddrLineEdit,1,1);
  mainLayout->addWidget(CodeLabel,2,0);
  mainLayout->addWidget(CodeLineEdit,2,1);
  mainLayout->addWidget(MoviTelLabel,3,0);
  mainLayout->addWidget(MoviTelLineEdit,3,1);
  mainLayout->addWidget(MoviTelCheckBook,3,2);
  mainLayout->addWidget(ProTelLabel,4,0);
  mainLayout->addWidget(ProTelLineEdit,4,1);
  mainLayout->setSizeConstraint(QLayout::SetFixedSize);
}

Content.cpp

#include "content.h"

Content::Content(QWidget *parent):QFrame(parent)
{
  stack = new QStackedWidget(this);
  stack->setFrameStyle(QFrame::Panel|QFrame::Raised); //设置框架的样式,这个是QFrame::Raised - 框架和内容看起来凸起,这个是画布的形状

  baseInfo = new BaseInfo();
  contact = new Contact();
  detail = new Detail();

  stack->addWidget(baseInfo);
  stack->addWidget(contact);
  stack->addWidget(detail);   //把这三个内容加入进去

  AmendBtn = new QPushButton(tr("修改"));
  CloseBtn = new QPushButton(tr("关闭"));
  QHBoxLayout *BtnLayout = new QHBoxLayout;
  BtnLayout->addStretch(1);   //就是一段弹簧一样,把按钮挤到右边去
  BtnLayout->addWidget(AmendBtn);
  BtnLayout->addWidget(CloseBtn);   //加入按钮

  QVBoxLayout *RightLayout = new QVBoxLayout(this);
  RightLayout->setMargin(10);   //设置控件之间的间隔
  RightLayout->setSpacing(6);
  RightLayout->addWidget(stack);
  RightLayout->addLayout(BtnLayout);

}

detail.cpp

#include "detail.h"

Detail::Detail(QWidget *parent):QWidget(parent)
{
  NationalLabel = new QLabel(tr("国家/地址:"));
  NationalComboBox = new QComboBox;
  NationalComboBox->insertItem(0, tr("中国"));
  NationalComboBox->insertItem(1, tr("美国"));
  NationalComboBox->insertItem(2, tr("英国"));

  ProvinceLabel = new QLabel(tr("省份"));
  ProvinceComboBox = new QComboBox;
  ProvinceComboBox->insertItem(0, tr("江苏sheng"));
  ProvinceComboBox->insertItem(1, tr("山东sheng"));
  ProvinceComboBox->insertItem(2, tr("湖北sheng"));

  CityLabel = new QLabel(tr("城市"));
  CityLineEdit = new QLineEdit;

  IntroductLabel = new QLabel(tr("个人说明"));
  IntroductTextEdit = new QTextEdit;

  mainLayout = new QGridLayout(this);
  mainLayout->setMargin(15);
  mainLayout->setSpacing(10);
  mainLayout->addWidget(NationalLabel, 0, 0);
  mainLayout->addWidget(NationalComboBox, 0, 1);
  mainLayout->addWidget(ProvinceLabel, 1, 0);
  mainLayout->addWidget(ProvinceComboBox, 1, 1);
  mainLayout->addWidget(CityLabel, 2, 0);
  mainLayout->addWidget(CityLineEdit, 2, 1);
  mainLayout->addWidget(IntroductLabel, 3, 0);
  mainLayout->addWidget(IntroductTextEdit, 3, 1);
}

main.cpp

/**
 *      出生
 *       ||
 *       ||
 *      \  /
 *       \/
 *      青年
 * (年龄 = rand(20,25)))    《==============
 *       ||                                                                 ||
 *       ||                                                                 ||
 *       ||        祝福所有开发工作者                         ||
 *       ||            永远年轻                                       ||
 *       ||                                                                 ||
 *      \  /                                                                ||
 *       \/                                                                 ||
 *( 20 <= 年龄 <= 25)        ===============
 *       ||
 *       ||
 *      \  /
 *       \/
 *     等死状态
 */

#include "content.h"
#include <QApplication>
#include <QTextCodec>
#include <QSplitter>
#include <QListWidget>

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  QFont font("AR PL KaitiM GB", 12);
  a.setFont(font);

  QSplitter *splitterMain = new QSplitter(Qt::Horizontal, 0);
  splitterMain->setOpaqueResize(true);

  QListWidget *list = new QListWidget;
  list->insertItem(0, QObject::tr("基本信息"));
  list->insertItem(1, QObject::tr("联系方式"));
  list->insertItem(2, QObject::tr("详细资料"));

  Content *content = new Content(splitterMain);

  QObject::connect(list, SIGNAL(currentRowChanged(int)), content->stack, SLOT(setCurrentIndex(int)));

  splitterMain->addWidget(list);
  splitterMain->setWindowTitle(QObject::tr("修改用户资料"));
  splitterMain->setMinimumSize(splitterMain->minimumSize());
  splitterMain->setMaximumSize(splitterMain->maximumSize());
  splitterMain->show();

  return a.exec();
}

扛不住了,最近好多考试,这个玩意我得先放一放了,不然要是挂科了,那就GG思密达啦!!!技术分享
















【Qt5开发及实例】7、综合:修改用户资料