I tried to use QStackedWidget
before, but I didn't understand exactly how to. The code below makes me understand how to change the current window from the main window to another window, already called in the mainwindow, and this is working good. I changed the current index to all the other windows, and every time the window is not the same, which is good.
My question is:
From another window how can I switch to another window (different than the current)? Do I Have to define this QStackedWidget
in all the other windows, so that I can use it the same way as I am using it here?
I would love that after clicking on a button on a window(the other windows) the window switch to another one, How can I do it?
For example, in this code I have the FenetrePrincipale that allow me to change the windows using the setCurrentIndex
, setting the setCurrentIndex
to 3 for example make the first window that appear is MAFENETRE3.
I would like that from for example, from MAFENTRE3 use a button that allow me to switch to another window .
( actually after having problems with QStackedWidget
I just implement my code normally and instead of switching to another window, I just open window on the bottom of the window calling which is not looking good!
PS HERE THE CODE OF TEST :
fenetrprincipale.h
#ifndef FENETRE_PRINCIPALE
#define FENETRE_PRINCIPALE
#include <QApplication>
#include <QtWidgets>
#include "MaFenetre.h"
#include "MaFenetre2.h"
#include "MaFenetre3.h"
#include "MaFenetre4.h"
class FenetrePrincipale : public QMainWindow
{
Q_OBJECT
public:
FenetrePrincipale();
~FenetrePrincipale();
public slots:
void slotDisplayFen(int fenIndex);
private:
QStackedWidget *stack;
MaFenetre *fen1;
MaFenetre2 *fen2;
MaFenetre3 *fen3;
MaFenetre4 *fen4;
};
#endif
fenetreprincipale.cpp
#include "FenetrePrincipale.h"
FenetrePrincipale::FenetrePrincipale() : QMainWindow()
{
stack = new QStackedWidget(this);
fen1 = new MaFenetre();
fen2 = new MaFenetre2 ();
fen3 = new MaFenetre3();
fen4 = new MaFenetre4();
stack->addWidget(fen1);
stack->addWidget(fen2);
stack->addWidget(fen3);
stack->addWidget(fen4);
this->setCentralWidget(stack);
stack->setCurrentIndex(0); // on affiche la première fenêtre à l'ouverture du programme
setWindowTitle("Test STACKEDLAYOUT");
resize(500,600);
connect(fen1, SIGNAL(askDisplayFen(int)), this, SLOT(slotDisplayFen(int)));
connect(fen2, SIGNAL(askDisplayFen(int)), this, SLOT(slotDisplayFen(int)));
connect(fen3, SIGNAL(askDisplayFen(int)), this, SLOT(slotDisplayFen(int)));
connect(fen4, SIGNAL(askDisplayFen(int)), this, SLOT(slotDisplayFen(int)));
}
FenetrePrincipale::~FenetrePrincipale()
{
}
void FenetrePrincipale::slotDisplayFen(int fenIndex)
{
if ((fenIndex < 0) || (fenIndex > 3)) {return;}
stack->setCurrentIndex(fenIndex);
}
Here is the code of Mafenetre MaFenetre.h
#ifndef DEF_MAFENETRE
#define DEF_MAFENETRE
#include <QtWidgets>
class MaFenetre : public QWidget // On hérite de QWidget (IMPORTANT)
{
public:
MaFenetre();
private:
QPushButton *m_bouton;
};
#endif
MaFenetre.cpp
#include "MaFenetre.h"
MaFenetre::MaFenetre() : QWidget()
{
setFixedSize(300, 150);
m_bouton = new QPushButton("Quitter", this);
m_bouton->setFont(QFont("Comic Sans MS", 14));
m_bouton->move(110, 50);
// Connexion du clic du bouton à la fermeture de l'application
QObject::connect(m_bouton, SIGNAL(clicked()), qApp, SLOT(quit()));
}