Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

is it possible to using QNetworkAccessManager in subclass of QGraphicsRectItem?



  • Hi everyone,

    I have the problem to using QNetworkAccessManager in QGraphicsRectItem.

    Here is my code snapshot

    ThRect.h

    class ThRect : public QObject, public QGraphicsRectItem
    {
    	Q_OBJECT
    
    public:
    	ThRect();
    	~ThRect();
    
    	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    
    	void mousePressEvent(QGraphicsSceneMouseEvent *e);
    	void mouseMoveEvent(QGraphicsSceneMouseEvent *e);
    	void hoverEnterEvent(QGraphicsSceneHoverEvent * e);
    	void hoverLeaveEvent(QGraphicsSceneHoverEvent * e);
    
    	void Prediction(QImage src);
    private:
    	QNetworkAccessManager *m_networkManager;
    
    	bool m_ishover = false;
    private slots:
    	void uploadDone(QNetworkReply*);
    };
    

    ThRect.cpp

    #include "ThRect.h"
    #include <QHttpMultiPart>
    #include <QBuffer>
    #include <QNetworkReply>
    #include <QCoreApplication>
    
    ThRect::ThRect()
    	:QObject()
    {
    	QGraphicsRectItem::QGraphicsRectItem();
    	setFlags(QGraphicsItem::ItemSendsGeometryChanges | QGraphicsItem::ItemIsFocusable);
    	m_networkManager = new QNetworkAccessManager();
    	//-----------------------console output-----------------------------
    	//
    	//QEventLoop: Cannot be used without QApplication
    	//QObject::connect: Cannot connect (null)::aboutToQuit() to QNativeWifiEngine::closeHandle()
    	//
    	setAcceptHoverEvents(true);
    	prepareGeometryChange();
    	connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(uploadDone(QNetworkReply*)));
    }
    
    ThRect::~ThRect()
    {
    	
    }
    
    void ThRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
    	//normal
    	setPen(QPen(Qt::blue, 4));
    	
    	//hover
    	if (m_ishover)
    	{
    		setPen(QPen(Qt::cyan, 4));
    	}
    
    	//focus
    	if (hasFocus())
    	{
    		setPen(QPen(Qt::yellow, 4));
    	}
    	QGraphicsRectItem::paint(painter, option, widget);
    }
    
    void ThRect::mousePressEvent(QGraphicsSceneMouseEvent *e)
    {
    	setFocus();
    	QGraphicsRectItem::mousePressEvent(e);
    }
    
    void ThRect::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
    {
    	QGraphicsRectItem::mouseMoveEvent(e);
    }
    
    void ThRect::hoverEnterEvent(QGraphicsSceneHoverEvent * e)
    {
    	m_ishover = true;
    	QGraphicsRectItem::hoverEnterEvent(e);
    }
    
    void ThRect::hoverLeaveEvent(QGraphicsSceneHoverEvent * e)
    {
    	m_ishover = false;
    	QGraphicsRectItem::hoverLeaveEvent(e);
    }
    
    
    
    void ThRect::Prediction(QImage src)
    {
    	QRect ROI(this->rect().x(), this->rect().y(), this->rect().width(), this->rect().height());
    	QImage img = src.copy(ROI);
    
    	QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    	QHttpPart imagePart;
    	imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
    	imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\"0.jpg\""));
    	QByteArray ba;
    	QBuffer buffer(&ba);
    	buffer.open(QIODevice::WriteOnly);
    	img.save(&buffer, "jpg"); // writes image into ba in PNG format
    	imagePart.setBody(ba);
    	multiPart->append(imagePart);
    
    	QUrl url("http://192.168.1.52:5000/upload");
    	QNetworkRequest request(url);
    
    	QNetworkAccessManager *networkManager = new QNetworkAccessManager(this);
    	QNetworkReply *reply = networkManager->post(request, multiPart); //Critical error detected c0000374 crash
    	qDebug() << QString(multiPart->boundary());
    	multiPart->setParent(reply); // delete the multiPart with the reply
    
    	connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(uploadDone(QNetworkReply*)));
    
    	
    }
    
    void ThCADxRect::uploadDone(QNetworkReply *reply)
    {
    	//reply->open(QIODevice::ReadOnly);
    
    	//// if the response is correct
    	//if (reply->error() == QNetworkReply::NoError)
    	//{
    	//	QByteArray str = (reply->readAll());
    	//	QString response = QString::fromUtf8(str.data(), str.size());
    	//	qDebug() << " re " << response;
    	//}
    	////error sever
    	//else
    	//	qDebug() << "error response server";
    	//qDebug() << reply;
    }
    

    Thansks in advance


  • Qt Champions 2019

    @FkZsd From software design point of view it does not make sense to have network functionality inside a GraphicsRectItem.
    Also you create a new QNetworkAccessManager in ThRect::Prediction instead of using the one you created in the constructor.



  • @FkZsd +1 to the reply from @jsulm but just to better understand your issue, what's your exact error...?



  • Thanks @jsulm and @Pablo-J-Rogina ,
    Finally I found the reason. It's caused by wrong lib include.
    I'm running at debug mode and using release mode lib.
    All I done is change Qt5Network.lib to Qt5Networkd.lib everything is well.

    Thanks you guys again.


Log in to reply