QProcess to update android system date/time locks GUI



  • Hello,

    I have an interesting issue that I have no idea how to tackle. I've tried several methods to change an android's system date/time and each method, when successful, locks my GUI for a solid minute. I see that the change happens instantly because of the android status bar reflecting the changed time. But, it doesn't explain why my GUI would get locked up.

    Here's the code to change the system date/time

    CallSystemFn(QString("su -c date -s ").append(altDateTime.toString("yyyyMMdd.hhmmss")))
    

    The [CallSystemFn(QString cmdStr)] code has been many different versions:

    /*  Version 1  */
    FILE *cmdFile = popen(cmdStr.toStdString().c_str(), "r");
    //Ensure a system output file exists
    if(cmdFile) {
        //Read the system output file
        char tmpBuffer[1024];
        while(fgets(tmpBuffer, sizeof(tmpBuffer), cmdFile) != NULL)
            outputStr.append(tmpBuffer);
        //Trim the last character from the string - it's a new line character
        outputStr.remove(outputStr.length()-1, 1);
    
        //Close the output data file
        pclose(cmdFile);
    }
    
    /*  Version 2  */
    QProcess cmdFile;
    cmdFile.start(cmdStr);
    cmdFile.waitForFinished();
    outputStr.append(cmdFile.readAll());
    cmdFile.close();
    
    /*  Version 3  */
    QProcess::startDetached(cmdStr);
    

    I have thought about running a QThread object to set the system date/time, but that's what "Version 3" pretty much does. Has anyone ran into a similar problem and if so, how did you solve it?

    Thanks to all for their help!


  • Moderators

    So, even the third version locks up the GUI?



  • @sierdzio
    Yes, even version 3, locks up the GUI. It's the strangest thing right?! My next approach is to create a QThread object that can run simultaneously to the GUI thread, to change the system date/time. I am hoping this idea won't cause any issues to my GUI thread. Do you think this would be a good attempt? Can you offer any other suggestions I should try first?

    Thanks!


  • Moderators

    Yes, it is very weird that a separate process can block the GUI.

    Since this is about changing the date, maybe you could use some native Android calls (in Java), and invoke them through JNI? Qt has some convenience functions to help you with that, see the QtAndroidExtras module.



  • After much digging around, I found out that it's not setting the system Date/Time that blocks the GUI thread. It's my QTimer's that are the culprit. It seems, when these objects are created, they take on the system's date/time stamp and any modification of the system date/time doesn't alter these original stamps. SO, when I'm waiting for my QTimers to fire off, they end up playing a "catch up" game with the altered system's date/time - hence the locked GUI that I've been experiencing.

    I hope this sheds some light for anyone else who runs into this same problem.


  • Moderators

    Congrats on solving this issue! Or at least finding the root cause.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.