Results 1 to 9 of 9

Thread: reqister bool variable into main.cpp

  1. #1
    Join Date
    Jan 2019
    Posts
    21
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default reqister bool variable into main.cpp

    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/...l-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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: reqister bool variable into main.cpp

    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,
    _

  3. #3
    Join Date
    Jan 2019
    Posts
    21
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: reqister bool variable into main.cpp

    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
    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!
    Last edited by shokarta; 26th March 2019 at 19:21.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: reqister bool variable into main.cpp

    One thing you could also have a look at is the QML/JavaScript XMLHTTPRequest API https://doc.qt.io/qt-5/qtqml-javascr...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,
    _

  5. #5
    Join Date
    Jan 2019
    Posts
    21
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: reqister bool variable into main.cpp

    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-ap...VAvWJNVRnsJch0

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

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: reqister bool variable into main.cpp

    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,
    _

  7. #7
    Join Date
    Jan 2019
    Posts
    21
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: reqister bool variable into main.cpp

    Back to the original question....
    regarding the registering custom bool from https://stackoverflow.com/questions/...l-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!

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: reqister bool variable into main.cpp

    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.

    Qt Code:
    1. class UrlChecker : public QObject
    2. {
    3. Q_OBJECT
    4. Q_PROPERTY(bool reachable READ reachable NOTIFY reachableChanged)
    5.  
    6. public:
    7. bool reachable() const; // property getter function
    8.  
    9. signals:
    10. void reachableChanged(); // property change notification signal
    11. };
    To copy to clipboard, switch view to plain text mode 

    Such a class can then be registered with the QML type system
    Qt Code:
    1. qmlRegisterType<UrlChecker>("MyTypes", 1, 0, "UrlChecker");
    To copy to clipboard, switch view to plain text mode 

    And used in QML like any other type
    Qt Code:
    1. import QtQuick 2.0
    2. import MyTypes 1.0
    3.  
    4. Item {
    5. UrlChecker {
    6. id: checker
    7. }
    8.  
    9. Text {
    10. text: checker.reachable ? "URL reachable" : "URL not reachable"
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  9. #9
    Join Date
    Jan 2019
    Posts
    21
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: reqister bool variable into main.cpp

    This was extremly helpfull!

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

    Thank you a lot!

Similar Threads

  1. Replies: 3
    Last Post: 6th February 2019, 18:07
  2. Replies: 2
    Last Post: 19th October 2017, 23:25
  3. Problem with variable in main.cpp and mainwindow.cpp
    By adis1001 in forum Qt Programming
    Replies: 13
    Last Post: 14th July 2015, 10:03
  4. Replies: 1
    Last Post: 20th January 2012, 04:39
  5. main.cpp variable access question
    By MarkoSan in forum Qt Programming
    Replies: 10
    Last Post: 10th March 2008, 20:48

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.