Qt插槽不被调用
我缩短了代码。Qt插槽不被调用,qt,slot,Qt,Slot,我缩短了代码。 我有一个带按钮的主窗口。该按钮打开另一个窗口以注册某人的个人信息。 当我点击“Confirmer”时,它会触发confirmerInformations()槽。但是什么也没发生。 我不明白为什么。我没有错误日志 很抱歉代码太多,但这个问题让我发疯。 我不明白为什么第一个窗口中的插槽可以工作,尽管第二个窗口中语法完全相同的插槽不会运行方法confirmerInformations main.ccp int main(int argc, char *argv[]) { QAp
我有一个带按钮的主窗口。该按钮打开另一个窗口以注册某人的个人信息。
当我点击“Confirmer”时,它会触发confirmerInformations()槽。但是什么也没发生。
我不明白为什么。我没有错误日志 很抱歉代码太多,但这个问题让我发疯。 我不明白为什么第一个窗口中的插槽可以工作,尽管第二个窗口中语法完全相同的插槽不会运行方法
confirmerInformations
main.ccp
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Principale fenetrePrincipale;
fenetrePrincipale.show();
return app.exec();
}
#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();
};
#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");
}
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
铭文.h
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Principale fenetrePrincipale;
fenetrePrincipale.show();
return app.exec();
}
#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();
};
#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");
}
#如果没有铭文#
#定义铭文
#包括
#包括
#包括
#包括
#包括
#包括
#包括
类铭文:公共QWidget
{
Q_对象
公众:
题字();
私人:
//信息人员
QGroupBox*BoxInformationsPersonelles;
QFormLayout*表单信息人员;
QLabel*labelNom;
QLineEdit*名称;
//鲍顿
QGroupBox*boxBoutons;
QHBoxLayout*布局图;
QPushButton*Bouton确认;
//布局负责人
QVBoxLayout*布局原则;
公众时段:
无效确认信息();
};
铭文.cpp
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Principale fenetrePrincipale;
fenetrePrincipale.show();
return app.exec();
}
#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();
};
#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");
}
#包括“铭文.h”
#包括
#包括
题词:题词{
//盒子信息人员
labelNom=新的QLabel(“Nom:”);
nom_=新QLineEdit();
BoxInformationsPersonelles=新QGroupBox(“Vos信息人员”);
BoxInformationsPersonelles->setLayout(表格信息人员);
//箱形布顿
boutonConfirmation=新的QPushButton(“确认人”);
layoutBoutons=新的QHBoxLayout();
layoutBoutons->addWidget(boutonConfirmation);
boxBoutons=新QGroupBox(“确认”);
boxBoutons->setLayout(布局Boutons);
//布局负责人
layoutPrincipal=新的QVBoxLayout();
layoutPrincipal->addWidget(BoxInformationsPersonelles);
设置布局(布局原则);
//布顿连接
boutonConfirmation=新的QPushButton(“确认人”);
连接(boutonConfirmation)、信号(clicked()、此、插槽(confirmerInformations());
}
//槽
无效铭文::确认信息(){
QErrorMessage*erreurInformationsPersonelles=新QErrorMessage();
ErrEurinformationsPersonelles->showMessage(“Veuillez remplir吹嘘vos信息人员”);
}
分配内存两次
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
结论:在您的代码中,您使用的是未连接的旧按钮,因此您从不打电话。请在您的答案中添加逻辑,说明为什么会发生这种情况?简而言之,
layoutBoutons->addWidget(boutonConfirmation)
正在将泄漏的指针添加到UI,并且已连接到被覆盖的指针。当然,不要着急。仅仅是读者没有对自己进行这种双重建构产生错误的印象,就可能导致这种情况。谢谢。:)@lpapp已经完成了。我添加了简短的可编译代码和解释