2014-12-03 2 views
1

코드를 짧게했습니다.
버튼이있는 기본 창이 있습니다. 버튼을 누르면 다른 사람의 개인 정보를 등록 할 수있는 다른 창이 열립니다.
"Confirmer"를 클릭하면 confirmerInformations() 슬롯이 트리거됩니다. 그러나 아무 일도 일어나지 않습니다.
이유를 모르겠습니다. 오류 로그가 없습니다.Qt 슬롯이 호출되지 않습니다.

거대한 코드는 유감이지만이 문제는 나를 미치게합니다. 첫 번째 창에 슬롯이 작동하는 이유는 동일한 구문을 사용하여 두 번째 창에 슬롯이 방법을 실행되지 않습니다 있지만 는 이해가 안 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"); 

} 

답변

3

당신은 두 번 메모리를 할당합니다.

boutonConfirmation_ = new QPushButton("Confirmer"); 
//... 
boutonConfirmation_ = new QPushButton("Confirmer");//why? 

한 줄을 삭제하십시오.

설명. 내가 쉽게 시스템에서 컴파일 할 수있는 몇 가지 간단한 코드를 추가 할 문제를 보여

QPushButton *ptr;   //just pointer 
ptr = new QPushButton(this);//allocate memory, this is a parent 
ptr->setObjectName("ptr"); //object name to find it in future 
qDebug() << ptr;   //show ptr 
ptr = new QPushButton;  //allocate memory again, but without parent 
qDebug() << connect(ptr,SIGNAL(clicked()),this,SLOT(echo())); 
          //show that connection was succesfull 
qDebug() << "new ptr"<< ptr << "old ptr" << this->findChildren<QPushButton *>("ptr"); 
          //show new and old ptrs 

출력 :

QPushButton(0x28d726a8, name = "ptr") //our old ptr 
true         //connection succesfull 
new ptr QPushButton(0x28d726e8) old ptr (QPushButton(0x28d726a8, name = "ptr")) 
//pay attention that new ptr and old has different adresses and names, it is 2 different buttons. 

결론 : 당신이 연결되지 않은 오래된 버튼을 사용하여 코드에서, 그래서 당신은 결코 전화하지 않습니다.

+0

답변에 논리를 추가해 주시겠습니까? 즉,'layoutBoutons _-> addWidget (boutonConfirmation _);'은 UI에 누출 포인터를 추가하고 오버라이드 된 포인터에 연결되었습니다. – lpapp

+0

@lpapp이 작업을 몇 분 정도만 수행하십시오. – Chernobyl

+0

예. 서둘러서는 안됩니다. 독자들이이 이중 구조를 독자적으로 수행하는 잘못된 인상을 갖지 않는다는 사실은이 문제를 일으킬 수 있습니다. 감사. :) – lpapp