简体   繁体   中英

Qt Connect Unable to Connect to Slot

I have no issues with connecting signals to slots in Qt until I met this issue. I am unable to connect to the slot when I clicked on my tool button (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:

#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:

#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:

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

class SetMalDlgInjRem : public QDialog
{
    Q_OBJECT

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

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

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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