Important: Please read the Qt Code of Conduct -

Robust re-fresh

  • Hi,

    I have a weather station and made a web page that shows the most important info in one page.

    I want to make an application that shows this web page, and re-freshes it every minute (or whatever
    is good), and continues to do this until the end of world. If the connection to the server is broken the
    app has to re-try forever. I have an old tablet that's going to be dedicated as a weather display, so this
    device will run this app (and nothing else).

    So far I have been using <meta http-equiv="refresh" content="60"> in the head-section of the web
    page (tried also all sorts of javascript approaches), but the re-fresh stops after XX hours, depending
    on how reliable the connection to the server is. I want something that works much more reliable.

    OK, creating an application that shows the web page was very easy, based on the Qt examples I found
    (disclaimer: this is my 1st Qt app, so I'm not an expert yet):

    @QT += webkit

    SOURCES += main.cpp@

    Above is the project file, below the source code:

    @#include <QApplication>
    #include <QWebView>
    #include <QUrl>

    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    QWebView view;

    view.load(QUrl(""));; /* or view.showMaximized(); */

    return a.exec();

    I have been looking for how to implement works-no-matter-what-and-still-it-works refresh, but
    failed to identify very straightforward way, which directorin should I look? QWebPage::Reload?


  • You can leave out the HTTP-META refresh and install a "QTimer": that fires every 60 seconds and calls slot "QWebView::reload() ": to refresh it:

    QTimer timer;
    connect(&timer, SIGNAL(timeout()), &view, SLOT(reload()));
    timer.setInterval(60 * 1000); // it's msecs!

    return a.exec();

    To make it reliable you can connect the timeout signal to a slot of your own, stop the timer there, reload the page and restart the timer once you're done. This avoids a reload flooding in case your weather station is not available or such.

  • Thanks, seems to work pretty well. Started the app, and renamed the web page, and web server log shows the app is trying to get the web page every minute just like I want.

    I'll test this for a while, and if this needs to be more reliable I'll follow your advice and implement my own slot, which uses loadFinished(bool ok) signal to check if the reload() slot was successful or not (since reload() returns nothing).

    Again, thanks for good and fast answer, Volker!


    PS I'll attach the modified source file in case somebody else needs similar thing; here the URL
    is not so hard-coded, and there is even a bit silly error message if the URL is empty (or user
    exits the QInputDialog::getText() with escape. Also added query for the reload interval.

    @#include <QApplication>
    #include <QWebView>
    #include <QInputDialog>
    #include <QErrorMessage>
    #include <QTimer>

    int main(int argc, char *argv[])
    bool ret; QString text="";
    QTimer timer; QApplication a(argc, argv); QWebView view; QErrorMessage error;

    int i=QInputDialog::getInt(0 /* parent */,
                               "web page reloader (wpr)",
                               "Enter refresh interval (1-60 minutes)", 5, 1, 60, 1,
                               &ret, 0 /* window flags */);
    text=QInputDialog::getText(0 /* parent */,
                               QObject::tr("Show web page - reload every %1 minute(s)").arg(i),
                               "Enter URL and press Enter to continue (or ESC to abort)",
                               QLineEdit::Normal, text, &ret, 0 /* window flags */);
    if(ret && !text.isEmpty())
        view.load(QUrl(text));; /* or view.showMaximized(); */
        QObject::connect(&timer, SIGNAL(timeout()), &view, SLOT(reload()));
        timer.setInterval(i * 60 * 1000); /* milliseconds */ timer.start();
        error.setFixedSize(400, 200); error.showMessage("Next time, then...");
    return a.exec&#40;&#41;;


Log in to reply