Search code examples
c++qtsignals-slots

How to get signals and slots working in Qt?


I need to link the loadReceivedFiles() signal inside StorageSCP class to the test() slot inside MainController.

The following code gave me 3 errors:

connect(storageSCPWorker->getStorageSCP, &StorageSCP::loadReceivedFiles, this, &MainController::test);

Error 2 error C2248: 'QObject::QObject' : cannot access private member declared in class 'QObject' c:\users\310214192\documents\visual studio 2012\projects\Test\Test\storagescp.h 18 1 Test

Error 3 error C2248: 'SCP::SCP' : cannot access private member declared in class 'SCP' c:\users\310214192\documents\visual studio 2012\projects\Test\Test\storagescp.h 18 1 Test

Error 1 error C3867: 'StorageSCPWorker::getStorageSCP': function call missing argument list; use '&StorageSCPWorker::getStorageSCP' to create a pointer to member C:\Users\310214192\Documents\Visual Studio 2012\Projects\Test\Test\maincontroller.cpp 10 1 Test

How can I get this working?

MainController.cpp:

MainController::MainController()
{
    storageSCPWorkerThread = new QThread();
    storageSCPWorker = new StorageSCPWorker(104, "AE_TEST", 16384, "C:/Test/Received/");
    storageSCPWorker->moveToThread(storageSCPWorkerThread);
    connect(storageSCPWorkerThread, &QThread::started, storageSCPWorker, &StorageSCPWorker::startSCP);
    //connect(storageSCPWorker->getStorageSCP, &StorageSCP::loadReceivedFiles, this, &MainController::test);
    storageSCPWorkerThread->start();
}

void MainController::test()
{
    qDebug() << "HELLO MOTO!!!!";
}

MainController.h:

class MainController : public QObject
{
    Q_OBJECT
public:
    MainController();
    ~MainController();
public slots:
    void test();
private:
    QThread* storageSCPWorkerThread;
    StorageSCPWorker* storageSCPWorker;
};

StorageSCPWorker.cpp:

StorageSCP StorageSCPWorker::getStorageSCP()
{
    return storageSCP;
}

StorageSCPWorker.h:

class StorageSCPWorker : public QObject
{
    Q_OBJECT
public:
    StorageSCPWorker(int port, const OFString &aeTitle, int maxPduLength, const OFString &outputDirectory);
    ~StorageSCPWorker();
    StorageSCP getStorageSCP();
public slots:
        void startSCP();
private:
    int port;
    OFString aeTitle;
    int maxPduLength;
    OFString outputDirectory;
    StorageSCP storageSCP;
    OFCondition status;
};

StorageSCP.cpp:

void StorageSCP::notifyInstanceStored()
{
    emit loadReceivedFiles();
}

StorageSCP.h:

signals:
    void loadReceivedFiles();

Solution

    • First, getStorageSCP() should return a pointer to prevent copying the object
    • Second connect takes poiters to QObject
    • Third, second parameter of connect must be a signal. As you have an implementation of notifyInstanceStored, it's apparently a slot, or a regular function. You meant to connect loadReceivedFiles(), to test(), not notifyInstanceStored() to test().

    So:

    You need to replace

    StorageSCP StorageSCPWorker::getStorageSCP()
    {
        return storageSCP;
    }
    

    By

    StorageSCP* StorageSCPWorker::getStorageSCP()
    {
        return &storageSCP;
    }
    

    And then replace

    connect(storageSCPWorker->getStorageSCP, &StorageSCP::notifyInstanceStored, this, &MainController::test);
    

    by

    connect(storageSCPWorker->getStorageSCP(), &StorageSCP::loadReceivedFiles, this, &MainController::test);
    

    or (the old way, Qt4, but still working in Qt5):

    connect(storageSCPWorker->getStorageSCP(), SIGNAL(loadReceivedFiles()), this, SLOT(test()));