Application always crashed
-
Hi everyone
I'm following c++ lessons on openclassrooms and I have a problem with the TP ZeroClassGenerator
Indeed, each time I execute the project, the application crashed when I clicked on the button "Generer"my .pro
/QT += widgets SOURCES += \ fenprincipale.cpp \ fencodegenere.cpp \ main.cpp HEADERS += \ fenprincipale.h \ fencodegenere.h
main.cpp
#include <QApplication> #include <fenprincipale.h> int main(int argc, char *argv[]) { QApplication app(argc, argv); /*Generateur de class c++ TP cours openclassroom 26/01/2018 */ //Creation fenetre principal FenPrincipale fenetre; //Execution fenetre.show(); return app.exec(); }
fenprincipale.h
#ifndef FENPRINCIPALE_H #define FENPRINCIPALE_H #include <QWidget> #include <QLayout> #include <QGroupBox> #include <QFormLayout> #include <QLineEdit> #include <QCheckBox> #include <QTextEdit> #include <QDateEdit> #include <QPushButton> #include "fencodegenere.h" class FenPrincipale : public QWidget { Q_OBJECT public: FenPrincipale(); private slots: void genererClass(); private: QGroupBox *grp1; QLayout *afa; QLineEdit *nom; QLineEdit *mere; QFormLayout *layoutGrp1; QGroupBox *grp2; QCheckBox *check1; QCheckBox *check2; QCheckBox *check3; QVBoxLayout *layoutGrp2; QGroupBox *grp3; QLineEdit *auteur; QDateEdit *date; QTextEdit *commentaire; QFormLayout *layoutGrp3; QPushButton *generer; QPushButton *quitter; QGridLayout *grp4; QVBoxLayout *layoutPrincipal; }; #endif // FENPRINCIPALE_H
fenprincipale.cpp
#include "fenprincipale.h" #include <QLayout> #include <QGroupBox> #include <QFormLayout> #include <QLineEdit> #include <QCheckBox> #include <QTextEdit> #include <QDateEdit> #include <QPushButton> #include <QMessageBox> FenPrincipale::FenPrincipale() { setWindowTitle("Zero Class Generator"); //Groupe 1 QGroupBox *grp1 = new QGroupBox; grp1->setTitle("Définition de la classe"); QLineEdit *nom = new QLineEdit; QLineEdit *mere = new QLineEdit; QFormLayout *layoutGrp1 = new QFormLayout(grp1); layoutGrp1->addRow("&Nom",nom); layoutGrp1->addRow("&Classe mère",mere); grp1->setLayout(layoutGrp1); //Groupe 2 QGroupBox *grp2 = new QGroupBox; grp2->setTitle("Options"); QCheckBox *check1 = new QCheckBox("Protéger le &header contre les inclusions multiples"); check1->setChecked(true); QCheckBox *check2 = new QCheckBox("Générer un &constructeur par défaut"); check2->setChecked(true); QCheckBox *check3 = new QCheckBox("Générer un &destructeur"); QVBoxLayout *layoutGrp2 = new QVBoxLayout; layoutGrp2->addWidget(check1); layoutGrp2->addWidget(check2); layoutGrp2->addWidget(check3); grp2->setLayout(layoutGrp2); //Groupe 3 QGroupBox *grp3 = new QGroupBox; grp3->setTitle("Ajouter des commentaires"); grp3->setCheckable(true); QLineEdit *auteur = new QLineEdit; QDateEdit *date = new QDateEdit; QTextEdit *commentaire = new QTextEdit; QFormLayout *layoutGrp3 = new QFormLayout; layoutGrp3->addRow("&Auteur",auteur); layoutGrp3->addRow("Da&te de création",date); layoutGrp3->addRow("&Rôle de la classe",commentaire); grp3->setLayout(layoutGrp3); //Groupe 4 QPushButton *generer = new QPushButton("Générer !"); QPushButton *quitter = new QPushButton("Quitter"); QHBoxLayout *grp4 = new QHBoxLayout; grp4->addWidget(generer); grp4->addWidget(quitter); grp4->setAlignment(Qt::AlignRight); //Layout principale QVBoxLayout *layoutPrincipal = new QVBoxLayout; layoutPrincipal->addWidget(grp1); layoutPrincipal->addWidget(grp2); layoutPrincipal->addWidget(grp3); layoutPrincipal->addLayout(grp4); setLayout(layoutPrincipal); //Connections connect(quitter,SIGNAL(clicked(bool)),this,SLOT(close())); connect(generer,SIGNAL(clicked(bool)),this,SLOT(genererClass())); } void FenPrincipale::genererClass() { if (nom->text().isEmpty()) { QMessageBox::critical(this,"Erreur","Veuillez selectionner au moins un nom de classe"); } QString code; if((grp3->isChecked())==true) { code+="/*\rAuteur : "+auteur->text()+"\rDate de création : "+date->text()+"\nRôle :\r"+commentaire->toPlainText()+"\r*/\n\n"; } QString nomMaj = nom->text(); nomMaj = nomMaj.toUpper(); if(check1->isChecked()) { code+="#ifndef HEADER_"+nomMaj+"\r#define HEADER_"+nomMaj+"\r\r"; } code+="class "+nom->text(); if(!(mere->text()=="")) { code+=": public "+mere->text(); } code+="\r{\r\tpublic:\r"; if(check2->isChecked()) { code+="\t\t"+nom->text()+"();"; } if(check3->isChecked()) { code+="\r\t\t~"+nom->text()+"();"; } code+="\r\n\tprotected:\r\n\tprivate:\r\n};"; if(check1->isChecked()) { code+="#endif "+nomMaj+"_H"; } FenCodeGenere *test = new FenCodeGenere(code,this); test->exec(); }
fencodegenere.h
#ifndef FENCODEGENERE_H #define FENCODEGENERE_H #include <QString> #include <QDialog> #include <QTextEdit> #include <QPushButton> class FenCodeGenere : public QDialog { public: FenCodeGenere(); FenCodeGenere(QString &a, QWidget *parent); private: QTextEdit *zone; QTextEdit *quitter; }; #endif // FENCODEGENERE_H
fencodegenere.cpp
#include "fencodegenere.h" #include <QLayout> FenCodeGenere::FenCodeGenere() { } FenCodeGenere::FenCodeGenere(QString &a, QWidget *parent = 0) : QDialog(parent) { setWindowTitle("Zero Class Generator"); QTextEdit *zone = new QTextEdit; zone->setText(a); QPushButton *quitter = new QPushButton("Quitter"); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(zone); layout->addWidget(quitter); setLayout(layout); QObject::connect(quitter,SIGNAL(clicked(bool)),this,SLOT(close())); }
so the program is crashing when the slot genererclass is execute
i need help please :/
alex -
Hi everyone
I'm following c++ lessons on openclassrooms and I have a problem with the TP ZeroClassGenerator
Indeed, each time I execute the project, the application crashed when I clicked on the button "Generer"my .pro
/QT += widgets SOURCES += \ fenprincipale.cpp \ fencodegenere.cpp \ main.cpp HEADERS += \ fenprincipale.h \ fencodegenere.h
main.cpp
#include <QApplication> #include <fenprincipale.h> int main(int argc, char *argv[]) { QApplication app(argc, argv); /*Generateur de class c++ TP cours openclassroom 26/01/2018 */ //Creation fenetre principal FenPrincipale fenetre; //Execution fenetre.show(); return app.exec(); }
fenprincipale.h
#ifndef FENPRINCIPALE_H #define FENPRINCIPALE_H #include <QWidget> #include <QLayout> #include <QGroupBox> #include <QFormLayout> #include <QLineEdit> #include <QCheckBox> #include <QTextEdit> #include <QDateEdit> #include <QPushButton> #include "fencodegenere.h" class FenPrincipale : public QWidget { Q_OBJECT public: FenPrincipale(); private slots: void genererClass(); private: QGroupBox *grp1; QLayout *afa; QLineEdit *nom; QLineEdit *mere; QFormLayout *layoutGrp1; QGroupBox *grp2; QCheckBox *check1; QCheckBox *check2; QCheckBox *check3; QVBoxLayout *layoutGrp2; QGroupBox *grp3; QLineEdit *auteur; QDateEdit *date; QTextEdit *commentaire; QFormLayout *layoutGrp3; QPushButton *generer; QPushButton *quitter; QGridLayout *grp4; QVBoxLayout *layoutPrincipal; }; #endif // FENPRINCIPALE_H
fenprincipale.cpp
#include "fenprincipale.h" #include <QLayout> #include <QGroupBox> #include <QFormLayout> #include <QLineEdit> #include <QCheckBox> #include <QTextEdit> #include <QDateEdit> #include <QPushButton> #include <QMessageBox> FenPrincipale::FenPrincipale() { setWindowTitle("Zero Class Generator"); //Groupe 1 QGroupBox *grp1 = new QGroupBox; grp1->setTitle("Définition de la classe"); QLineEdit *nom = new QLineEdit; QLineEdit *mere = new QLineEdit; QFormLayout *layoutGrp1 = new QFormLayout(grp1); layoutGrp1->addRow("&Nom",nom); layoutGrp1->addRow("&Classe mère",mere); grp1->setLayout(layoutGrp1); //Groupe 2 QGroupBox *grp2 = new QGroupBox; grp2->setTitle("Options"); QCheckBox *check1 = new QCheckBox("Protéger le &header contre les inclusions multiples"); check1->setChecked(true); QCheckBox *check2 = new QCheckBox("Générer un &constructeur par défaut"); check2->setChecked(true); QCheckBox *check3 = new QCheckBox("Générer un &destructeur"); QVBoxLayout *layoutGrp2 = new QVBoxLayout; layoutGrp2->addWidget(check1); layoutGrp2->addWidget(check2); layoutGrp2->addWidget(check3); grp2->setLayout(layoutGrp2); //Groupe 3 QGroupBox *grp3 = new QGroupBox; grp3->setTitle("Ajouter des commentaires"); grp3->setCheckable(true); QLineEdit *auteur = new QLineEdit; QDateEdit *date = new QDateEdit; QTextEdit *commentaire = new QTextEdit; QFormLayout *layoutGrp3 = new QFormLayout; layoutGrp3->addRow("&Auteur",auteur); layoutGrp3->addRow("Da&te de création",date); layoutGrp3->addRow("&Rôle de la classe",commentaire); grp3->setLayout(layoutGrp3); //Groupe 4 QPushButton *generer = new QPushButton("Générer !"); QPushButton *quitter = new QPushButton("Quitter"); QHBoxLayout *grp4 = new QHBoxLayout; grp4->addWidget(generer); grp4->addWidget(quitter); grp4->setAlignment(Qt::AlignRight); //Layout principale QVBoxLayout *layoutPrincipal = new QVBoxLayout; layoutPrincipal->addWidget(grp1); layoutPrincipal->addWidget(grp2); layoutPrincipal->addWidget(grp3); layoutPrincipal->addLayout(grp4); setLayout(layoutPrincipal); //Connections connect(quitter,SIGNAL(clicked(bool)),this,SLOT(close())); connect(generer,SIGNAL(clicked(bool)),this,SLOT(genererClass())); } void FenPrincipale::genererClass() { if (nom->text().isEmpty()) { QMessageBox::critical(this,"Erreur","Veuillez selectionner au moins un nom de classe"); } QString code; if((grp3->isChecked())==true) { code+="/*\rAuteur : "+auteur->text()+"\rDate de création : "+date->text()+"\nRôle :\r"+commentaire->toPlainText()+"\r*/\n\n"; } QString nomMaj = nom->text(); nomMaj = nomMaj.toUpper(); if(check1->isChecked()) { code+="#ifndef HEADER_"+nomMaj+"\r#define HEADER_"+nomMaj+"\r\r"; } code+="class "+nom->text(); if(!(mere->text()=="")) { code+=": public "+mere->text(); } code+="\r{\r\tpublic:\r"; if(check2->isChecked()) { code+="\t\t"+nom->text()+"();"; } if(check3->isChecked()) { code+="\r\t\t~"+nom->text()+"();"; } code+="\r\n\tprotected:\r\n\tprivate:\r\n};"; if(check1->isChecked()) { code+="#endif "+nomMaj+"_H"; } FenCodeGenere *test = new FenCodeGenere(code,this); test->exec(); }
fencodegenere.h
#ifndef FENCODEGENERE_H #define FENCODEGENERE_H #include <QString> #include <QDialog> #include <QTextEdit> #include <QPushButton> class FenCodeGenere : public QDialog { public: FenCodeGenere(); FenCodeGenere(QString &a, QWidget *parent); private: QTextEdit *zone; QTextEdit *quitter; }; #endif // FENCODEGENERE_H
fencodegenere.cpp
#include "fencodegenere.h" #include <QLayout> FenCodeGenere::FenCodeGenere() { } FenCodeGenere::FenCodeGenere(QString &a, QWidget *parent = 0) : QDialog(parent) { setWindowTitle("Zero Class Generator"); QTextEdit *zone = new QTextEdit; zone->setText(a); QPushButton *quitter = new QPushButton("Quitter"); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(zone); layout->addWidget(quitter); setLayout(layout); QObject::connect(quitter,SIGNAL(clicked(bool)),this,SLOT(close())); }
so the program is crashing when the slot genererclass is execute
i need help please :/
alex@alex_tu28 Hi, friend. Welcome.
you should to debug it to see which line has crashed. maybe where is pointer value is
NULL
.you can try
dichotomy
way to know where has crash in your code. the crash happened after clickedGenerer Button
,so, you can debug some information in
genererClass function
...to find, crash area.good luck.
void FenPrincipale::genererClass() { qDebug("Error: 01"); if (nom->text().isEmpty()) { QMessageBox::critical(this,"Erreur","Veuillez selectionner au moins un nom de classe"); } qDebug("Error: 02"); QString code; if((grp3->isChecked())==true) { code+="/*\rAuteur : "+auteur->text()+"\rDate de création : "+date->text()+"\nRôle :\r"+commentaire->toPlainText()+"\r*/\n\n"; } qDebug("Error: 03"); QString nomMaj = nom->text(); nomMaj = nomMaj.toUpper(); if(check1->isChecked()) { code+="#ifndef HEADER_"+nomMaj+"\r#define HEADER_"+nomMaj+"\r\r"; } code+="class "+nom->text(); qDebug("Error: 04"); if(!(mere->text()=="")) { code+=": public "+mere->text(); } code+="\r{\r\tpublic:\r"; if(check2->isChecked()) { code+="\t\t"+nom->text()+"();"; } if(check3->isChecked()) { code+="\r\t\t~"+nom->text()+"();"; } code+="\r\n\tprotected:\r\n\tprivate:\r\n};"; if(check1->isChecked()) { code+="#endif "+nomMaj+"_H"; } qDebug("Error: 05"); FenCodeGenere *test = new FenCodeGenere(code,this); test->exec(); qDebug("Error: 06"); }
-
Ok thanks you @joeQ
I debug some information in genererClass function like you said and I got a "Error 1" in the output
So I put the first if condition in comment and when I execute i got this time a "Error 2"!!
And that's it until "Error 5"
But the end of the function with the object FenCodeGenere is working correctly
So after some test I found that the crash is happening when I use attributs in my if condition
Are my pointers badly declared? -
@alex_tu28 said in Application always crashed:
Are my pointers badly declared?
Hi
Its hard to declare them wrongly
ClassType * varname;
But you might have forgotten to new them at some point ?
varname = new ClassType()
(you seem to new stuff so might be ok)
But i spotted one classic whoops, you should fix.
in fencodegenere.h , you declare
QTextEdit *zone;
but you new a local copy!
QTextEdit *zone = new QTextEdit; // that is NOT the one from .h but new one !
same with QTextEdit *quitter; you later make a QPushButton called quitter, not a QTextEdit. is that intended?
Make sure you are newing the ones from .h file and not make local copy.What is attributs ?
can you show its declaration ? -
Hi
Super
Its a classic copy & paste error :)You seems to be making a code generator ?
Note if the string+string+string becomes very complex, you might consider using something like
QString classTemplate = R"( class %1 %2 { }; )"; void MainWindow::on_btGenerate_released() { QString out = classTemplate.arg("classname").arg(":parent;"); qDebug() << out;
Or for even more complex generation, something like
https://github.com/steveire/grantlee
really helps. -
It's a code generator indeed
Yes it could be better if I want to continue
Thank you for this precisions and help ;)@alex_tu28
Well for small generations, the + way works super. im just mentioned it
as im a big fan of code generators and if they live long and get many options, it became hard
to read in the due to the MANY + operations and the template being very fragmented to look at.