未调用 Qt 插槽

  • 本文关键字:插槽 Qt 调用 qt slot
  • 更新时间 :
  • 英文 :


我缩短了代码。
我有一个带按钮的主窗口。该按钮将打开另一个窗口以注册某人的个人信息。
当我单击"确认者"时,它应该触发确认者信息()插槽。然而什么也没发生。
我不明白为什么。我没有错误日志。

很抱歉代码庞大,但这个问题让我发疯。我不明白为什么第一个窗口中的插槽有效,尽管第二个窗口中具有完全相同语法的插槽不会运行该方法confirmerInformations

主.CCP

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    Principale fenetrePrincipale;
    fenetrePrincipale.show();
    return app.exec();
}

校长

#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

校长.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();
    }

铭文.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();
    };

铭文.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");
}

您分配内存两次。

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.

结论:在您的代码中,您使用未连接的旧按钮,因此您的插槽永远不会调用。

最新更新