PDA

View Full Version : QFile not readable



seux
10th August 2011, 16:38
Hello out there,
i am trying to read from a file that the user chooses via a QFileDialog, but after that the file is not readyble...


file = new QFile(QFileDialog::getOpenFileName(....));
if(file.isReadable() != true)
{
//This gets executed
QMessageBox msg(QMessageBox::Information, "DEBUG", "File not readable", QMessageBox::Ok);
msg.show();
return;
}

The interesting thing is, that the messagebox never really appears. But when I start the programm in the debugger i can see step by step that the code in the if condition gets executed and that the messgebox is not complete drawn (Just a empty MsgBox appears without text, just the "DEBUG" title).

What is going wrong?

NullPointer
10th August 2011, 16:59
Try:



QString filename=QFileDialog::getOpenFileName(....);
if(filename.isEmpty()) return;
file = new QFile(filename);
file->open(QIODevice::ReadOnly); // Use ReadWrite if you want to write on it...
if(file.isReadable() != true)
{
//This gets executed
QMessageBox msg(QMessageBox::Information, "DEBUG", "File not readable", QMessageBox::Ok);
msg.show();
return;
}

Here: QFile and QIODevice

Hth.

stampede
11th August 2011, 19:55
Of course you cannot see the message box:


{
QMessageBox msg(QMessageBox::Information, "DEBUG", "File not readable", QMessageBox::Ok);
msg.show();
return; // message box is destroyed here
}

Message box is deleted immediately after creation, use "exec()" instead of "show()". Or try with static methods:

{
QMessageBox::information(this,"DEBUG", "File not readable"); // blocks until message box is closed
return;
}