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. Class problem

Class problem

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 705 Views
  • 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.
  • Nafab213N Offline
    Nafab213N Offline
    Nafab213
    wrote on last edited by
    #1

    Hello Everyone
    I am a beginner in programming.
    I have a main class (fenprincipale.h, fenprincipale.cpp, fenprincipale.ui) and two classrooms class 1 (fencene.h, fencene.cpp, fencene.ui) / class 2 (fencenep.h, fencenep.cpp, fencenep.ui).
    I have two buttons (bttoncene, bttoncenep) on the main window. I would like the fencene.ui to be displayed as a Central widget when we click on the button "bttoncene" and that the fencenep.ui is displayed as Widget Central when we click on the button "bttoncenep".
    I do not know how to do. I mixed everything in the code.
    That's the code

    fenprincipale.h

    #ifndef FENPRINCIPALE_H
    #define FENPRINCIPALE_H
    
    #include <QMainWindow>
    #include <QWidget>
    #include <QStackedWidget>
    #include "fencene.h"
    #include "fencenep.h"
    
    
    namespace Ui {
    class FenPrincipale;
    }
    
    class FenPrincipale : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit FenPrincipale(QWidget *parent = 0);
        ~FenPrincipale();
    
    private:
        Ui::FenPrincipale *ui;
        QWidget *fencen;
        QStackedWidget *centralfencen;
    
    
    public slots:
        void Affichefencene();
        void Affichefencenep();
    
    };
    
    #endif // FENPRINCIPALE_H
    
    

    fenprincipale.cpp

    #include "fenprincipale.h"
    #include "ui_fenprincipale.h"
    #include "fencene.h"
    #include "fencenep.h"
    
    FenPrincipale::FenPrincipale(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::FenPrincipale)
    {
        ui->setupUi(this);
        fencen = new QWidget;
        setCentralWidget(fencen);
    
    
        QStackedWidget *centralfencen = new QStackedWidget;
        stackedWidget->addWidget(fencen);
        stackedWidget->addWidget(FenCene());
        stackedWidget->addWidget(FenCenep());
    
        connect(ui->bttoncene, &QPushButton::clicked, this, &FenPrincipale::Affichefencene);
        connect(ui->bttoncenep,  &QPushButton::clicked, this, &FenPrincipale::Affichefencenep);
    }
    
    FenPrincipale::~FenPrincipale()
    {
        delete ui;
    }
    
    void FenPrincipale::Affichefencene()
    {
        centralfencen->setCentralWidget(FenCene());
    }
    
    void FenPrincipale::Affichefencenep()
    {
       centralfencen->setCentralWidget(FenCenep());
    }
    
    

    fencene.h

    #ifndef FENCENE_H
    #define FENCENE_H
    
    #include <QObject>
    #include <QWidget>
    
    namespace Ui {
    class FenCene;
    }
    
    class FenCene : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit FenCene(QWidget *parent = nullptr);
        ~FenCene();
    
    private:
        Ui::FenCene *ui;
    };
    
    #endif // FENCENE_H
    
    

    fencene.cpp

    #include "fencene.h"
    #include "ui_fencene.h"
    
    FenCene::FenCene(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::FenCene)
    {
        ui->setupUi(this);
    }
    
    FenCene::~FenCene()
    {
        delete ui;
    }
    
    

    fencenep.h

    #ifndef FENCENEP_H
    #define FENCENEP_H
    
    #include <QWidget>
    
    
    namespace Ui {
    class FenCenep;
    }
    
    class FenCenep : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit FenCenep(QWidget *parent = nullptr);
        ~FenCenep();
    
    private:
        Ui::FenCenep *ui;
    };
    
    #endif // FENCENEP_H
    
    

    fencenep.cpp

    #include "fencenep.h"
    #include "ui_fencenep.h"
    
    FenCenep::FenCenep(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::FenCenep)
    {
        ui->setupUi(this);
    }
    
    FenCenep::~FenCenep()
    {
        delete ui;
    }
    
    

    Thanks..

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      Remove fencen completely


      change

      fencen = new QWidget;
          setCentralWidget(fencen);
      
      
          QStackedWidget *centralfencen = new QStackedWidget;
          stackedWidget->addWidget(fencen);
          stackedWidget->addWidget(FenCene());
          stackedWidget->addWidget(FenCenep());
      

      to

      centralfencen = new QStackedWidget;
      stackedWidget->addWidget(new FenCene);
      stackedWidget->addWidget(new FenCenep);
      setCentralWidget(centralfencen);
      

      void FenPrincipale::Affichefencene()
      {
          centralfencen->setCentralWidget(FenCene());
      }
      
      void FenPrincipale::Affichefencenep()
      {
         centralfencen->setCentralWidget(FenCenep());
      }
      

      should be

      void FenPrincipale::Affichefencene()
      {
          centralfencen->setCurrentIndex(0);
      }
      
      void FenPrincipale::Affichefencenep()
      {
         centralfencen->setCurrentIndex(1);
      }
      

      P.S.
      All the above can be done directly in designer, you can add widgets to your designer file (fenprincipale.ui) then right click them and select "Promote to" to change them to become FenCene or FenCenep

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      5
      • Nafab213N Offline
        Nafab213N Offline
        Nafab213
        wrote on last edited by
        #3

        I did exactly what is said and my IDE presents this notification to me
        " stackedWidget was not declared in scope"

        Code.h

        #ifndef FENPRINCIPALE_H
        #define FENPRINCIPALE_H
        
        #include <QMainWindow>
        #include <QWidget>
        #include <QStackedWidget>
        #include "fencene.h"
        #include "fencenep.h"
        
        
        namespace Ui {
        class FenPrincipale;
        }
        
        class FenPrincipale : public QMainWindow
        {
            Q_OBJECT
        
        public:
            explicit FenPrincipale(QWidget *parent = 0);
            ~FenPrincipale();
        
        private:
            Ui::FenPrincipale *ui;
            QStackedWidget *centralfencen;
        
        
        public slots:
            void Affichefencene();
            void Affichefencenep();
        
        };
        
        #endif // FENPRINCIPALE_H
        
        

        code.cpp

        #include "ui_fenprincipale.h"
        #include "fencene.h"
        #include "fencenep.h"
        #include <QStackedWidget>
        
        FenPrincipale::FenPrincipale(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::FenPrincipale)
        {
            ui->setupUi(this);
        
            centralfencen = new QStackedWidget;
            stackedWidget->addWidget(new FenCene);
            stackedWidget->addWidget(new FenCenep);
            setCentralWidget(centralfencen);
        
            connect(ui->bttoncene, &QPushButton::clicked, this, &FenPrincipale::Affichefencene);
            connect(ui->bttoncenep,  &QPushButton::clicked, this, &FenPrincipale::Affichefencenep);
        }
        
        FenPrincipale::~FenPrincipale()
        {
            delete ui;
        }
        
        void FenPrincipale::Affichefencene()
        {
            centralfencen->setCurrentIndex(0);
        }
        
        void FenPrincipale::Affichefencenep()
        {
           centralfencen->setCurrentIndex(1);
        }
        
        

        What solution can I use?
        Thanks

        kshegunovK 1 Reply Last reply
        0
        • Nafab213N Nafab213

          I did exactly what is said and my IDE presents this notification to me
          " stackedWidget was not declared in scope"

          Code.h

          #ifndef FENPRINCIPALE_H
          #define FENPRINCIPALE_H
          
          #include <QMainWindow>
          #include <QWidget>
          #include <QStackedWidget>
          #include "fencene.h"
          #include "fencenep.h"
          
          
          namespace Ui {
          class FenPrincipale;
          }
          
          class FenPrincipale : public QMainWindow
          {
              Q_OBJECT
          
          public:
              explicit FenPrincipale(QWidget *parent = 0);
              ~FenPrincipale();
          
          private:
              Ui::FenPrincipale *ui;
              QStackedWidget *centralfencen;
          
          
          public slots:
              void Affichefencene();
              void Affichefencenep();
          
          };
          
          #endif // FENPRINCIPALE_H
          
          

          code.cpp

          #include "ui_fenprincipale.h"
          #include "fencene.h"
          #include "fencenep.h"
          #include <QStackedWidget>
          
          FenPrincipale::FenPrincipale(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::FenPrincipale)
          {
              ui->setupUi(this);
          
              centralfencen = new QStackedWidget;
              stackedWidget->addWidget(new FenCene);
              stackedWidget->addWidget(new FenCenep);
              setCentralWidget(centralfencen);
          
              connect(ui->bttoncene, &QPushButton::clicked, this, &FenPrincipale::Affichefencene);
              connect(ui->bttoncenep,  &QPushButton::clicked, this, &FenPrincipale::Affichefencenep);
          }
          
          FenPrincipale::~FenPrincipale()
          {
              delete ui;
          }
          
          void FenPrincipale::Affichefencene()
          {
              centralfencen->setCurrentIndex(0);
          }
          
          void FenPrincipale::Affichefencenep()
          {
             centralfencen->setCurrentIndex(1);
          }
          
          

          What solution can I use?
          Thanks

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          Read this very carefully, if you don't spot the error reread it ... and repeat until you see what the problem is.

          centralfencen = new QStackedWidget;
          stackedWidget->addWidget(new FenCene);
          stackedWidget->addWidget(new FenCenep);
          

          The compiler tells you exactly what is wrong and where.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          4
          • Nafab213N Offline
            Nafab213N Offline
            Nafab213
            wrote on last edited by
            #5

            I read and reread but I see only one detail but when I regulate the detail. I am always in the same situation.

            That's the detail I added

            QStackedWidget *centralfencen = new QStackedWidget;
                stackedWidget->addWidget(new FenCene);
                stackedWidget->addWidget(new FenCenep);
            

            Precision I start in programming and I want to learn so help me a little and this mistake will be learned forever ..
            Please..

            kshegunovK 1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              What you did is wrong, that's shadowing the member variable. it should be:

              centralfencen = new QStackedWidget;
                  centralfencen ->addWidget(new FenCene);
                  centralfencen ->addWidget(new FenCenep);
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              4
              • Nafab213N Nafab213

                I read and reread but I see only one detail but when I regulate the detail. I am always in the same situation.

                That's the detail I added

                QStackedWidget *centralfencen = new QStackedWidget;
                    stackedWidget->addWidget(new FenCene);
                    stackedWidget->addWidget(new FenCenep);
                

                Precision I start in programming and I want to learn so help me a little and this mistake will be learned forever ..
                Please..

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

                @Nafab213 said in Class problem:

                I want to learn so help me a little

                That's why I only pointed you to the relevant part but wanted you to find the actual mistake by yourself. You're using different names for the variable.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                3

                • Login

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