2016-08-15 62 views
0

的提示我正在致力於異步多線程服務器,基於this example。現在,我下面的方法:QObject :: connect:沒有這樣的信號運行時間警告 - 無法找到

void UeClient::ueSlotReadyRead() 
{ 
    QList<QVariant> incomingData=QList<QVariant>(); 
    QByteArray incomingMessage=this->ueSocket()->readAll(); 

    UePosCommProtocolArch::UeCommand incomingCommand=UePOSCommProtocol::ueDecodeCommand(incomingMessage, 
                         incomingData); 

    UeTask* ueTask=new UeTask("testDb", 
           "testUsername", 
           "testPassword", 
           "localhost", 
           "3306", 
           incomingCommand); 

    connect(ueTask, 
      SIGNAL(ueSignalTaskFinished(UePosCommProtocolArch::UeCommand, 
             QByteArray)), 
      this, 
      SLOT(ueSlotFetchData(UePosCommProtocolArch::UeCommand, 
           QByteArray)), 
      Qt::QueuedConnection); 

    QThreadPool::globalInstance()->start(ueTask); 
} // ueSlotReadyRead 

我獲得以下運行時警告/錯誤QtCreator

的QObject ::連接:沒有這樣的信號UeTask :: ueSignalTask​​Finished(UePosCommProtocolArch :: UeCommand,QByteArray中)在../../../net/ueclient.cpp:55

這裏是UeTask類的頭文件,從中我們可以看到ueSignalTaskFinished(UePosCommProtocolArch::UeCommand& executedCommand, QByteArray& fetchedData);聲明

#ifndef UETASK_H 
#define UETASK_H 

#include <QObject> 
#include <QRunnable> 
#include <QSqlDatabase> 
#include <QSqlError> 
#include <QUuid> 
#include <QString> 
#include <QSqlQuery> 
#include <QSqlRecord> 
#include <QVariant> 
#include <QJsonDocument> 
#include <QJsonValue> 
#include <QJsonObject> 
#include <QJsonArray> 
#include <QByteArray> 

#include "../../uePOSCommProtocol/uecommands.h" 
#include "database/models/uefieldindexes.h" 
#include "database/models/uequeries.h" 

/*! 
* \brief The UeTask class 
*/ 
class UeTask : public QObject, 
       public QRunnable 
{ 
    Q_OBJECT 

private: 
    /*! 
    * \brief m_ueDatabaseName 
    */ 
    QString m_ueDatabaseName; 

    /*! 
    * \brief m_ueUserName 
    */ 
    QString m_ueUserName; 

    /*! 
    * \brief m_uePassword 
    */ 
    QString m_uePassword; 

    /*! 
    * \brief m_ueHostName 
    */ 
    QString m_ueHostName; 

    /*! 
    * \brief m_uePort 
    */ 
    QString m_uePort; 

    /*! 
    * \brief m_ueCommand 
    */ 
    UePosCommProtocolArch::UeCommand m_ueCommand; 

    /*! 
    * @brief ueDatabaseName 
    * @return database name 
    */ 
    inline const QString& ueDatabaseName() const 
     { return this->m_ueDatabaseName; } 

    /*! 
    * @brief ueDatabaseUserName 
    * @return database user name 
    */ 
    inline const QString& ueDatabaseUserName() const 
     { return this->m_ueUserName; } 

    /*! 
    * @brief ueDatabasePassword 
    * @return database password 
    */ 
    inline const QString& ueDatabasePassword() const 
     { return this->m_uePassword; } 

    /*! 
    * @brief ueDatabasHost 
    * @return database host name 
    */ 
    inline const QString& ueDatabasHost() const 
     { return this->m_ueHostName; } 

    /*! 
    * @brief uePort 
    * @return database host port 
    */ 
    inline const QString& ueDatabaseHostPort() const 
     { return this->m_uePort; } 

    /*! 
    * \brief ueCommand 
    * \return task command 
    */ 
    inline UePosCommProtocolArch::UeCommand& ueCommand() 
     { return this->m_ueCommand; } 

    /*! 
    * @brief ueSetDatbaseName 
    * @param name 
    */ 
    inline void ueSetDatbaseName(const QString& name) 
     { this->m_ueDatabaseName=name; } 

    /*! 
    * @brief ueSetDatabaseUserName 
    * @param userName 
    */ 
    inline void ueSetDatabaseUserName(const QString& userName) 
     { this->m_ueUserName=userName; } 

    /*! 
    * @brief ueSetDatabasePassword 
    * @param password 
    */ 
    inline void ueSetDatabasePassword(const QString& password) 
     { this->m_uePassword=password; } 

    /*! 
    * @brief ueSetDatabaseHostName 
    * @param hostName 
    */ 
    inline void ueSetDatabaseHostName(const QString& hostName) 
     { this->m_ueHostName=hostName; } 

    /*! 
    * @brief ueSetDatabaseHostPort 
    * @param port 
    */ 
    inline void ueSetDatabaseHostPort(const QString& port) 
     { this->m_uePort=port; } 

    /*! 
    * \brief ueSetCommand 
    * \param command 
    */ 
    inline void ueSetCommand(const UePosCommProtocolArch::UeCommand& command) 
     { this->m_ueCommand=command; } 

public: 
    /*! 
    * @brief UeTask 
    */ 
    explicit UeTask(const QString& databaseName, 
        const QString& userName, 
        const QString& password, 
        const QString& hostName, 
        const QString& port, 
        const UePosCommProtocolArch::UeCommand& command); 

protected: 
    /*! 
    * @brief run 
    */ 
    void run(); 

signals: 
    /*! 
    * \brief ueSignalTaskFinished 
    * \param executedCommand 
    * \param fetchedData 
    */ 
    void ueSignalTaskFinished(UePosCommProtocolArch::UeCommand& executedCommand, 
           QByteArray& fetchedData); 

    /*! 
    * @brief ueSignalDatabaseConnectionOk 
    */ 
    void ueSignalDatabaseConnectionOk(); 

    /*! 
    * @brief ueSignalDatabaseConnectionError 
    * @param error 
    */ 
    void ueSignalDatabaseConnectionError(const QSqlError& error); 
}; 

#endif // UETASK_H 

憑啥我是一個得到這個運行時警告/錯誤和signal連接到slot?這可能是一些愚蠢的錯誤做,但我一直在用這個bug了幾個小時的奮力...

回答

0

我已經成功地解決了通過@佩普的回答這個問題,我已經添加到constSIGNAL的聲明:

#ifndef UETASK_H 
#define UETASK_H 

#include <QObject> 
#include <QRunnable> 
#include <QSqlDatabase> 
#include <QSqlError> 
#include <QUuid> 
#include <QString> 
#include <QSqlQuery> 
#include <QSqlRecord> 
#include <QVariant> 
#include <QJsonDocument> 
#include <QJsonValue> 
#include <QJsonObject> 
#include <QJsonArray> 
#include <QByteArray> 

#include "../../uePOSCommProtocol/uecommands.h" 
#include "database/models/uefieldindexes.h" 
#include "database/models/uequeries.h" 

/*! 
* \brief The UeTask class 
*/ 
class UeTask : public QObject, 
       public QRunnable 
{ 
    Q_OBJECT 

private: 
    /*! 
    * \brief m_ueDatabaseName 
    */ 
    QString m_ueDatabaseName; 

    /*! 
    * \brief m_ueUserName 
    */ 
    QString m_ueUserName; 

    /*! 
    * \brief m_uePassword 
    */ 
    QString m_uePassword; 

    /*! 
    * \brief m_ueHostName 
    */ 
    QString m_ueHostName; 

    /*! 
    * \brief m_uePort 
    */ 
    QString m_uePort; 

    /*! 
    * \brief m_ueCommand 
    */ 
    UePosCommProtocolArch::UeCommand m_ueCommand; 

    /*! 
    * @brief ueDatabaseName 
    * @return database name 
    */ 
    inline const QString& ueDatabaseName() const 
     { return this->m_ueDatabaseName; } 

    /*! 
    * @brief ueDatabaseUserName 
    * @return database user name 
    */ 
    inline const QString& ueDatabaseUserName() const 
     { return this->m_ueUserName; } 

    /*! 
    * @brief ueDatabasePassword 
    * @return database password 
    */ 
    inline const QString& ueDatabasePassword() const 
     { return this->m_uePassword; } 

    /*! 
    * @brief ueDatabasHost 
    * @return database host name 
    */ 
    inline const QString& ueDatabasHost() const 
     { return this->m_ueHostName; } 

    /*! 
    * @brief uePort 
    * @return database host port 
    */ 
    inline const QString& ueDatabaseHostPort() const 
     { return this->m_uePort; } 

    /*! 
    * \brief ueCommand 
    * \return task command 
    */ 
    inline UePosCommProtocolArch::UeCommand& ueCommand() 
     { return this->m_ueCommand; } 

    /*! 
    * @brief ueSetDatbaseName 
    * @param name 
    */ 
    inline void ueSetDatbaseName(const QString& name) 
     { this->m_ueDatabaseName=name; } 

    /*! 
    * @brief ueSetDatabaseUserName 
    * @param userName 
    */ 
    inline void ueSetDatabaseUserName(const QString& userName) 
     { this->m_ueUserName=userName; } 

    /*! 
    * @brief ueSetDatabasePassword 
    * @param password 
    */ 
    inline void ueSetDatabasePassword(const QString& password) 
     { this->m_uePassword=password; } 

    /*! 
    * @brief ueSetDatabaseHostName 
    * @param hostName 
    */ 
    inline void ueSetDatabaseHostName(const QString& hostName) 
     { this->m_ueHostName=hostName; } 

    /*! 
    * @brief ueSetDatabaseHostPort 
    * @param port 
    */ 
    inline void ueSetDatabaseHostPort(const QString& port) 
     { this->m_uePort=port; } 

    /*! 
    * \brief ueSetCommand 
    * \param command 
    */ 
    inline void ueSetCommand(const UePosCommProtocolArch::UeCommand& command) 
     { this->m_ueCommand=command; } 

public: 
    /*! 
    * @brief UeTask 
    */ 
    explicit UeTask(const QString& databaseName, 
        const QString& userName, 
        const QString& password, 
        const QString& hostName, 
        const QString& port, 
        const UePosCommProtocolArch::UeCommand& command); 

protected: 
    /*! 
    * @brief run 
    */ 
    void run(); 

signals: 
    /*! 
    * \brief ueSignalTaskFinished 
    * \param executedCommand 
    * \param fetchedData 
    */ 
    void ueSignalTaskFinished(const UePosCommProtocolArch::UeCommand& executedCommand, 
           const QByteArray& fetchedData); 

    /*! 
    * @brief ueSignalDatabaseConnectionOk 
    */ 
    void ueSignalDatabaseConnectionOk(); 

    /*! 
    * @brief ueSignalDatabaseConnectionError 
    * @param error 
    */ 
    void ueSignalDatabaseConnectionError(const QSqlError& error); 
}; 

#endif // UETASK_H 

然後我不得不register meta type有:

qRegisterMetaType<UePosCommProtocolArch::UeCommand>("UePosCommProtocolArch::UeCommand"); 

在上層類的構造函數:

UeClient::UeClient(QObject *parent, 
        UeSettings* const settings) 
    : QObject(parent) 
{ 
    qRegisterMetaType<UePosCommProtocolArch::UeCommand>("UePosCommProtocolArch::UeCommand"); 

    QThreadPool::globalInstance()->setMaxThreadCount(10); 

    this->ueSetSettings(settings); 
} // constructor 

現在它作品!

2

信號簽名是引用類型(UePosCommProtocolArch :: UeCommand &,QByteArray中&)。你試圖用另一個簽名將信號連接到插槽。

+0

請注意,它將匹配,如果它是一個const引用 – UmNyobe

5

的信號的簽名不匹配的你在SIGNAL宏寫的簽名 - 這些都是非const引用:

SIGNAL(ueSignalTaskFinished(UePosCommProtocolArch::UeCommand&, // reference 
          QByteArray&)), //ditto 

非const引用的使用作爲信號參數已經是可疑足夠的,並用排隊連接使用它們肯定是錯誤的。你想在這裏做什麼?

+0

與這兩個參數我想從'ueSignalTask​​Finished'數據傳輸到'slot',所以我可以處理提取的數據,這是做錯了嗎?! – KernelPanic

+1

爲什麼不通過引用傳遞給const呢? – peppe

相關問題