Results 1 to 2 of 2

Thread: QFile::OpenError on when file has write protection and on when opened

  1. #1
    Join Date
    Sep 2009
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QFile::OpenError on when file has write protection and on when opened

    Hi,
    Is there any way by which we can differentiate on when QFile::FileError returns QFile::OpenError.When the particular file is has write protection then also the file is returning this code and when the file is opened then also its returning QFile::OpenError ?

    Actually i have to check for write protection of file .Here is my code for your help

    QFile telegramFile(filePath);
    QFileInfo fileInfo(telegramFile);
    QTextStream telegramStream(&telegramFile);

    if(!fileInfo.isWritable())
    {
    QMessageBox *mBox = new QMessageBox();
    mBox->setText(QObject::tr("m_This File is write-protected.Do you want to overwrite it?"));
    mBox->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    mBox->setIcon(QMessageBox::Warning);
    mBox->setWindowTitle(QObject::tr("m_Warning"));

    switch(mBox->exec())
    {
    case QMessageBox::Yes:
    chk = telegramFile.setPermissions(QFile::WriteUser);
    break;

    case QMessageBox::No:
    return false;

    default:
    return false;
    }
    }

    if(!telegramFile.open(QIODevice::WriteOnly))
    {
    QMessageBox *mBox = new QMessageBox();
    int errorVal = telegramFile.error();
    mBox->setText(telegramFile.errorString());
    mBox->exec();
    telegramFile.close();
    return false;
    }
    if(telegramFile.isOpen())
    telegramStream << m_dataBuffer.toUtf8();


    Any help would be greatly appreciated.Thanks

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    507
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QFile::OpenError on when file has write protection and on when opened

    Hi, you can use QFileInfo::isWritable () to check if a file is writable by the user.

    Ginsengelf

    edit: did not read you full post...
    Last edited by Ginsengelf; 11th October 2010 at 10:22. Reason: updated contents

Similar Threads

  1. QFile write isn't stable
    By ruben.rodrigues in forum Newbie
    Replies: 2
    Last Post: 8th July 2010, 21:14
  2. QFile write and file modyfication time
    By Talei in forum Newbie
    Replies: 1
    Last Post: 9th May 2010, 20:46
  3. QFile - QDataStream read and write each character
    By nhs_0702 in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2010, 19:03
  4. possible open/write two files same time with QFile
    By npotency in forum Qt Programming
    Replies: 4
    Last Post: 15th November 2009, 00:28
  5. Unable to write to file QFile
    By cuter in forum Qt Programming
    Replies: 4
    Last Post: 15th July 2009, 11:19

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.