16,175
社区成员
发帖
与我相关
我的任务
分享
#ifndef PAGEONE_H
#define PAGEONE_H
#include <QWidget>
#include<QLabel>
class PageOne : public QWidget
{
Q_OBJECT
public:
explicit PageOne(QWidget *parent = 0);
signals:
public slots:
private:
QLabel *label1;
};
#endif // PAGEONE_H
#ifndef PAGETWO_H
#define PAGETWO_H
#include <QWidget>
#include<QLabel>
class PageTwo : public QWidget
{
Q_OBJECT
public:
explicit PageTwo(QWidget *parent = 0);
signals:
public slots:
private:
QLabel *label2;
};
#endif // PAGETWO_H
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include<QKeyEvent>
#include<QStackedWidget>
class PageOne;
class PageTwo;
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
void keyPressEvent(QKeyEvent *event);
~Widget();
private:
Ui::Widget *ui;
PageOne *first;
PageTwo *second;
QStackedWidget *stack;
signals:
void del(int);
public slots:
void showPage(int i);
};
#endif // WIDGET_H
#include "pageone.h"
#include<QLabel>
PageOne::PageOne(QWidget *parent) :
QWidget(parent)
{ label1=new QLabel("the first page");
}
#include "pagetwo.h"
#include<QLabel>
PageTwo::PageTwo(QWidget *parent) :
QWidget(parent)
{ label2=new QLabel("the second page");
}
#include "widget.h"
#include "ui_widget.h"
#include"pageone.h"
#include"pagetwo.h"
#include<QStackedWidget>
#include<QHBoxLayout>
#include<QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{ first=new PageOne;
second=new PageTwo;
stack=new QStackedWidget;
stack->addWidget(first);
stack->addWidget(second);
QHBoxLayout *layout=new QHBoxLayout(this);
layout->addWidget(stack);
connect(this,SIGNAL(del(int)),this,SLOT(showPage(int)));
ui->setupUi(this);
}
void Widget::keyPressEvent(QKeyEvent *event)
{ if(event->key()==Qt::Key_1)
emit del(0);
if(event->key()==Qt::Key_2)
emit del(1);
}
Widget::~Widget()
{
delete ui;
}
void Widget::showPage(int i)
{ stack->setCurrentIndex(i);
qDebug()<<i;
}