socket notifiers cannot be enabled from another thread
最简单粗暴的解决方法,使用如下代码执行write即可
QMetaObject::invokeMethod( &socket, std::bind( static_cast< qint64(QTcpSocket::*)(const QByteArray &) >( &QTcpSocket::write ), &socket, QByteArray( "xxxx" ) ) );
void Test::SendMsg(QString msg)
{
QByteArray str=msg.toUtf8();
str.append('\n');
QMetaObject::invokeMethod( tcpClient, std::bind( static_cast< qint64(QTcpSocket::*)(const QByteArray &) >( &QTcpSocket::write ), tcpClient, QByteArray( str ) ) );
}
同理,下面QTimes报错也可以解决
QObject::startTimer: Timers cannot be started from another thread
解决
QMetaObject::invokeMethod( &timer, std::bind( static_cast< void(QTimer::*)(int) >( &QTimer::start ), &timer, 1000 ) );