用到布局管理器将d出的d窗分成三个部分并往里面加入东西。
基本信息页面:
联系方式页面:
详细资料页面:
所使用的软件为 QT5.12.12,下载网站为Index of /archive/qt/5.12https://download.qt.io/archive/qt/5.12/新建Qt Widgets Application,自己取一个项目名称,基类选QDialog,类(class)命名为Content,取消创建界面的 复选框选中状态。
1.导航页的实现 "content.h"中的代码为
#ifndef CONTENT_H
#define CONTENT_H
#include
#include
#include
#include
#include "baseinfo.h"
#include "contact.h"
#include "detail.h"
class Content : public QFrame
{
Q_OBJECT
public:
Content(QWidget *parent = nullptr);
~Content();
QStackedWidget *stack;
QPushButton *AmendBtn;
QPushButton *CloseBtn;
BaseInfo *baseInfo;
Contact *contact;
Detail *detail;
};
#endif // CONTENT_H
Content.cpp中的代码为
#include "content.h"
#include
#include
Content::Content(QWidget *parent)
: QFrame(parent)
{
stack =new QStackedWidget(this);//创建一个QStackedWidget对象
stack->setFrameStyle(QFrame::Panel|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);
}
Content::~Content()
{
}
上面的代码是先创建一个有两个按钮的空页面d窗,而且将页面分割。然后需要为子页面里面添加内容。在项目的文件夹上面点击鼠标右键。选择Add New
然后d出的d窗选择“C++ Class”,点击“Choose”。d出的d窗在“Base class”下拉列表框中选择基类名为“QWidget”,在“Class name”的文本框中输入“BaseInfo”,(意思是修改用户基本信息的子页面),点击下一步,点击完成。同样的方法,再创建两个, *** 作方式其他都一样。只有一个不一样,就是类名(Class name)不一样,第二个为“Contact”,(意思是显示用户的联系方式的子页面),第三个为“Detail”,(意思是显示用户详细资料的子页面)。
打开baseinfo.h。添加下面的代码:
#ifndef BASEINFO_H
#define BASEINFO_H
#include
#include
#include
#include
#include
#include
#include
class BaseInfo : public QWidget
{
Q_OBJECT
public:
explicit BaseInfo(QWidget *parent = nullptr);
signals:
public slots:
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;
QTextEdit *IntroductionTextEdit;
QLabel *IntroductionLabel;
QVBoxLayout *RightLayout;
};
#endif // BASEINFO_H
打开baseinfo.cpp,添加如下代码:
#include "baseinfo.h"
BaseInfo::BaseInfo(QWidget *parent) : QWidget(parent)
{
/*左侧*/
UserNameLabel = new QLabel("用户名:");
UserNameLineEdit =new QLineEdit;
NameLabel =new QLabel("姓名:");
NameLineEdit =new QLineEdit;
SexLabel =new QLabel("性别:");
SexComboBox =new QComboBox;
SexComboBox->addItem("女");
SexComboBox->addItem("男");
DepartmentLabel =new QLabel("部门:");
DepartmentTextEdit =new QTextEdit;
AgeLabel =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(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("头像");//右上角部分
QSize picSize(150,100);
QPixmap icon("IMG_1751(20220430-120124).JPG");
QPixmap newIcon = icon.scaled(picSize,Qt::KeepAspectRatio,Qt::SmoothTransformation);
HeadIconLabel = new QLabel();
HeadIconLabel->setPixmap(newIcon);
/*HeadIconLabel =new QLabel;
QPixmap icon("312.png");
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("个人说明:");//右下角部分
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.h。添加如下代码:
#ifndef CONTACT_H
#define CONTACT_H
#include
#include
#include
#include
#include
class Contact : public QWidget
{
Q_OBJECT
public:
explicit Contact(QWidget *parent = nullptr);
signals:
public slots:
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;
};
#endif // CONTACT_H
打开contact.cpp ,添加如下代码:
#include "contact.h"
Contact::Contact(QWidget *parent) : QWidget(parent)
{
EmailLabel =new QLabel("电子邮件:");
EmailLineEdit =new QLineEdit;
AddrLabel =new QLabel("联系地址:");
AddrLineEdit =new QLineEdit;
CodeLabel =new QLabel("邮政编码:");
CodeLineEdit =new QLineEdit;
MoviTelLabel =new QLabel("移动电话:");
MoviTelLineEdit =new QLineEdit;
MoviTelCheckBook =new QCheckBox("接收留言:");
ProTelLabel =new QLabel("办公电话:");
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);
}
打开detail.h,添加:
#ifndef DETAIL_H
#define DETAIL_H
#include
#include
#include
#include
#include
#include
class Detail : public QWidget
{
Q_OBJECT
public:
explicit Detail(QWidget *parent = nullptr);
signals:
public slots:
private:
QLabel *NationalLabel;
QComboBox *NationalComboBox;
QLabel *ProvinceLabel;
QComboBox *ProvinceComboBox;
QLabel *CityLabel;
QLineEdit *CityLineEdit;
QLabel *IntroductLabel;
QTextEdit *IntroductTextEdit;
QGridLayout *mainLayout;
};
#endif // DETAIL_H
打开detail.cpp,添加:
#include "detail.h"
Detail::Detail(QWidget *parent) : QWidget(parent)
{
NationalLabel =new QLabel("国家/地址:");
NationalComboBox =new QComboBox;
NationalComboBox->insertItem(0,("中国"));
NationalComboBox->insertItem(1,("美国"));
NationalComboBox->insertItem(2,("英国"));
ProvinceLabel =new QLabel("省份:");
ProvinceComboBox = new QComboBox;
ProvinceComboBox->insertItem(0,("江苏省"));
ProvinceComboBox->insertItem(1,("山东省"));
ProvinceComboBox->insertItem(2,("浙江省"));
CityLabel =new QLabel("城市:");
CityLineEdit = new QLineEdit;
IntroductLabel = new QLabel("个人说明:");
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文件,添加:
#include "content.h"
#include
#include
#include
#include
#include
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(splitterMain); //
list->insertItem(0,QObject::tr("基本信息"));
list->insertItem(1,QObject::tr("联系方式"));
list->insertItem(2,QObject::tr("详细资料"));
Content *content =new Content(splitterMain);//插入Content对象
QObject::connect(list,SIGNAL(currentRowChanged(int)),content->stack,SLOT(setCurrentIndex(int)));
//设置主布局框即水平分割窗口的标题
splitterMain->setWindowTitle(QObject::tr("修改用户资料"));
//设置主布局框即水平分割窗口的最小尺寸
splitterMain->setMinimumSize(splitterMain->minimumSize());
//设置主布局框即水平分割窗口的最大尺寸
splitterMain->setMaximumSize(splitterMain->maximumSize());
splitterMain->show(); //显示主布局框,其上面的控件一同显示
//Content w;
//w.show();
return a.exec();
}
这些文件都在同一个项目下面
本文是基于 第4版QT5 开发及实例 为知识基础。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)