Unsolved Centralwidget is not displayed
-
Hello everyone..
I thank in passing the various people who work on this site to help and advance knowledge.
I start programming and I have a problem.
I created a centralwidget with three buttons (bttoncene, bttoncenep, quit) in the file fenprincipale.cpp.
When I compile "The window is displayed but the buttons are not displayed." I have the impression that my centralwidget create in the fenprincipale.cpp did not take into account in the execution of the graphic window "The fenprincipale.h
#ifndef FENPRINCIPALE_H #define FENPRINCIPALE_H #include <QMainWindow> #include <QWidget> #include <QStackedWidget> #include <QPushButton> #include <QGridLayout> #include <QFont> #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; QPushButton *bttoncene; QPushButton *bttoncenep; QPushButton *quitter; QGridLayout *fenprincipal; public slots: void Affichefencene(); void Affichefencenep(); }; #endif // FENPRINCIPALE_H
The fenprincipale.cpp
#include "fenprincipale.h" #include "ui_fenprincipale.h" #include "fencene.h" #include "fencenep.h" #include <QStackedWidget> #include <QPushButton> #include <QGridLayout> #include <QFont> FenPrincipale::FenPrincipale(QWidget *parent) : QMainWindow(parent), ui(new Ui::FenPrincipale) { ui->setupUi(this); centralfencen = new QStackedWidget; centralfencen ->addWidget(new FenCene); centralfencen ->addWidget(new FenCenep); setCentralWidget(centralfencen); bttoncene = new QPushButton("Cahier Electronique de Note de L'Enseignant (CEN_E)"); //Finir le boulot bttoncene->setFont(QFont("Times New Roman", 20)); bttoncene->setCursor(Qt::PointingHandCursor); bttoncenep = new QPushButton("Cahier Electronique de Note de L'Enseignant Principal (CEN_EP)"); bttoncenep->setFont(QFont("Times New Roman", 20)); bttoncenep->setCursor(Qt::PointingHandCursor); quitter = new QPushButton("Quitter"); quitter->setFont(QFont("Times New Roman", 20)); quitter->setCursor(Qt::PointingHandCursor); fenprincipal = new QGridLayout; fenprincipal->addWidget(bttoncene, 0, 0); fenprincipal->addWidget(bttoncenep, 0, 2); fenprincipal->addWidget(quitter, 1, 0, 1, 2); centralfencen->setLayout(fenprincipal); connect(bttoncene, &QPushButton::clicked, this, &FenPrincipale::Affichefencene); connect(bttoncenep, &QPushButton::clicked, this, &FenPrincipale::Affichefencenep); } FenPrincipale::~FenPrincipale() { delete ui; } void FenPrincipale::Affichefencene() { centralfencen->setCurrentIndex(0); } void FenPrincipale::Affichefencenep() { centralfencen->setCurrentIndex(1); }
Please .. What is the problem?
Help me understand what's wrong.
Thanks in advance.. -
Hello.
If you delete the centralWidgets default layout before setting your own, it should work.
(I have rebuilt your program and added this line):delete centralfencen->layout(); // NEW: Get and clear default layout centralfencen->setLayout(fenprincipal); // Sets your GridLayout as CentralWidget-Layout
I dont know, if it is a good solution, but it works
BTW: Dont forget to clean up your memory in destructor (You have to delete every object you create with "new" -> your PushButton instances, your GridLayout and your "centralfencen")
-
@Pl45m4 I added the line of code to my fenprincipale.cpp but when I compile. My IDE says that "the program suddenly stopped."
Here is the new code fenprincipale.cpp
#include "fenprincipale.h" #include "ui_fenprincipale.h" #include "fencene.h" #include "fencenep.h" #include <QStackedWidget> #include <QPushButton> #include <QGridLayout> #include <QFont> FenPrincipale::FenPrincipale(QWidget *parent) : QMainWindow(parent), ui(new Ui::FenPrincipale) { ui->setupUi(this); delete centralfencen->layout(); centralfencen = new QStackedWidget; centralfencen ->addWidget(new FenCene); centralfencen ->addWidget(new FenCenep); setCentralWidget(centralfencen); bttoncene = new QPushButton("Cahier Electronique de Note de L'Enseignant (CEN_E)"); //Finir le boulot bttoncene->setFont(QFont("Times New Roman", 20)); bttoncene->setCursor(Qt::PointingHandCursor); bttoncenep = new QPushButton("Cahier Electronique de Note de L'Enseignant Principal (CEN_EP)"); bttoncenep->setFont(QFont("Times New Roman", 20)); bttoncenep->setCursor(Qt::PointingHandCursor); quitter = new QPushButton("Quitter"); quitter->setFont(QFont("Times New Roman", 20)); quitter->setCursor(Qt::PointingHandCursor); fenprincipal = new QGridLayout; fenprincipal->addWidget(bttoncene, 0, 0); fenprincipal->addWidget(bttoncenep, 0, 2); fenprincipal->addWidget(quitter, 1, 0, 1, 2); delete centralfencen->layout(); centralfencen->setLayout(fenprincipal); connect(bttoncene, &QPushButton::clicked, this, &FenPrincipale::Affichefencene); connect(bttoncenep, &QPushButton::clicked, this, &FenPrincipale::Affichefencenep); } FenPrincipale::~FenPrincipale() { delete ui; } void FenPrincipale::Affichefencene() { centralfencen->setCurrentIndex(0); } void FenPrincipale::Affichefencenep() { centralfencen->setCurrentIndex(1); }
Why does not it compile?
Thanks -
Now you are trying to delete the centralfencen layout BEFORE it is initialized. This is the reason why it crashed. Because it tries to access and delete memory, which wasn't allocated before.
Just move the line down (above the last line in your constructor, where you set your layout)
EDIT: I just realized that the "delete layout"-line is two times in your code. Delete the first one (after setup ui) and keep the second one. That should fix it :)
-
@Pl45m4 I moved the line and it compiles but the buttons still do not display.
Here is an image of the compilation.
thanks
-
Mhh... If this wont work, I guess it has to do with your StackedWidget.
If you have a look at the StackedWidget-Doc, it says that the addWidget-Function sets the Widget as currentWidget, if the StackedWidget is empty. So maybe it displays just the wrong StackedWidget page?
"FenCene" is your current Widget-Page?
The use of StackedWidget and FenCene / FenCenep + the GridLayout confuses me a bit -
@Pl45m4
I will say it theoretically
My intention is to have the "Fencen" as the default centralwidget, on the default widget I would have three buttons set with a grillelayout.
The button- "bttoncene" if we click on the button the widgetcentral will be the graphical window of the class "Fencene".
- "bttoncenep" if we click on the button the widgetcentral will be the graphical window of the class "Fencenep".
- "quitter" if we click on the button we close the window.
Thanks for your help...
-
Exemple fait avec le Designer
Example made with the Designer
https://drive.google.com/open?id=18ZUdt2xk_Yb1gZRUCAwe-nUmcl7G5nIj -
Forget the delete-approach :) It only worked for me, because I did not create your FenCene(p) - Classes (I commented out the lines)
Your design structure seems wrong...
You will need at least 3 pages in your StackedWidget (which is your CentralWidget).
- Your main page (GridLayout with your Menu-PushButtons to select FenCene, FenCenep or Quit)
- One page which represents your FenCene-Widget
- One page which represents your FenCenep-Widget
Have a look at this:
MainWindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QPushButton> #include <QStackedWidget> #include <QGridLayout> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); public slots: void Affichefencene(); void Affichefencenep(); void showMainPage(); // Dummy slot to return to mainPage private: Ui::MainWindow *ui; QStackedWidget *centralfencen; QPushButton *bttoncene; QPushButton *bttoncenep; QPushButton *quitter; QGridLayout *fenprincipal; }; #endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); centralfencen = new QStackedWidget(); setCentralWidget(centralfencen); // Main Page in StackedWidget (shows Buttons to select option: FenCene, FenCenep or Quit) QWidget *main_page = new QWidget; // Buttons // Dummy Buttons to return to MainPage from FenCene(p) // PLACEHOLDER FOR YOUR FENCENE(P) QPushButton *btn_back1 = new QPushButton("FenCene-Page\nBack to MainPage", this); btn_back1->setFont(QFont("Times New Roman", 20)); btn_back1->setCursor(Qt::PointingHandCursor); QPushButton *btn_back2 = new QPushButton("FenCenep-Page\nBack to MainPage"); btn_back2->setFont(QFont("Times New Roman", 20)); btn_back2->setCursor(Qt::PointingHandCursor); // Your Buttons bttoncene = new QPushButton("Cahier Electronique de Note\nde L'Enseignant (CEN_E)"); //Finir le boulot bttoncene->setFont(QFont("Times New Roman", 20)); bttoncene->setCursor(Qt::PointingHandCursor); bttoncenep = new QPushButton("Cahier Electronique de Note\nde L'Enseignant Principal (CEN_EP)"); bttoncenep->setFont(QFont("Times New Roman", 20)); bttoncenep->setCursor(Qt::PointingHandCursor); quitter = new QPushButton("Quitter"); quitter->setFont(QFont("Times New Roman", 20)); quitter->setCursor(Qt::PointingHandCursor); fenprincipal = new QGridLayout(); fenprincipal->addWidget(bttoncene, 0, 0); fenprincipal->addWidget(bttoncenep, 0, 2); fenprincipal->addWidget(quitter, 1, 0, 1, 2); // Sets GridLayout with Buttons to mainPage in StackedWidget main_page->setLayout(fenprincipal); // Adds mainPage with Layout (+Buttons) to StackedWidget (= CentralWidget) centralfencen->addWidget(main_page); // Adds Page2 (FenCene) to StackedWidget centralfencen->addWidget(btn_back1); // Placeholder for FenCene // centralfencen->addWidget(new FenCene); // Your FenCene-Widget // Adds Page3 (FenCenep) centralfencen->addWidget(btn_back2); // Placeholder for FenCenep // centralfencen->addWidget(new FenCenep); // Your FenCenep-Widget connect(bttoncene, &QPushButton::clicked, this, &MainWindow::Affichefencene); connect(bttoncenep, &QPushButton::clicked, this, &MainWindow::Affichefencenep); // Connection to Quit program / close Window connect(quitter, &QPushButton::clicked, this, &MainWindow::close); // Dummy Connection to return to MainPage connect(btn_back1, &QPushButton::clicked, this, &MainWindow::showMainPage); connect(btn_back2, &QPushButton::clicked, this, &MainWindow::showMainPage); } MainWindow::~MainWindow() { delete ui; delete fenprincipal; delete bttoncene; delete bttoncenep; delete quitter; delete centralfencen; } void MainWindow::Affichefencene() { centralfencen->setCurrentIndex(1); } void MainWindow::Affichefencenep() { centralfencen->setCurrentIndex(2); } void MainWindow::showMainPage() { centralfencen->setCurrentIndex(0); }
-
Thanks....