-1

I've tried to emit a custom signal login() from my loginmanager class to the mainwindow. The signal is fired on the loginButtonClicked slot, and to my understand on the signal/slot mechanism, it should be able to capture any signal fired event and "look" for the corresponding slot to be execute. But it doesn't work as what I've think.

The connect function returns 1, which means it is able to be implemented in the moc file, and it DOES work if i run the m_LoginManager->setLogin() which fires the login() signal.

But what I prefer is the signal is emitted by the loginButton, and pass to the mainwindow to be process (in this case, init()).

Please correct me if I'm wrong.

Below are the code.

loginmanager.cpp

LoginManager::LoginManager(QWidget * parent) : QWidget(parent) 
{
    ui.setupUi(this);

    connect(ui.loginButton, SIGNAL(clicked()), this, SLOT(loginButtonClicked());
}

LoginManager::~LoginManager() 
{

}

void LoginManager::setLogin()
{
    emit login();
}

void LoginManager::loginButtonClicked()
{
    setLogin();
}

loginmanager.hpp

#include <QWidget>
#include "ui_loginmanager.h"

class DatabaseManager;
class SettingManager;

class LoginManager : public QWidget 
{
    Q_OBJECT

public:
    LoginManager(QWidget * parent = Q_NULLPTR);
    ~LoginManager();

    void setLogin();

signals:
    void login();

public slots:
    void loginButtonClicked();

private:
    Ui::LoginManager ui;
};

mainwindow.hpp

#include <QtWidgets/QMainWindow>
#include "ui_safeboxmanager.h"

class SafeboxManager : public QMainWindow
{
    Q_OBJECT

public:
    SafeboxManager(QWidget *parent = 0);
    ~SafeboxManager();

public slots:
    void init();

private:
    Ui::SafeboxManagerClass ui;
    LoginManager*       m_LoginManager;
};

#endif // SAFEBOXMANAGER_H

mainwindow.cpp

#include "safeboxmanager.hpp"
#include "loginmanager.hpp"

SafeboxManager::SafeboxManager(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    m_LoginManager = new LoginManager();

    ui.mainToolBar->setEnabled(false);
    ui.tableWidget->setEnabled(false);

    connect(m_LoginManager, SIGNAL(login()), this, SLOT(init()));

    //m_LoginManager->setLogin() << this work
}

SafeboxManager::~SafeboxManager()
{

}

void SafeboxManager::init()
{
    ui.mainToolBar->setEnabled(true);
    ui.tableWidget->setEnabled(true);
}
Louis Langholtz
  • 2,913
  • 3
  • 17
  • 40
ReverseEngineer
  • 529
  • 1
  • 5
  • 18
  • This could help: http://stackoverflow.com/q/26422154/1421332 – Silicomancer Oct 08 '16 at 17:43
  • I've read through this. Checked everything (I believe) and the problem still persist. I've read a few people asking about exactly the same issue I'm facing but did not get a proper solution. That's why I decided to ask again hopefully can help the others as well.. – ReverseEngineer Oct 08 '16 at 17:52
  • You should check the connect() return values in your code. Also loginButtonClicked() definition lacks the class name. – Silicomancer Oct 08 '16 at 17:55
  • As stated in my post, the connect return true. The emit login() is fired but the slot function did not get called. The loginButtonClicked() class name definition is just a typo. I'll edit it. – ReverseEngineer Oct 08 '16 at 18:01
  • 1
    Are you sure SafeboxManager isn't destroyed while LoginManager isn't? Check life times. – Silicomancer Oct 08 '16 at 18:04
  • SafeboxManager isn't destroyed, not even LoginManager. – ReverseEngineer Oct 09 '16 at 02:59
  • @Silicomancer, seems like you're right. My LoginManager only stores on the stack, so it is destroyed when the main function exits. – ReverseEngineer Oct 09 '16 at 03:42
  • Ok, I made it an actual answer. You should accept it and you should remove the "[SOLVED]" from your question title. – Silicomancer Oct 09 '16 at 07:58

1 Answers1

0

SafeboxManager and LoginManager objects must live long enough. Check life times.

Silicomancer
  • 8,604
  • 10
  • 63
  • 130
  • I also added this kind of mistake to the signal/slot trouble shooting answer here: http://stackoverflow.com/a/26422155/1421332 – Silicomancer Oct 09 '16 at 08:10