I want to received image through using TCP socket and display in QMl window.
currently :I am able to receive .png image from socket. and : able to display the image from local disk in qml using image provider. but while integrating the both things i am facing the issue. how to call the socket's function.
tcpserver.h
class MyTcpServer : public QObject
{
Q_OBJECT
public:
explicit MyTcpServer(QObject *parent = 0);
signals:
public slots:
void newConnection();
void on_readyRead();
private:
QTcpServer *server;
};```
```
tcpserver.cpp
QByteArray array,array1;
MyTcpServer::MyTcpServer(QObject *parent) :
QObject(parent)
{server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()),
this, SLOT(newConnection()));
connect(server,SIGNAL(readyRead()),this,SLOT(readyRead()));
if(!server->listen(QHostAddress::Any, 9999))
{
qDebug() << "Server could not start";
}
else
{
qDebug() << "Server started!";
}
}
void MyTcpServer::newConnection()
{
QTcpSocket *socket = server->nextPendingConnection();
if(socket)
{
connect(socket ,SIGNAL(readyRead()),this,SLOT(on_readyRead()));
socket->write("Hello client
");
socket->flush();
socket->waitForBytesWritten(3000);
}
}
void MyTcpServer::on_readyRead()
{
QTcpSocket * server = dynamic_cast<QTcpSocket*>(sender());
qDebug()<<"reading..";
if(server)
{
qDebug()<<"Array size " << array.size() ;
array.append(server->readAll());
if(array.size()<230400)
{
qDebug()<<"Array size " << array.size() ;
}
else {
qDebug()<<"Array size else " << array.size() ;
cv::Mat img,img1;
cv::Size const frame_size(240,320);
img = cv::Mat(240,320, CV_8UC3,array.data());
qDebug()<<"Image type " <<img.type();
qDebug()<<"Array size " << array.size() ;
qDebug()<<"beforeImage size size " << img.dims;
cv::imshow("image display",img);
cv::waitKey(5000);
QImage imdisplay((uchar*)img.data, img.cols, img.rows, img.step, QImage::Format_RGB888);
}
}
}
//socket->close()
}
```
imageprovider.cpp
QImage QmlImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
printf("In request function ..
");
MyTcpServer *m = new MyTcpServer();
m->newConnection();
printf("after connectrion....");
QImage image1= m->on_readyRead();
printf("after request....");
return image;
}
```
question from:
https://stackoverflow.com/questions/65937095/received-image-from-socket-and-display-in-qml 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…