如何在Qt中使用信号和插槽?

时间:2015-12-29 20:50:15

标签: c++ qt signals-slots

我需要将StorageSCP类中的loadReceivedFiles()信号链接到MainController内的test()插槽。

以下代码给了我3个错误:

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

错误2错误C2248:'QObject :: QObject':无法访问私有成员   在类'QObject'c:\ users \ 310214192 \ documents \ visual studio中声明   2012 \ projects \ Test \ Test \ storagescp.h 18 1测试

     

错误3错误C2248:'SCP :: SCP':无法访问私人会员   在'SCP'c:\ users \ 310214192 \ documents \ visual studio中声明   2012 \ projects \ Test \ Test \ storagescp.h 18 1测试

     

错误1错误C3867:'StorageSCPWorker :: getStorageSCP':函数调用   缺少参数列表;使用'& StorageSCPWorker :: getStorageSCP'来   创建指向成员C:\ Users \ 310214192 \ Documents \ Visual Studio的指针   2012 \ Projects \ Test \ Test \ maincontroller.cpp 10 1测试

我怎样才能使这个工作?

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();

1 个答案:

答案 0 :(得分:3)

  • 首先,getStorageSCP()应该返回一个指针,以防止复制对象
  • 第二次连接需要poiters QObject
  • 第三,连接的第二个参数必须是signal。由于您有notifyInstanceStored的实现,它显然是slot或常规函数。您的意思是将loadReceivedFiles()test()联系起来,而不是notifyInstanceStored()test()

所以:

您需要替换

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

通过

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

然后替换

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

通过

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

或(旧方式,Qt4,但仍在Qt5中工作):

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