Important: Please read the Qt Code of Conduct -

Using QLockFile

  • Hi

    I am trying to use QLockFile in a function that receives a file path as a QString.

    When I pass the string directly to the constructor of QLockFile and the call tryLock it always fails.

    However, if I hard code the string the lock succeeds (although I get compiler warnings about unknown escape sequences)

    If I user the toNativeSeparators method of QDir on my file path the call to tryLock fails although in the debugger the value of the converted string appears to the same as the hard coded value.

    I would be grateful if someone could explain this behaviour

    FileDestination::FileDestination(const QString& filePath)
    QLockFile lockFile(filePath); // tryLock fails

    QString s = QDir::toNativeSeparators(filePath)
    QLockFile lockFile2(s); // tryLock fails
    QLockFile lockFile3("C:\jobs\Myjob.txt"); // tryLock succeeds bu get compiler warnings for unknown escape sequences


  • Lifetime Qt Champion


    You have two escape sequences here \j and \M which don't exist. You would have to double the backslashes to create a valid path.

    Out of curiosity why don't you use the unix notation since you are using Qt ?

  • Hi thanks for the reply
    I appreciate why I get the compile warnings, but this is the only version that works.
    The incoming filePath argument has unix notation but the call to tryLock fails

    This is confusing

Log in to reply