我目前在 Qt 中遇到一个奇怪的问题。我有一个使用 QTcpSocket 处理多个客户端的应用程序。客户端向服务器提供登录详细信息,服务器正在检查 MySQL 数据库以验证单独线程上的凭据。
我有 2 个不同的客户端程序登录到该服务器 1. 一个使用 Qt 连接到那里的简单客户端 2. 一个用 C-Sharp (.net) 编码的客户端
然而
当第二个客户端尝试登录服务器时,我在服务器上收到一条错误消息,告诉我:
QObject:无法为不同线程中的父级创建子级。Parent是QNativeSocketEngine(0x1231e00),parent的线程是QThread(0xc625f0),当前线程是CMySQLHandler(0xc67188)
我猜这发生在 MySQL 处理程序线程上但奇怪的是,它根本没有发生在用 Qt 编码的客户端上
这是代码:
void CMySQLHandler::onAuthRequest(ulong clientID, QString email, QString psw, QString HWID, QString ip)
{
string pass = "", salt = "", name = "", avatar = "";
uint UserID = 0;
CUserData *extra = new CUserData();
bool success = false;
QString failReason = "Success";
string Query = "SELECT password, username, avatar, userid FROM jos_users LEFT JOIN jos_community_users ON jos_users.id = jos_community_users.userid WHERE email = '"+email.toStdString()+"'";
SQLRow Row = GetRow(Query);
if(!Row.empty())
{
QString hashedPass = QString(Row[0].c_str());
pass = hashedPass.split(':').at(0).toStdString();
salt = hashedPass.split(':').at(1).toStdString();
name = Row[1];
avatar = Row[2];
UserID = QString(Row[3].c_str()).toUInt();
QByteArray data;
data.append((psw + QString(salt.c_str())));
QString blah = QString(QCryptographicHash::hash(data,QCryptographicHash::Md5).toHex());
if(QString(pass.c_str()) == blah )
success = true;
else
failReason = "Invalid password";
}
else
{
failReason = "E-Mail not found";
emit Handle->onAuthCompleted(clientID, false, failReason, extra);
return;
}
extra->Avatar += avatar.c_str();
extra->UserID = UserID;
extra->Username = QString(name.c_str());
emit Handle->onAuthCompleted(clientID, true, QString(), extra);
}
这是信号定义
void CMySQLHandler :: onTimerInit()
{
//Authentication
connect(this, SIGNAL(doAuthCompleted(ulong,bool,QString,CUserData*)), Handle, SLOT(onAuthCompleted(ulong,bool,QString,CUserData*)));
connect(Handle, SIGNAL(doRequestAuth(ulong,QString,QString,QString,QString)), this, SLOT(onAuthRequest(ulong,QString,QString,QString,QString)));
//end
CMySQL::Init(Handle);
}
我是 Qt 框架的新手。请原谅缺乏知识。
感谢您花时间阅读本文。