0

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()));
}
Grabinuo
  • 334
  • 2
  • 11
MadHer
  • 91
  • 3
  • 11
  • thanks I edited it , hope its clear now – MadHer Jun 11 '18 at 05:50
  • how to use it the other windows, or call it , this is what I want to do ? – MadHer Jun 11 '18 at 05:55
  • so this mean , that I have to , each time , create pointers on the windows that I choose to switch to them , and implement the same slot on those windows ? , actually this code I just copied it from internet , and switch between the window , is not clear for me , I juste change the setCurrentIndex each time , and see which window will appear first , but the signals emitted I do not see how they work – MadHer Jun 11 '18 at 06:02
  • actually I kind of understand what you said , but this mean that I have to define another time in each window slotDisplayFen ? , if so its clear , but this is a little confusing , because after searching on the net , I just found that this window serve to all the other windows , and allow them to switch ( maybe I did not got them well ) – MadHer Jun 11 '18 at 06:08
  • about the slotDisplayFen no its not clear , I do not see when they ll be activated – MadHer Jun 11 '18 at 06:11
  • posible duplicate of [How to use QStackedWidget in GUI?](https://stackoverflow.com/questions/45727089/how-to-use-qstackedwidget-in-gui) – eyllanesc Jun 11 '18 at 06:19
  • where have you defined the `askDisplayFen` signal? – eyllanesc Jun 11 '18 at 06:21
  • I saw before many examples on the web , but no one of them show how to switch to another window from the others windows , maybe I m not right , but this what I ve found , they just explain that we must define a mainwindow like mine , but the other process is not clear after , I guess that the correct question right now is how to use this askdiplayFen in the other windows , and how the slotDisplayFen will be called ? thanks for all your answers by the way – MadHer Jun 11 '18 at 06:23
  • According to your explanation, I understood that the code that works works but you do not like it but I see that this code does not work, I recommend you read the examples of Qt, you are wanting to learn new things without having established the basics, goodbye. – eyllanesc Jun 11 '18 at 06:26
  • about the askDisplayFen this a good question its not defined here , guess this the thing , it must be defined on other windows , but still struggling what will be it definition – MadHer Jun 11 '18 at 06:26
  • I do not know, do not ask me about that, it's your code, not mine. review the example of where you got it. – eyllanesc Jun 11 '18 at 06:27
  • https://openclassrooms.com/forum/sujet/qt-changer-le-contenu-d-une-fenetre – MadHer Jun 11 '18 at 06:28
  • ask in the forum, tell the author to show more code, I recommend you check the examples provided by Qt, there are hundreds. Do not use incomplete examples because as you are a beginner you will get confused, look for complete codes. – eyllanesc Jun 11 '18 at 06:30
  • sir I know that you have no exact ideas about my code , I appreciate how you helped me , but I just said that you helped me to figure out , that there is no definition of the signal askDisplayFen and this is 70% the answer of my question so thank you again ! – MadHer Jun 11 '18 at 06:32

2 Answers2

1

I have shared with a below sample code i hope it would be help for you.

#include "test1.h"
#include "ui_test1.h"
#include<QDebug>

test1::test1(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::test1)
{
 ui->setupUi(this);
 stack = new QStackedWidget(this);
 tes = new test2();
 stack->addWidget(ui->pushButton);
 stack->addWidget(tes);
 this->setCentralWidget(stack);
 stack->setCurrentIndex(0);
 connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(slotDisplayFen()));

}

 test1::~test1()
 {
  delete ui;
 }

void test1::slotDisplayFen()
{
  qDebug()<<"test";
  stack->setCurrentIndex(1);
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Subrata Das
  • 135
  • 1
  • 12
0

The answer is just to define a custom signal on the desired window to switch , and that signal will be sent to the main window so it will display the right switch for you

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
MadHer
  • 91
  • 3
  • 11