简体   繁体   English

Qt连接无法连接到插槽

[英]Qt Connect Unable to Connect to Slot

I have no issues with connecting signals to slots in Qt until I met this issue. 在遇到此问题之前,我没有将信号连接到Qt中的插槽的问题。 I am unable to connect to the slot when I clicked on my tool button (btnNR). 当我点击我的工具按钮(btnNR)时,我无法连接到插槽。

The slot calls another widget to be shown. 插槽调用另一个小部件进行显示。 I am quite certain the connect signal is correctly formatted. 我很确定连接信号格式正确。 However, the slot is not called. 但是,不调用插槽。 Can anyone please help? 有人可以帮忙吗?

SetMalDlg.h: SetMalDlg.h:

#pragma once
#include <QDialog>
#include <QtGui>

class SetMalDlgInjRem;

class SetMalDlg : public QDialog
{
    Q_OBJECT

public:
    SetMalDlg(void);
    ~SetMalDlg(void);
    SetMalInjRem *malInjRem;

public slots:
   void slot_SetMalDlgInjRem();

public:
   void createLayout();
   ...
};

SetMalDlg.cpp: SetMalDlg.cpp:

#include "SetMalDlg.h"
#include "SetMalDlgInjRem.h"

SetMalDlg::SetMalDlg(void)
{
    malInjRem = new SetMalDlgInjRem;
    createLayout();
    connect(btnNR, SIGNAL(clicked()), this, SLOT(slot_SetMalDlgInjRem()));
    setWindowModality(Qt::WindowModal);
}

SetMalDlg::~SetMalDlg(void)
{
    disconnect(btnNR, SIGNAL(clicked()), this, SLOT(slot_SetMalDlgInjRem()));
}

void SetMalDlg::createLayout()
{
    ...

    // create btnNR here

    ...
}


void SetMalDlg::slot_SetMalDlgInjRem()
{
    malInjRem->show();
}

SetMalDlgInjRem.h: SetMalDlgInjRem.h:

#pragma once
#include <QDialog>
#include <QtGui>

class SetMalDlgInjRem : public QDialog
{
    Q_OBJECT

public:
    SetMalDlgInjRem(void);
    ~SetMalDlgInjRem(void);

public:
   void createLayout();
   ...
};

SetMalDlgInjRem.cpp: SetMalDlgInjRem.cpp:

#include "SetMalDlgInjRem.h"
#include <QtGui>

SetMalDlgInjRem::SetMalDlgInjRem(void)
{
    createLayout();
    setWindowModality(Qt::WindowModal);
}

SetMalDlgInjRem::~SetMalDlgInjRem(void)
{
}

void SetMalDlgInjRem::createLayout()
{
    this->resize(1033, 452);
    labelMalInjRem = new QLabel(this);
    labelMalInjRem->setText("Text");
    labelMalInjRem->setGeometry(QRect(10, 10, 301, 31));

    ...
}

Your code is fine you have to just clean project, qmake and then build. 你的代码很好,你只需要清理项目,qmake然后构建。 Since you have edited code but did not qmake it so meta object compiler was not properly invoked to update changes in moc generated files and hence slot was not actually connected. 由于您已经编辑了代码,但没有对其进行qmake,因此未正确调用元对象编译器来更新moc生成的文件中的更改,因此插槽实际上并未连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM