简体   繁体   中英

QStackedWidget how to use it exactly?

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()));
}

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);
}

答案只是在要切换的所需窗口上定义一个自定义信号,该信号将发送到主窗口,以便为您显示正确的开关。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM