views:

136

answers:

1

I am making a Qt application and I have a button to open a file, which is connected to a custom slot. This is the slot code so far:

void MainWindow::file_dialog() {
    const QFileDialog *fd;
    const QString filename = fd->getOpenFileName();
}

How could I have it then convert the file name to a const char *, open the file, read it and store the text in a QString, and then close the file. I am using Qt4.

+4  A: 

To read the contents of a file, you can do this:

QString filename = QFileDialog::getOpenFileName();

QFile file(filename);
 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
     return;

QString content = file.readAll();

file.close();
JimDaniel