当前位置: 代码迷 >> QT开发 >> ,又是页面切换有关问题
  详细解决方案

,又是页面切换有关问题

热度:69   发布时间:2016-04-25 04:19:11.0
高手请进,又是页面切换问题
pageone.h
#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


pagetwo.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


widget.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


pageone.cpp
#include "pageone.h"
#include<QLabel>
PageOne::PageOne(QWidget *parent) :
    QWidget(parent)
{   label1=new QLabel("the first page");
}


pagetwo.cpp
#include "pagetwo.h"
#include<QLabel>
PageTwo::PageTwo(QWidget *parent) :
    QWidget(parent)
{  label2=new QLabel("the second page");
}


widget.cpp
#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)
  相关解决方案