首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > QT开发 >

,又是页面切换有关问题

2012-12-14 
高手请进,又是页面切换问题pageone.h#ifndef PAGEONE_H#define PAGEONE_H#include QWidget#includeQLab

高手请进,又是页面切换问题
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)


{  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;
}



我做了两个页面,但是最后在widget主窗口显示不出来,不知道是为什么?求高手解决。

[最优解释]
楼主代码的问题很严重。

你所有new出来的对象,都没有指定parent,而且没有释放,这将造成:

1. 对象不能显示在窗口上
2. 内存泄漏

建议楼主使用Qt Designer画出相同界面,然后编译,然后看它生成的ui_xxxx.h,看看人家是怎样实现的。
[其他解释]
有木有show????

热点排行