关于QT貌似不能在子线程发出connectToHost连接请求 发出去另一边貌似完全不接收
研究了很多 还是没找到问题,想了一个折中的方案
在主线程建立连接再给子线程去通信
void Widget::on_pushButton_clicked()
{
QRegExp rxp("\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");
if(!rxp.exactMatch(ui->lineEdit->text()))
{
QMessageBox::information(this, tr("错误"), tr("ip地址错误"));
return;
}
if(!flagSend)
{
m_client=new MySocket(ui->lineEdit->text());
connect(m_client,SIGNAL(readyRead()),this,SLOT(recvData()));
connect(m_client,SIGNAL(disconnected()),this,SLOT(slotDdisconnect()));
flagSend=true;
}
else
{
emit amendIp(ui->lineEdit->text());
}
}
void Widget::connectServer(QString ip)
{
this->hide();
receive_Thread=new ThreadReceive(ip);
receive_Thread->start();
}
void Widget::slotDdisconnect()
{
qDebug()<<"断开";
m_client->flagConnect=false;
delete m_client;
m_client=new MySocket(ui->lineEdit->text());
connect(m_client,SIGNAL(readyRead()),this,SLOT(recvData()));
connect(m_client,SIGNAL(disconnected()),this,SLOT(slotDdisconnect()));
}
void Widget::recvData()
{
QByteArray array= m_client->readAll();
qDebug()<<QString(array);
if(array.toStdString()=="连接成功")
{
qDebug()<<"发送套接字连接成功";
connectServer(ui->lineEdit->text());
send_Thread=new ThreadSend(ui->lineEdit->text());
m_client->moveToThread(send_Thread);
m_client->flagConnect=true;
send_Thread->start();
}
if(array.toStdString()=="清晰")
m_client->flagClarity=true;
if(array.toStdString()=="模糊")
m_client->flagClarity=false;
if(array.toStdString()=="无播放窗口")
{
QMessageBox::information(nullptr, tr("错误"), tr("服务器连接已满无播放窗口"));
send_Thread->quit();
delete m_client;
}
}
本文主要是QT学习遇到的bug的记录,希望有大佬可以指正