简体   繁体   English

QStackedWidget具体怎么用呢?

[英]QStackedWidget how to use it exactly?

I tried to use QStackedWidget before, but I didn't understand exactly how to.我之前尝试过使用QStackedWidget ,但我不明白如何使用。 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?我是否必须在所有其他窗口中定义这个QStackedWidget ,以便我可以像在这里使用它一样使用它? 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.例如,在这个代码中,我有FenetrePrincipale允许我使用来改变窗口setCurrentIndex ,设定setCurrentIndex例如使出现是MAFENETRE3第一窗口3。

I would like that from for example, from MAFENTRE3 use a button that allow me to switch to another window .我想从例如,从 MAFENTRE3 使用一个按钮,允许我切换到另一个窗口。 ( 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! (实际上在QStackedWidget出现问题后,我只是正常实现我的代码,而不是切换到另一个窗口,我只是在窗口调用的底部打开窗口,这看起来不太好!

PS HERE THE CODE OF TEST : PS 这里的测试代码:

fenetrprincipale.h fenetprincipale.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 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这是 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 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);
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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