用 QStackedWidget,怎么实现窗口切换?
这里有两个界面,一个是显示go 的按钮 ,一个是显示back 的按钮,,我想按go 按钮 就进入到 back 这个按钮界面,然后在按back 就返回到go 这个按钮,因为按钮也是继承QWidget的,所以也可以作为一个widget。
现在的问题,就是按go 按钮没反应啊,
我现在把代码贴出来了,我怎么也找不出哪里出错了,但 按按钮又没反应,纠结啊
//=====================================
#include<QWidget>
class QPushButton;
class QStackedWidget;
class MeunBt : public QWidget
{
Q_OBJECT
public:
MeunBt(QWidget *parent = 0);
QPushButton *go;
QPushButton *back;
QStackedWidget *stack;
public slots:
void change1();
void change2();
};
//==========================================
#include<QtGui>
#include "meun.h"
MeunBt::MeunBt(QWidget *parent)
: QWidget(parent)
{
setWindowTitle(tr("STACKED"));
go = new QPushButton(tr("go"));
back = new QPushButton(tr("back"));
QHBoxLayout *mainLayout = new QHBoxLayout;
stack = newQStackedWidget(this);
stack->addWidget(go);
stack->addWidget(back);
mainLayout->addWidget(stack);
setLayout(mainLayout);
connect(go,SIGNAL(clicked()),stack,SLOT(change1()));
connect(back,SIGNAL(clicked()),stack,SLOT(change2()));
}
void MeunBt::change1()
{
stack->setCurrentIndex(1);
}
void MeunBt::change2()
{
stack->setCurrentIndex(0);
}
[解决办法]
因为我也是QT新手,斗胆改了两下代码,不知道是否正确。
connect(go,SIGNAL(clicked()),this,SLOT(change1()));
connect(back,SIGNAL(clicked()),this,SLOT(change2()));
connect(go,SIGNAL(clicked()),this,SLOT(change1()));
connect(back,SIGNAL(clicked()),this,SLOT(change2()));