Generally, I've been searching for a while and could not find a serious answer. The problem is that I've a QString variable containing certain url, e.g. "C:/Users/Me/Desktop/image.png". How to open it and display the image in my application window?
I know the problem might seem trivial, however I can't find a working solution.
Load the image by using QPixmap and then show it with QLabel:
QString url = R"(C:/Users/Me/Desktop/image.png)";
QPixmap img(url);
QLabel *label = new QLabel(this);
label->setPixmap(img);
ImageViewer example
void LoadAvatar(const std::string &strAvatarUrl, QLabel &lable)
{
QUrl url(QString().fromStdString(strAvatarUrl));
QNetworkAccessManager manager;
QEventLoop loop;
QNetworkReply *reply = manager.get(QNetworkRequest(url));
QObject::connect(reply, &QNetworkReply::finished, &loop, [&reply, &lable,&loop](){
if (reply->error() == QNetworkReply::NoError)
{
QByteArray jpegData = reply->readAll();
QPixmap pixmap;
pixmap.loadFromData(jpegData);
if (!pixmap.isNull())
{
lable.clear();
lable.setPixmap(pixmap);
}
}
loop.quit();
});
loop.exec();
}
Related
In my project, I am attempting to download a YouTube profile picture. The YouTube API returns this link https://yt3.ggpht.com/-7ipuUvDjVT8/AAAAAAAAAAI/AAAAAAAAAAA/hSPOcUsb1nw/s240-c-k-no-mo-rj-c0xffffff/photo.jpg
However, using the same function for downloading files from my own website doesn't work with this URL. I get QNetworkReply::NetworkError(ProtocolUnknownError) in the application output.
Here is my code:
QByteArray downloadFileData(QString url)
{
QNetworkRequest request;
request.setUrl(QUrl(url));
QNetworkReply *reply = manager->get(request);
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
if(reply->error() == QNetworkReply::NoError)
{
return reply->readAll();
}
else
{
qDebug() << reply->error();
}
reply->deleteLater();
return "";
}
When I pass that url to this function, I get that error. What is going on?
Thanks for your time
So I have this code:
QUrl url("http://...");
QNetworkRequest request(url);
QNetworkReply *reply = m_networkManager->get(request);
connect(reply, SIGNAL(finished()), SLOT(onRequestCompleted()));
connect(reply,SIGNAL(error(QNetworkReply::NetworkError)),SLOT(onError(QNetworkReply::NetworkError)));
and I cant get signal to the other fuction
void IpResolver::onRequestCompleted()
{
QString webContent;
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
if (reply)
{
if (reply->error() == QNetworkReply::NoError)
{
QString webContent = reply->readAll();
}
}
}
I cant figure out the solution, help please.
I don't know what exactly you want, but:
Why do you use reply pointer instead of some kind onRequestCompleted(QNetworkReply *reply)?
If you do so:
QUrl url("http://...");
QNetworkRequest request(url);
connect(m_networkManager, &QNetworkAccessManager::finished, this, &IpResolver::onRequestCompleted);
m_networkManager->get(request);
And your slot will be, for example:
void IpResolver::onRequestCompleted(QNetworkReply *reply)
{
QString webContent;
if (reply->error() == QNetworkReply::NoError)
webContent = reply->readAll();
}
I saw an other thread talking about this, but I'm not succeeding in the display of my image.
Currently, I'm downloading my image like this :
void MyClass::imgHandle() {
QNetworkAccessManager *nam = new QNetworkAccessManager(this);
QUrl url(_code.c_str());
QNetworkReply* reply = nam->get(QNetworkRequest(url));
QEventLoop eventLoop;
connect(reply,SIGNAL(finished()),&eventLoop,SLOT(quit()));
eventLoop.exec();
if (reply->error() == QNetworkReply::NoError)
{
QImageReader imageReader(reply);
imageReader.setAutoDetectImageFormat (false);
_img = imageReader.read();
}
}
_code is built from a code got from a Json parsing, and the url looks like this : http://l.yimg.com/a/i/us/we/52/33.gif
_img is a QImage in my class.
And in my other class I do this :
int OtherClass::displayWeather()
{
MyClass mC = new MyClass;
mC->exec() // Where I get the code from the Json
QLabel *imgWeather = new QLabel(this);
imgWeather->setPixmap(QPixmap::fromImage(mC->getImg()));
// getImg() return a QImage.
//The QImage created in MyClass.
imgWeather->setGeometry(1700, 0, 120, 120);
}
And at the end .. Nothing is displayed !
You should check the QImageReader::read result:
QImageReader imageReader(reply);
imageReader.setAutoDetectImageFormat(false);
QImage _img = imageReader.read();
if (_img.isNull())
{
qDebug() << imageReader.errorString();
}
In your case the error is "Unsupported image format".
By default QImageReader tries to autodetect the image format and you've just disabled it by calling setAutoDetectImageFormat(false). Remove it and QImageReader will do the job.
I am trying to get the content of a HTTP request into a QString variable with Qt and C++
QNetworkAccessManager networkManager;
QUrl url("https://someurl.test.com/this-actually-exists");
QNetworkRequest request;
request.setUrl(url);
QNetworkReply* currentReply = networkManager.get(request); // GET
QString reply = QTextCodec::codecForMib(1015)->toUnicode(currentReply->readAll());
Still, the variable reply seems to stay empty. Obviously, I misunderstand the documentation. How do I get this to perform?
You can use two different ways even the synchronous or asynchronous ways to do this. The asynchronous way is :
connect (&networkManager , SIGNAL(finished(QNetworkReply*)) ,this, SLOT(done(QNetworkReply*)));
networkManager.get(request);
And you should read the contents from the returned reply in the slot connected to finished signal in the following way :
void net::done(QNetworkReply * reply)
{
if (reply->error() == QNetworkReply::NoError)
{
data = QString(reply->readAll ());
}
else
{
data = QString(reply->errorString ());
}
}
The synchronous way is like :
QNetworkReply *reply = networkManager.get(request);
QEventLoop loop;
connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), &loop, SLOT(quit()));
loop.exec();
QByteArray bts = reply->readAll();
QString str(bts);
Here you use an event loop to wait until the reply is finished and then read the available bytes and get the string.
I need to assume you're running an application with an event-loop in place? If not, then it's a bit harder...
If so, replace your last line that builds the reply QString:
connect(currentReply, SIGNAL(finished()), this, SLOT(gotAReply()));
Then you'll have to define another method in your class as a slot that gets triggered as soon as that reply got filled:
void gotAReply()
{
QNetworkReply *reply = qobject_cast<QNetworkReply*>(QObject::sender());
if (reply)
{
if (reply->error() == QNetworkReply::NoError)
{
QString replyText( reply->readAll() );
}
reply->deleteLater();
}
}
Don't forget: for Signals and Slot to work your class declaration must contain the Q_OBJECT macro.
I am trying to display the image obtained from the get request made using networkaccess manager. I am able to compile and even able to run it. but I am unable to show the image in a Qlabel.
QNetworkAccessManager* nam;
void MainWindow::on_pushButton_clicked()
{
nam = new QNetworkAccessManager(this);
QUrl url("http://i.imgur.com/Uw7Fk.jpg");
QNetworkReply* reply = nam->get(QNetworkRequest(url));
if (reply->error() == QNetworkReply::NoError)
{
QImageReader imageReader(reply);
imageReader.setAutoDetectImageFormat (false);
QImage pic = imageReader.read();
ui->label_2->setPixmap(QPixmap::fromImage(pic));
}
}
Please tell me where I am going wrong.
The data in QNetworkReply is not ready immediately after the call to QNetworkAccessManager::get(). The call is asynchronous, and you need to connect to either the finished() signal of QNetworkAccessManager, or readyRead() signal of QNetworkReply before you attempt to retrieve any data.
To get image synchronously, you can use QEventLoop like below:
QNetworkAccessManager* nam;
void MainWindow::on_pushButton_clicked()
{
nam = new QNetworkAccessManager(this);
QUrl url("http://i.imgur.com/Uw7Fk.jpg");
QNetworkReply* reply = nam->get(QNetworkRequest(url));
QEventLoop eventloop;
connect(reply,SIGNAL(finished()),&eventloop,SLOT(quit()));
eventLoop.exec();
if (reply->error() == QNetworkReply::NoError)
{
QImageReader imageReader(reply);
imageReader.setAutoDetectImageFormat (false);
QImage pic = imageReader.read();
ui->label_2->setPixmap(QPixmap::fromImage(pic));
}
}