我正在开发基于this example的异步多线程服务器。现在,我采用以下方法:
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 :: connect:在../../../ net / ueclient.cpp:55
中没有这样的信号UeTask :: ueSignalTaskFinished(UePosCommProtocolArch :: UeCommand,QByteArray)
以下是 UeTask 类头文件,我们可以从中查看ueSignalTaskFinished(UePosCommProtocolArch::UeCommand& executedCommand, QByteArray& fetchedData);
IS声明:
#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
?这可能是一些愚蠢的错误,但我一直在努力解决这个错误几个小时......
答案 0 :(得分:5)
您的信号的签名与您在SIGNAL
宏中写的内容的签名不匹配 - 这些是非const引用:
SIGNAL(ueSignalTaskFinished(UePosCommProtocolArch::UeCommand&, // reference
QByteArray&)), //ditto
使用非const引用作为信号参数已经足够可疑,并且将它们与排队连接一起使用肯定是错误的。你想在这里实现什么目标?
答案 1 :(得分:2)
信号签名是引用类型(UePosCommProtocolArch :: UeCommand&amp;,QByteArray&amp;)。你试图用另一个签名将信号连接到插槽。
答案 2 :(得分:0)
我已经设法通过@peppe的答案解决了这个问题,并且已经const
添加了SIGNAL
的声明:
#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
现在它有效!