#ifndef CONFIG_H
#define CONFIG_H
#include <QWidget>
#include <QListWidget> //列表框类
#include <QListWidgetItem> //列表框项目
#include <QHBoxLayout> //水平布局
#include <QVBoxLayout> //垂直布局
#include <QPushButton> //按钮
#include <mypage.h> //在主窗口中定义三个窗口
#include <QStackedWidget> //容器窗口,用来存储三个窗口
class Config : public QWidget
{
Q_OBJECT
public:
Config(QWidget *parent = 0);
~Config();
QListWidget *list ; //定义全局变量列表框类指针
MyPage1 *page1 ;
MyPage2 *page2 ;
MyPage3 *page3 ;
QStackedWidget *InfoWidget ;
protected slots :
void pagechanged(int);
};
#endif // CONFIG_H
#ifndef MYPAGE
#define MYPAGE
#include <QWidget>
#include <QLabel> //标签, 用来显示提示语
#include <QLineEdit> //用来显示信息
#include <QGridLayout> // 网格布局
class MyPage1 : public QWidget
{
Q_OBJECT
public:
MyPage1(QWidget *parent = 0);
~MyPage1(){ }
QLabel *NameLabel ,*HeightLabel ,*WeightLabel ,*BWHLabel ;
QLineEdit *NameEdit ,*HeightEdit ,*WeightEdit ,*BWHEdit ;
};
class MyPage2 : public QWidget
{
Q_OBJECT
public:
MyPage2(QWidget *parent = 0);
~MyPage2(){ }
};
class MyPage3 : public QWidget
{
Q_OBJECT
public:
MyPage3(QWidget *parent = 0);
~MyPage3(){ }
};
#endif // MYPAGE
#include "config.h"
Config::Config(QWidget *parent)
: QWidget(parent)
{
list = new QListWidget ; //创建一个列表框
list->setIconSize(QSize(256,128));//设置图标尺寸
list->setSpacing(40); //设置项目间隔
list->setViewMode(QListView::IconMode); //设置项目显示方式,从左到右
list->setMaximumWidth(128); //设置列表框最大宽度,迫使项目换行
list->setMovement(QListView::Static); //设置项目为不可移动
this->resize(800,600); //重新设置窗口大小
/*创建三个列表框项目,并设置好图片*/
QListWidgetItem *item1 = new QListWidgetItem ;
item1->setIcon(QIcon(":/1.png"));
item1->setText(tr("苍井空"));
QListWidgetItem *item2 = new QListWidgetItem ;
item2->setIcon(QIcon(":/2.png"));
item2->setText(tr("泷泽萝拉"));
QListWidgetItem *item3 = new QListWidgetItem ;
item3->setIcon(QIcon(":/3.png"));
item3->setText(tr("小甲鱼"));
/*将创建好的三个项目添加进列表框*/
list->addItem(item1);
list->addItem(item2);
list->addItem(item3);
/*添加按钮*/
QPushButton *btn = new QPushButton ;
connect(btn , SIGNAL(clicked()), this , SLOT(close()));
btn->setText(tr("关闭"));
page1 = new MyPage1 ;
page2 = new MyPage2 ;
page3 = new MyPage3 ;
InfoWidget = new QStackedWidget ;
InfoWidget->addWidget(page1) ;
InfoWidget->addWidget(page2) ;
InfoWidget->addWidget(page3) ;
connect(list ,SIGNAL(currentRowChanged(int)) ,this ,SLOT(pagechanged(int)) );
/*布局*/
QHBoxLayout *HLayout = new QHBoxLayout ;
HLayout->addWidget(list);
HLayout->addWidget(InfoWidget);
QGridLayout *mainLayout = new QGridLayout(this);
mainLayout->addLayout(HLayout,0,0,1,6);
mainLayout->addWidget(btn,1,5,1,1);
}
Config::~Config()
{
}
void Config::pagechanged(int index)
{
InfoWidget->setCurrentIndex(index);
}
#include "config.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Config w;
w.show();
return a.exec();
}
#include "mypage.h"
MyPage1::MyPage1(QWidget *parent)
: QWidget(parent)
{
NameLabel = new QLabel(tr("姓名:"));
HeightLabel = new QLabel(tr("身高:"));
WeightLabel = new QLabel(tr("体重:"));
BWHLabel = new QLabel(tr("三围:"));
NameEdit = new QLineEdit(tr("苍井空"));
HeightEdit = new QLineEdit(tr("165cm"));
WeightEdit = new QLineEdit(tr("60kg"));
BWHEdit = new QLineEdit(tr("85,80,85"));
QGridLayout *GLayout = new QGridLayout(this);
GLayout->addWidget(NameLabel,0,0,1,2);
GLayout->addWidget(NameEdit,0,2,1,2);
GLayout->addWidget(HeightLabel,1,0,1,2);
GLayout->addWidget(HeightEdit,1,2,1,2);
GLayout->addWidget(WeightLabel,2,0,1,2);
GLayout->addWidget(WeightEdit,2,2,1,2);
this->setLayout(GLayout);
}
MyPage2::MyPage2(QWidget *parent)
: QWidget(parent)
{
}
MyPage3::MyPage3(QWidget *parent)
: QWidget(parent)
{
}