QT 5.5 - QNetworkReply empty data - qt

I already took at others questions but I didn't find an answer.
I have a problem to print HTML code I download with a QNetworkAccessManager.
I need to log into a website to retrieve this code.
I have a slot like this:
void Aims::slotRequestFinished(QNetworkReply* requestReply)
{
QString data = QString(requestReply->readAll());
qDebug() << data;
}
For the first two steps (connection), I can see the HTML code in the console.
The last step doesn't get any data. There is no redirection nor error.
Now, the stranger part is that when I change my code to show the page into a webview, qDebug doesn't show anything, but the code loaded is shown correctly in the webview.
void Aims::slotRequestFinished(QNetworkReply* requestReply)
{
QString data = QString(requestReply->readAll());
qDebug() << data;
ui->webView->setHtml(data);
}
Well, I can save the content into a file. But I would really like to understand why I can't see anything in qDebug

Related

Is there a way to read a line from a file and get pause for few seconds, during pause a function checks whether user enters the same word or not?

I am working to develop typing tutor project. i am unable to read a specific line from file and allow user to enter the word. so how can i fix this??
void MainWindow::on_pushButton_clicked()
{
QFile file("D:/programs/QT/file_handle/file.txt");
if(!file.open(QFile::ReadOnly | QFile::Text))
QMessageBox::warning(this,"title","file not open");
QTextStream in(&file);
while(!in.atEnd()){
line=in.readLine();
ui->textBrowser->setPlainText(line);
}
file.close();
}
void MainWindow::on_pushButton_2_clicked()
{
QString str=ui->textEdit->toPlainText();
if(line==str)
ui->label->setText("they are same");
else
ui->label->setText("they are not same");
}
i expect to pause and pass control to textEdit where user enters a word after reading the first line. but the actual output appears reading the final line of the file
You'll need to do this asynchronously.
Whether you read the whole file into a buffer / list or you keep the file open doesn't matter.
You would need to make file (or buffer / list) a member of MainWindow.
In on_pushButton_clicked() you would only read the first / next line and display it.

Delete or smart pointer failure

I'm using Taglib and writing the cover art to the mp3. The following code here works:
bool MediaHelper::AddCoverArt(const QString &media, const QString &image_file)
{
TagLib::MPEG::File mpeg(media.toStdString().c_str());
TagLib::ID3v2::Tag *id3v2Tag = mpeg.ID3v2Tag(true);
TagLib::ID3v2::AttachedPictureFrame *frame = new TagLib::ID3v2::AttachedPictureFrame;
frame->setMimeType("image/jpeg");
frame->setPicture(image.data());
id3v2Tag->addFrame(frame);
mpeg.save();
delete frame;
return true;
}
but once i leave the function the app crashes with read access violation
I then tried it with a QScopedPointer:
bool MediaHelper::AddCoverArt(const QString &media, const QString &image_file)
{
TagLib::ID3v2::Tag *id3v2Tag = mpeg.ID3v2Tag(true);
QScopedPointer<TagLib::ID3v2::AttachedPictureFrame> frame(new TagLib::ID3v2::AttachedPictureFrame);
frame->setMimeType("image/jpeg");
frame->setPicture(image.data());
id3v2Tag->addFrame(frame.data());
mpeg.save();
return true;
}
But same thing happens when I leave the function. I'm kinda stumped because if I dont take care of deleting frame then I will be creating a big problem for myself. If anyone can give me some insight.
From the TagLib API documentation:
void TagLib::ID3v2::Tag::addFrame(Frame * frame)
Add a frame to the tag. At this point the tag takes ownership of the frame
and will handle freeing its memory.
The tag takes care of deleting the frame. If you delete the frame yourself as well, you end up with a double-delete, and if the tag accesses the frame before deleting it in its destructor, that would result in an access violation as well.

Constantly generating QPixmap* fails after a number of iterations

I am using the following code in order to generate QPixmap* pointers and then insert them into QHash<QString, QPixmap*> (I will show only the pointers generation code since this is the one that fails).
QPixmap* MyClass::loadImg(QString fileName)
{
QImage qimage(fileName);
if (qimage.isNull()) {
qDebug() << "Cannot load image " << fileName;
}
QPixmap *image = new QPixmap(fileName);
return image;
}
The problem that I have is the following:
For the first about 200 calls the method works fine - it is being called on a loop that iterates through the image files of a directory. Then suddenly the QPixmap* starts returning QPixmap(null) for no apparent reason.QImage is also null when that happens.
I have checked and made sure that the path is fine. Also, I have tried with various sets of images and the same always happens - it runs with no problems the ~200 calls and then starts generating nulls.
Any help would be appreciated.
Thank you.
Just don't create it on heap. QPixmap is implicitly shared.

QWebView not showing page when JS involved

I successfully managed to compile Qt 4 including WebKit for WinCE. QWebView seems to run fine - I can load a HTML5 document and it gets displayed.
ui.webView->page()->settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
ui.webView->setUrl(QUrl::fromLocalFile(QFileInfo(QLatin1String("test.html")).absoluteFilePath()));
However, as soon as the page contains any JavaScript (even an empty script-tag already causes the problem), QWebView will only display a blank page.
The QWebInspector window can also not be shown, as it also just displays a blank window.
I doubt this is a common problem, so my question is, how I could dig into that issue. Is there any debug log for QWebView?
Unfortunately, there's no exception or assertion failure being thrown while execution.
Any ideas? :)
Thank you in advance.
EDIT:
Here is the test code creating the problem. The first four lines of loadFinished are some test code to check valid pointers and whether I could call methods on QWebFrame at all. All code lines execute well until the evaluateJavaScript, where I get an access violation.
I could not post the HTML code inside setHtml, seems to be forbidden by Stackoverflow.
ce0::ce0(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
connect(ui.webView, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
ui.webView->page()->settings()->setAttribute(QWebSettings::LocalContentCanAccessRemoteUrls, true);
ui.webView->page()->settings()->setAttribute(QWebSettings::LocalContentCanAccessFileUrls, true);
ui.webView->page()->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
ui.webView->page()->settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
//ui.webView->setUrl(QUrl::fromLocalFile(QFileInfo(QLatin1String("test.html")).absoluteFilePath()));
ui.webView->setHtml(...);
}
void ce0::loadFinished(bool ok) {
QWebPage* p = ui.webView->page();
QWebFrame* f = p->mainFrame();
QString qs = f->title();
bool x = ui.webView->page()->settings()->testAttribute( QWebSettings::JavascriptEnabled );
f->evaluateJavaScript("document.getElementById(\"content\").innerHTML = \"Whatever\";");
}

Qt: opening qrc pdf with the poppler library

I'm having a bit of trouble with my function for displaying pdf's with the poppler library. The code below is the function in which the problem occurs.
const QString &file is the path to the file
int page is the page on which it has to open
When i set file to a real path (e.g. "/Users/User/Documents/xxx.pdf"), it is no problem to open it. But when i give the path to a qrc file (":/files/xxx.pdf"), it won't work. I want to use it for displaying a user manual for instance, within the application.
I've also tried first making a QFile out of it, opening it and doing readAll, then loading the QByteArray received by doingPoppler::Document::loadFromData(the qbytearray), but it errors already when opening the QFile in ReadOnly mode.
void class::setPdf(const QString &file, int page)
{
Poppler::Document *doc = Poppler::Document::load(file);
if (!doc) {
QMessageBox msgbox(QMessageBox::Critical, tr("Open Error"), tr("Please check preferences: cannot open:\n") + file,
QMessageBox::Ok, this);
msgbox.exec();
}
else{ /*Code for displaying the pdf, which works fine*/
}
}
I hope you can help me,
greetings,
Matt
I've also tried first making a QFile
out of it, opening it and doing
readAll, then loading the QByteArray
received by
doingPoppler::Document::loadFromData(the
qbytearray), but it errors already
when opening the QFile in ReadOnly
mode.
QFile f;
f.setFileName(":/skin/AppIcon16.png");
f.open(QIODevice::ReadOnly);
QByteArray r=f.readAll();
Perfectly reads all data from the resource, have checked it. So i suggest you did something wrong when tried that. Maybe path errors, maybe something else...

Resources