我的问题是我找到了一种切换 UI 的方法。但是当它切换 UI 时,不会加载 UI 的 .cpp。
主菜单.cpp
#include "mainmenu.h"
#include "ui_mainmenu.h"
MainMenu::MainMenu(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainMenu),
newgame(new Ui::PlayerMenu),
optionmenu(new Ui::OptionMenu)
{
ui->setupUi(this);
QPixmap background("../../res/Testbg.png");
background = background.scaled(this->size(), Qt::IgnoreAspectRatio);
QPalette palette;
palette.setBrush(QPalette::Background, background);
this->setPalette(palette);
}
MainMenu::~MainMenu()
{
delete ui;
}
void MainMenu::on_pushButtonNewGame_clicked()
{
changeAppearance(1);
}
void MainMenu::on_pushButtonOption_clicked()
{
changeAppearance(2);
}
void MainMenu::changeAppearance(int id)
{
if(id == 0)
{
ui->setupUi(this);
}
else if(id == 1)
{
newgame->setupUi(this);
}
else if(id ==2)
optionmenu->setupUi(this);
}
主菜单.h
#ifndef MAINMENU_H
#define MAINMENU_H
#include <QMainWindow>
#include "playermenu.h"
#include "optionmenu.h"
namespace Ui {
class MainMenu;
}
class MainMenu : public QMainWindow
{
Q_OBJECT
public:
explicit MainMenu(QWidget *parent = 0);
~MainMenu();
private slots:
void on_pushButtonNewGame_clicked();
void on_pushButtonOption_clicked();
private:
void changeAppearance(int id);
Ui::MainMenu *ui;
Ui::PlayerMenu *newgame;
Ui::OptionMenu *optionmenu;
};
#endif // MAINMENU_H
播放器菜单.cpp
#include "playermenu.h"
#include "ui_playermenu.h"
PlayerMenu::PlayerMenu(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::PlayerMenu),
levelmenu(new Ui::LevelMenu)
{
ui->setupUi(this);
QPixmap background("../../res/Testbg.png");
background = background.scaled(this->size(),Qt::IgnoreAspectRatio);
QPalette palette;
palette.setBrush(QPalette::Background, background);
this->setPalette(palette);
}
...
播放器菜单.h
#ifndef PLAYERMENU_H
#define PLAYERMENU_H
#include <QMainWindow>
#include <ui_playermenu.h>
#include "levelmenu.h"
namespace Ui {
class PlayerMenu;
}
class PlayerMenu : public QMainWindow, Ui::PlayerMenu
{
Q_OBJECT
public:
explicit PlayerMenu(QWidget *parent = 0);
~PlayerMenu();
...
private:
Ui::PlayerMenu *ui;
Ui::LevelMenu *levelmenu;
};
#endif // PLAYERMENU_H
我是 QT 的新手,所以我真的不知道这是否是正确的方法。有没有人知道问题出在哪里或者是否有解决方法?