Moving to another Page, setCurrentIndex() and QStackedLayout



  • Hello everyone,
    I would like when you click on one of these buttons (button Next or Button Back), to go to another Page.
    See the following cod. Someone can give a clue. Thanks

    @#ifndef FINDDIALOG_H
    #define FINDDIALOG_H

    #include <QDialog>

    class QPushButton;
    class QLabel;
    class QStackedLayout;

    class FindDialog : public QDialog
    {
    Q_OBJECT

    public :
      FindDialog(QWidget *parent=0);
    
    private slots :
      void framenext        ();
      void frameBack        ();
    
    private :
      QLabel                *title1;
      QLabel                *title2;
      QLabel                *title3;
    
      QPushButton           *afficheButton1;
      QPushButton           *afficheButton2;
      QPushButton           *afficheButton3;
    
      QPushButton           *buttonNext;
      QPushButton           *buttonBack;
    
      QStackedLayout        *layout;
    

    };

    #endif // FINDDIALOG_@

    @#include <QtGui>

    #include "finddialog.h"

    FindDialog::FindDialog(QWidget *parent):QDialog(parent)
    {
    // Page 1
    QWidget *page1 = new QWidget;

    title1           =        new QLabel("FRAME_1");
    afficheButton1   =        new QPushButton("BOUTON_1");
    afficheButton1->setFixedSize(190,100);
    
    QVBoxLayout *Box1 = new QVBoxLayout;
    Box1->addWidget(title1);
    Box1->addWidget(afficheButton1);
    

    page1->setLayout(Box1);

    // Page 2
    QWidget *page2 = new QWidget;

    title2           =        new QLabel("FRAME_2");
    afficheButton2   =        new QPushButton("BOUTON_2");
    afficheButton2->setFixedSize(190,100);
    
    QVBoxLayout *Box2 = new QVBoxLayout;
    Box2->addWidget(title2);
    Box2->addWidget(afficheButton2);
    

    page2->setLayout(Box2);

    // Page 3
    QWidget *page3 = new QWidget;

    title3           =        new QLabel("FRAME_3");
    afficheButton3   =        new QPushButton("BOUTON_3");
    afficheButton2->setFixedSize(190,100);
    
    QVBoxLayout *Box3 = new QVBoxLayout;
    Box3->addWidget(title2);
    Box3->addWidget(afficheButton2);
    

    page3->setLayout(Box3);

    // Ajout de page 1, Page 2 et Page 3 dans un QStackedLayout
    layout = new QStackedLayout;
    layout->addWidget(page1);
    layout->addWidget(page2);
    layout->addWidget(page3);

    // Bouton permettant de passer d'une page vers une autre
    buttonBack = new QPushButton("Back");
    buttonNext = new QPushButton("Next");

    QHBoxLayout *downBox = new QHBoxLayout;
    downBox->addWidget(buttonBack);
    downBox->addWidget(buttonNext);

    //Ajout du QstackedLayout et des deux boutons (Next, Back) dans le
    //layout principal
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(layout);
    mainLayout->addLayout(downBox);

    //Definition du slot permettant de passer aux pages suivantes
    connect(buttonNext, SIGNAL(clicked()), this, SLOT(framenext()));

    setLayout(mainLayout);
    setWindowTitle("Boîte_Ly");
    setFixedHeight(sizeHint().height());
    }

    // Définion du slot personnalisé
    void FindDialog::framenext()
    {
    layout->setCurrentIndex(1);
    }

    void FindDialog::frameBack ()
    {
    layout->setCurrentIndex(0);
    }@



  • What does not work?
    Can you see yout initial layouted dialog?
    Only the buttons don't work?


  • Moderators

    what about this?
    @
    void FindDialog::framenext()
    {
    layout->setCurrentIndex( layout->currentIndex() + 1 );
    }

    void FindDialog::frameBack ()
    {
    layout->setCurrentIndex( layout->currentIndex() - 1 );
    }
    @
    setCurrentIndex() does nothing when you provide it with an invalid index, so this is the most basic implementation to achieve what you want.



  • Hello, Thank you for your help.

    I adapted your code like this :
    @void FindDialog::framenext()
    {
    layout->setCurrentIndex( layout->currentIndex() + 2 );
    }

    void FindDialog::frameBack ()
    {
    layout->setCurrentIndex( layout->currentIndex() - 2 );
    }@

    This code works with 2 pages, when I have three ou four pages , it doesn't work. Do you have some others ideas ?


  • Moderators

    well with what reason did you adapt it?



  • Because this code does not works with two pages
    @void FindDialog::framenext()
    {
    layout->setCurrentIndex( layout->currentIndex() + 1 );
    }

    void FindDialog::frameBack ()
    {
    layout->setCurrentIndex( layout->currentIndex() - 1 );
    }@



  • this is all my source code can you try this. thanks

    @#include <QApplication>

    #include "finddialog.h"

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    FindDialog *dialog = new FindDialog;
    dialog->show();
    
    return app.exec();
    

    }
    @



  • @#include <QtGui>

    #include "finddialog.h"

    FindDialog::FindDialog(QWidget *parent):QDialog(parent)
    {
    // Page 1
    QWidget *page1 = new QWidget;

    title1           =        new QLabel("FRAME_1");
    afficheButton1   =        new QPushButton("BOUTON_1");
    afficheButton1->setFixedSize(150,100);
    
    QVBoxLayout *Box1 = new QVBoxLayout;
    Box1->addWidget(title1);
    Box1->addWidget(afficheButton1);
    

    page1->setLayout(Box1);

    // Page 2
    QWidget *page2 = new QWidget;

    title2           =        new QLabel("FRAME_2");
    afficheButton2   =        new QPushButton("BOUTON_2");
    afficheButton2->setFixedSize(150,100);
    
    QVBoxLayout *Box2 = new QVBoxLayout;
    Box2->addWidget(title2);
    Box2->addWidget(afficheButton2);
    

    page2->setLayout(Box2);

    // Page 3
    QWidget *page3 = new QWidget;

    title3           =        new QLabel("FRAME_3");
    afficheButton3   =        new QPushButton("BOUTON_3");
    afficheButton2->setFixedSize(150,100);
    
    QVBoxLayout *Box3 = new QVBoxLayout;
    Box3->addWidget(title2);
    Box3->addWidget(afficheButton2);
    

    page3->setLayout(Box3);

    // Ajout de page 1, Page 2 et Page 3 dans un QStackedLayout
    layout = new QStackedLayout;
    layout->addWidget(page1);
    layout->addWidget(page2);
    layout->addWidget(page3);

    // Bouton permettant de passer d'une page vers une autre
    buttonBack = new QPushButton("Back");
    buttonNext = new QPushButton("Next");

    QHBoxLayout *downBox = new QHBoxLayout;
    downBox->addWidget(buttonBack);
    downBox->addWidget(buttonNext);

    //Ajout du QstackedLayout et des deux boutons (Next, Back) dans le
    //layout principal
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(layout);
    mainLayout->addLayout(downBox);

    //Definition du slot permettant de passer aux pages suivantes
    connect(buttonNext, SIGNAL(clicked()), this, SLOT(framenext()));
    connect(buttonBack, SIGNAL(clicked()), this, SLOT(frameBack()));

    //qDebug() << "Page: " << layout->currentIndex();

    setLayout(mainLayout);
    setWindowTitle("Boîte_Ly");
    setFixedHeight(sizeHint().height());
    }

    // Définion du slot personnalisé
    void FindDialog::framenext()
    {
    //layout->setCurrentIndex(1);
    layout->setCurrentIndex( layout->currentIndex() + 2);

    }

    void FindDialog::frameBack ()
    {
    //layout->setCurrentIndex(0);
    layout->setCurrentIndex( layout->currentIndex() - 2 );
    }
    @



  • @#ifndef FINDDIALOG_H
    #define FINDDIALOG_H

    #include <QDialog>

    class QPushButton;
    class QLabel;
    class QStackedLayout;

    class FindDialog : public QDialog
    {
    Q_OBJECT

    public :
      FindDialog(QWidget *parent=0);
    
    private slots :
      void framenext        ();
      void frameBack        ();
    
    private :
      QLabel                *title1;
      QLabel                *title2;
      QLabel                *title3;
    
      QPushButton           *afficheButton1;
      QPushButton           *afficheButton2;
      QPushButton           *afficheButton3;
    
      QPushButton           *buttonNext;
      QPushButton           *buttonBack;
    
      QStackedLayout        *layout;
    

    };

    #endif // FINDDIALOG_H
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.