I have shortened the code.
I have a main window with a button. The button opens another window to register someone's personnal information.
When I click on "Confirmer" it should trigger the confirmerInformations() slot. However nothing happens.
I don't understand why. I have no error log.
Sorry for the huge code but this problem makes me crazy.
I don't understand why the slot in the first window works although the slot in the second window with the exact same syntax won't run the method confirmerInformations
main.ccp
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Principale fenetrePrincipale;
fenetrePrincipale.show();
return app.exec();
}
Principale.h
#ifndef PRINCIPALE_H
#define PRINCIPALE_H
#include<QApplication>
#include<QPushButton>
#include<QBoxLayout>
#include<QGroupBox>
#include"Inscription.h"
#include"Connexion.h"
class Principale: public QWidget
{
Q_OBJECT
public:
Principale();
public slots:
void inscription();
void connexion();
private:
QPushButton * boutonInscription;
QVBoxLayout * vboxPrincipale;
QVBoxLayout * layoutPrincipal;
QGroupBox * general;
QGroupBox* groupPrincipal;
};
#endif // PRINCIPALE_H
Principale.cpp
Principale::Principale()
{
setFixedSize(250, 150);
boutonInscription = new QPushButton("Ouvrir un nouveau compte bancaire");
vboxPrincipale = new QVBoxLayout;
vboxPrincipale->addWidget(boutonInscription);
general = new QGroupBox("Cliquez sur un bouton");
general->setLayout(vboxPrincipale);
layoutPrincipal = new QVBoxLayout;
layoutPrincipal->addWidget(general);
setLayout(layoutPrincipal);
setWindowTitle("Bienvenue dans votre banque");
connect(boutonInscription, SIGNAL(clicked()), this, SLOT(inscription()));
}
void Principale::inscription()
{
Inscription *uneInscription = new Inscription();
uneInscription->show();
}
Inscription.h
#ifndef INSCRIPTION_H
#define INSCRIPTION_H
#include<QWidget>
#include<QLineEdit>
#include<QPushButton>
#include<QGroupBox>
#include<QBoxLayout>
#include<QLabel>
#include<QFormLayout>
class Inscription : public QWidget
{
Q_OBJECT
public:
Inscription();
private:
// Information personnellses
QGroupBox* boxInformationsPersonnelles_;
QFormLayout* formInformationsPersonnelles_;
QLabel* labelNom_;
QLineEdit* nom_;
// Boutons
QGroupBox* boxBoutons_;
QHBoxLayout* layoutBoutons_;
QPushButton* boutonConfirmation_;
//Layout principal
QVBoxLayout* layoutPrincipal_;
public slots:
void confirmerInformations();
};
Inscription.cpp
#include"Inscription.h"
#include<QErrorMessage>
#include<QDebug>
Inscription::Inscription(){
// Box Informations personnelles
labelNom_ = new QLabel("Nom :");
nom_ = new QLineEdit();
boxInformationsPersonnelles_ = new QGroupBox("Vos informations personnelles");
boxInformationsPersonnelles_->setLayout(formInformationsPersonnelles_);
// Box boutons
boutonConfirmation_ = new QPushButton("Confirmer");
layoutBoutons_ = new QHBoxLayout();
layoutBoutons_->addWidget(boutonConfirmation_);
boxBoutons_ = new QGroupBox("Confirmation");
boxBoutons_->setLayout(layoutBoutons_);
// Layout principal
layoutPrincipal_ = new QVBoxLayout();
layoutPrincipal_->addWidget(boxInformationsPersonnelles_);
setLayout(layoutPrincipal_);
// Connexion des boutons
boutonConfirmation_ = new QPushButton("Confirmer");
connect(boutonConfirmation_, SIGNAL(clicked()), this, SLOT(confirmerInformations()));
}
//Slots
void Inscription::confirmerInformations(){
QErrorMessage* erreurInformationsPersonnelles = new QErrorMessage();
erreurInformationsPersonnelles->showMessage("Veuillez remplir toutes vos informations personnelles");
}
You allocate memory twice.
Remove one line.
Explanation. I want to add some short code which you can easily compile on your machine and show the problem:
Output:
Conclusion: in your code you use old button which was not connected, so you slot never calls.