1

Reproducible example in QML:

main.cpp

#include <QSsl>

main.qml

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.0

Window {
    visible: true
    width: 640
    height: 480

    Component.onCompleted: getPage(logResults)

    function logResults(results) {
        console.log("RESULTS: " + results)
    }

    function getPage(callback) {
        var xhttp = new XMLHttpRequest();
        var url = "https://www.google.com/"

        xhttp.onreadystatechange = function() {
            if (xhttp.readyState === 4 && xhttp.status === 200) {
                console.log("calling callback")
                callback(xhttp.responseText)                
            }
        };
        xhttp.open("GET", url);
        xhttp.send();
    }
}

Output I get:

qml: calling callback

Output I expect:

qml: calling callback
qml: RESULTS: <the HTML located at https://www.google.com/>

The xhttp.responseText is not empty or undefined, in fact the console.log() throws no errors. Reformat the code like so:

        xhttp.onreadystatechange = function() {
            if (xhttp.readyState === 4 && xhttp.status === 200) {
                console.log("Am I alive?")
                console.log("response text: " + xhttp.responseText)
                console.log("I am alive")
            }
        };

then I get output:

qml: Am I alive?
qml: I am alive

Why does XMLHttpRequest responseText not exist? Why does it kill the console.log() and not throw an 'undefined' or some other error? Is there a better way to scrape the html from this web page?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Tyler M
  • 352
  • 4
  • 21
  • In linux with Qt 5.14 I get `qml: calling callback qml: RESULTS: – eyllanesc Dec 26 '19 at 01:08
  • Thanks, that's a big relief to know it's at least partially working. I'm running windows QT 5.12.6 ... QtWebEngine installed, i'm exposing SSL .dll files by placing them in the /debug and /release folders... if that might cause this issue please enlighten me. Otherwise, I'm installing 5.14 and I'll try it out on there – Tyler M Dec 26 '19 at 01:16
  • I recommend you find out what version of OpenSSL you need for the version of Qt you use. Do you get any error messages in the console? – eyllanesc Dec 26 '19 at 01:22
  • Originally I was getting an error until i dropped the .dll files into the debug and release folders ... now I've updated OpenSSL to version 1.1.1, then I set `INCLUDEPATH += C:\OpenSSL-Win64\bin` ... I also dropped the `libcrypto` and `libssl` .dll files from `\bin` into the /debug and /release folders. I'm getting error `qt.network.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed` – Tyler M Dec 26 '19 at 01:42
  • It's working in my Ubuntu 19.04 environment... I'll dig deeper into the windows OpenSSL, that seems to be the issue here – Tyler M Dec 26 '19 at 15:23
  • @TylerM and @eyllanesc: now that the OpenSSL issue has been addressed in the other question (and my bounty is collected :-) I'm going to answer also this one (which will require several edits) with the findings on the other question about `console.log()` and the goals of recording a good bug report for Qt and for other people suffering the same issue and looking here in the future (which is the *raison d'être* of SO). – Former contributor Jan 09 '20 at 07:54
  • This question is about `console.log()` failing in Windows, and not about `XMLHttpRequest`. I've tried to edit the question tags, but both @nKognito and @nick-a rejected the edit. Shame! – Former contributor Jan 09 '20 at 09:38

1 Answers1

2

In Windows 10 x86_64 with Qt Creator 4.11.0, this function in OP's main.qml does nothing at all:

function logResults(results) {
    console.log("RESULTS: " + results)
}

But it works perfectly on Linux. The problem is only in Windows. Nothing is printed in the output panel. No errors, no "qml: something". Nada. As a workaround, this alternative works:

function logResults(results) {
    console.log("RESULTS Length=", results.length);
    console.log("results.substr=", results.substr(0, 20)); 
}

It prints something at the "Application output" panel:

qml: RESULTS Length= 47932
qml: results.substr= <!doctype html><html

Digging a bit further, we can observe that this:

console.log("results.substr=", results.substr(0, 32000)); 

Works, but this fails again:

console.log("results.substr=", results.substr(0, 32800)); 

This suggest that there is a limit about 32K, and console.log() fails silently when the content is larger than this limit. But only in Windows. This smells like a bug somewhere in Qt...

Edit

It is not only a problem with console.log(), but in QtCreator's logging. This program fails to print all the output in the Application Output panel:

#include <QCoreApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QString str;
    str.fill('1', 32763);
    qDebug() << str;
    str.fill('2', 32764);
    qDebug() << str;
    return 0;
}

The '2's string is correctly displayed only when selecting Project > Run Settings > Run in terminal checkbox, but it is omitted when the checkbox is clear. Any message larger than 32763 characters is silenced in Application Output.

Bug report created: https://bugreports.qt.io/browse/QTCREATORBUG-23454

Former contributor
  • 2,466
  • 2
  • 10
  • 15