My problem is that i found a way to switch UIs. But when it switch the UIs the .cpp of the UI will not load.
mainmenu.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);
}
mainmenu.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
playermenu.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);
}
...
playermenu.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
I'm new to QT so i don't really know if this is the right way to do this. Does anyone have a clue where the Problem is or if there is a work around?
It sounds like you want to have a single window which switches between different states. I wouldn't recommend using multiple .ui files to do this.. a couple of better ways might be:
Use a QStackedWidget - you can add this in the UI designer, think of it like a set of pages which you select programatically. Use that and have your buttons change it to the appropriate page.
Have multiple different classes for your different views, and set the central widget of the main window to different widgets when needed.
Personally, I would go for option 1.