안녕하세요, 백그라운드에서 소켓 통신을해야하는데, QtConcurrent::run
을 사용했지만 경고 메시지를 전합니다. 여기 QObject 다른 스레드에있는 부모를위한 자식을 만들 수 없습니다.
QObject: Cannot create children for a parent that is in a different thread.
(Parent is MainWindow(0x7fff3e69f500), parent's thread is QThread(0x16f8070), current thread is QThread(0x17413d0)
는
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),ui(new Ui::MainWindow){
ui->setupUi(this);
QFuture<void> f2 = QtConcurrent::run(this,&MainWindow::checkCamStatus);
}
void MainWindow::checkCamStatus(){
while(1){
bool flag = false;
QTcpSocket* socket = new QTcpSocket(this);
socket->moveToThread(this->thread());
socket->setParent(this);
socket->connectToHost("10.0.7.112", 80);
if(socket->waitForConnected(1000))//check for connection for i second
{
qDebug() << "Cam online";
}
else{
qDebug() << "..............................Cam offline";
}
QThread::sleep(1);
}
}
가 어떻게 경고를 제거 할 수 있습니다, 코드인가?
문제는 무엇입니까?경고를 없애려면 부모를 설정하지 마십시오. –
예 경고를 제거해야합니다. – Haris
부모를 사용하여 다른 스레드의 개체를 삭제할 수 없습니다. 'deleteLater()'메소드를 사용하면 이벤트 대기열에서 객체를 삭제할 수 있습니다. –