我設計多個窗口,然後切換。問題現象是點擊按鈕後總是每次出現兩個相同窗口,而不是只有一個。窗口界面有QWidget類,也有Dialog類。代碼如下:
main.cpp
#include "widget_login.h"
//#include "mainmenu.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget_Login w;
w.move(200, 200);
w.show();
return a.exec();
}
登錄界面widget_login.h
#include <QWidget>
namespace Ui {
class Widget_Login;
}
class Widget_Login : public QWidget
{
Q_OBJECT
public:
explicit Widget_Login(QWidget *parent = 0);
~Widget_Login();
private slots:
void on_pushButton_clicked();
private:
Ui::Widget_Login *ui;
};
#endif // WIDGET_LOGIN_H
widget_login.cpp
#include "widget_login.h"
#include "ui_widget_login.h"
#include "dialog.h"
#include "mainmenu.h"
#include <iostream>
#include <fstream>
Widget_Login::Widget_Login(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget_Login)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));
}
Widget_Login::~Widget_Login()
{
delete ui;
}
void Widget_Login::on_pushButton_clicked()
{
Mainmenu *menu = new Mainmenu();
this->close();
menu->show();
}
主界面mainmenu.h
#ifndef MAINMENU_H
#define MAINMENU_H
#include <QWidget>
#include "oper_window.h"
namespace Ui {
class Mainmenu;
}
class Mainmenu : public QWidget
{
Q_OBJECT
public:
explicit Mainmenu(QWidget *parent = 0);
~Mainmenu();
private slots:
void on_pushButton_SET_clicked();
void on_pushButton_CHECK_clicked();
void on_pushButton_OPERATION_clicked();
void on_pushButton_REPLAY_clicked();
private:
Ui::Mainmenu *ui;
};
#endif // MAINMENU_H
mainmenu.cpp
#include "mainmenu.h"
#include "ui_mainmenu.h"
#include "widget_login.h"
Mainmenu::Mainmenu(QWidget *parent) :
QWidget(parent),
ui(new Ui::Mainmenu)
{
ui->setupUi(this);
connect(ui->pushButton_SET, SIGNAL(clicked()), this, SLOT(on_pushButton_SET_clicked()));
connect(ui->pushButton_CHECK, SIGNAL(clicked()), this, SLOT(on_pushButton_CHECK_clicked()));
connect(ui->pushButton_OPERATION, SIGNAL(clicked()), this, SLOT(on_pushButton_OPERATION_clicked()));
connect(ui->pushButton_REPLAY, SIGNAL(clicked()), this, SLOT(on_pushButton_REPLAY_clicked()));
}
Mainmenu::~Mainmenu()
{
delete ui;
}
void Mainmenu::on_pushButton_SET_clicked()
{
Widget_Login *wi = new Widget_Login;
this->close();
wi->show();
}
void Mainmenu::on_pushButton_CHECK_clicked()
{
}
void Mainmenu::on_pushButton_OPERATION_clicked()
{
}
void Mainmenu::on_pushButton_REPLAY_clicked()
{
}
參考QT實現多窗口切換
http://blog.csdn.net/wc7620awjh/article/details/8574131