Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Moving to another Page, setCurrentIndex() and QStackedLayout
Forum Updated to NodeBB v4.3 + New Features

Moving to another Page, setCurrentIndex() and QStackedLayout

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    WarriorQt
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kaivolde
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        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.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • W Offline
          W Offline
          WarriorQt
          wrote on last edited by
          #4

          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 ?

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            well with what reason did you adapt it?

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • W Offline
              W Offline
              WarriorQt
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • W Offline
                W Offline
                WarriorQt
                wrote on last edited by
                #7

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

                }
                @

                1 Reply Last reply
                0
                • W Offline
                  W Offline
                  WarriorQt
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • W Offline
                    W Offline
                    WarriorQt
                    wrote on last edited by
                    #9

                    @#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
                    @

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved