Class problem
-
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 codefenprincipale.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..
-
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 becomeFenCene
orFenCenep
-
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 -
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?
ThanksRead 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.
-
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.. -
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..@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.