16,219
社区成员
发帖
与我相关
我的任务
分享
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
pOne = new WidgetOne;
pTwo = new WidgetTwo;
ui->verticalLayout->addWidget(pOne);
QObject::connect(this,SIGNAL(sendToOneSignal()),
pOne,SLOT(receiveSlot()));
currentWidget = 1;
QObject::connect(ui->pushButton,SIGNAL(clicked()),
this,SLOT(pushBtnOneSlot()));
QObject::connect(ui->pushButton_2,SIGNAL(clicked()),
this,SLOT(pushBtnTwoSlot()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::pushBtnOneSlot()
{
if (currentWidget == 1)
return ;
else
{
ui->verticalLayout->removeWidget(this->pTwo);
this->pTwo->hide();
QObject::disconnect(this,SIGNAL(sendToTwoSignal()));
ui->verticalLayout->addWidget(this->pOne);
this->pOne->show();
QObject::connect(this,SIGNAL(sendToOneSignal()),
this->pOne,SLOT(receiveSlot()));
currentWidget = 1;
if (this->pOne->isVisible())
emit sendToOneSignal();
}
}
void MainWindow::pushBtnTwoSlot()
{
if (currentWidget == 2)
return ;
else
{
ui->verticalLayout->removeWidget(this->pOne);
this->pOne->hide();
QObject::disconnect(this,SIGNAL(sendToOneSignal()));
ui->verticalLayout->addWidget(this->pTwo);
this->pTwo->show();
QObject::connect(this,SIGNAL(sendToTwoSignal()),
this->pTwo,SLOT(receiveSlot()));
currentWidget = 2;
if (this->pTwo->isVisible())
emit sendToTwoSignal();
}