高手请进,又是页面切换问题

trustguan 2012-11-06 10:47:01
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主窗口显示不出来,不知道是为什么?求高手解决。
...全文
157 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiachm 2012-11-06
  • 打赏
  • 举报
回复
楼主代码的问题很严重。 你所有new出来的对象,都没有指定parent,而且没有释放,这将造成: 1. 对象不能显示在窗口上 2. 内存泄漏 建议楼主使用Qt Designer画出相同界面,然后编译,然后看它生成的ui_xxxx.h,看看人家是怎样实现的。
SilenceSu 2012-11-06
  • 打赏
  • 举报
回复
有木有show????

16,202

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧