PDA

View Full Version : reqister bool variable into main.cpp



shokarta
26th March 2019, 05:31
Hello guys,
I was searching for the way how to check webpage availability, and the best solution i have found here:
https://stackoverflow.com/questions/28494571/how-in-qt5-to-check-if-url-is-available

howeve, i dont realy know how to make this c++ function to retrieve true/false on boolean works....

could someone walk me through how to registrer this bool variable? as far as its onle variable, can i use main.cpp without using any other external cpp file?

and then how to retrieve value from this bool variable via qml, i guess this is not correct:
property bool urlExists("https://google.com") is not correct realy... (i also shoul dinclude something in main.qml i guess).

Thanks guys :)

anda_skoa
26th March 2019, 07:09
The suggestion on StackOverflow is way to low level, there is QNetworkAccessManager for doing HTTP calls such as "HEAD".

In any case the part of your question regarding integration with QML has many different options, so it depends a lot on how your program needs to use this.

Is it something done on startup or during program operations?
Is it a single URL or multiple?
What effect should the availability status have on the program?

Cheers,
_

shokarta
26th March 2019, 19:05
Thanks for quick reply and for suggestion, I will check QNetworkAccessManager later on,

however, to reply on your questions:

Is it something done on startup or during program operations?
- this is something i didnt think abou before, but basicaly in my case of need, there will be timer of 1min, every tick should update the bool variable, on at any case i ask for update out of timer (good question tho, how will I ask for update?)
Is it a single URL or multiple?
- single url only, I need to check one specific url, it it passes, then do something, if not, do not do anything :D
What effect should the availability status have on the program?
- well, i guess it makes sence if the url status is 404 (as not reachable for whatever reason) then pass as false, anything else (2xx or 3xx) it can pass

EDIT:
I have checked the QNetworkAccessManager and it seems as owesome tool to get my needs done!
However, I am facing the same problem as above...
I have zero experience with c++, and mainly how to implement...
I am quite good with qml/js inside qt, however how c++ works in qml is fully out of my understanding,
therefore if I can kindly ask you to guide me step by step in blank qt how to do what i need with QNetworkAccessManager?

Thanks a lot!

anda_skoa
27th March 2019, 07:28
One thing you could also have a look at is the QML/JavaScript XMLHTTPRequest API https://doc.qt.io/qt-5/qtqml-javascript-qmlglobalobject.html#xmlhttprequest

This use case, however, would be a good opportunity to learn about the options for C++/QML integration.

A continuous background task that updates one or more values, potentially can be started/stopped/restarted from QML, potentially being configured from QML.

But it will require at least a base understanding of C++.

Cheers,
_

shokarta
27th March 2019, 17:08
Well I have tried:

function check() {
var xhr = new XMLHttpRequest();

xhr.onload = function () {
if (xhr.status >= 200 && xhr.status < 300) {
console.log('success!', xhr);
} else {
console.log('The request failed!');
}
console.log('This always runs...');
};
xhr.open('GET', 'https://jsonplaceholder.typicode.com/posts');
xhr.send();
}

from the link:
https://gomakethings.com/ajax-and-apis-with-vanilla-javascript/?fbclid=IwAR2Xtohkoz6TKtk7E-YCuR4vLSHbLe1C2IYrj-9a8RisKVAvWJNVRnsJch0

but I am getting this error:
qt.network.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed

anda_skoa
28th March 2019, 07:08
Hmm, that could point to an incomplete Qt installation, e.g. some SSL library missing, but it is really hard to tell.

Maybe try one of the Qt Examples that do web requests, e.g. something from "webengine" or "webenginewidgets" directories.

Cheers,
_

shokarta
30th March 2019, 11:11
Back to the original question....
regarding the registering custom bool from https://stackoverflow.com/questions/28494571/how-in-qt5-to-check-if-url-is-available

how to?

meaning:
- how header .h file should look like
- how source .cpp file should look like
- how should i modify the main.cpp
- and how to update and call this bool variable from qml file

if I can kindly ask you to write this so I can learn how all this is conected together, it would be way more usefull that different guides i found online.
this should be simple for you as pro :).

Thank you!

anda_skoa
31st March 2019, 10:52
One of the most central mechanisms in QML are properties and property bindings.

Thus one of the best ways to make a value, especially one that gets modified during runtime, accessible from C++ is to define a property of a C++ class.



class UrlChecker : public QObject
{
Q_OBJECT
Q_PROPERTY(bool reachable READ reachable NOTIFY reachableChanged)

public:
bool reachable() const; // property getter function

signals:
void reachableChanged(); // property change notification signal
};


Such a class can then be registered with the QML type system


qmlRegisterType<UrlChecker>("MyTypes", 1, 0, "UrlChecker");


And used in QML like any other type


import QtQuick 2.0
import MyTypes 1.0

Item {
UrlChecker {
id: checker
}

Text {
text: checker.reachable ? "URL reachable" : "URL not reachable"
}
}


Cheers,
_

shokarta
31st March 2019, 15:55
This was extremly helpfull!

After a little tweaks on the code above I made it work!

Thank you a lot!