None
None

Reputation: 3933

Opening a file from a Qt String

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.

Upvotes: 1

Views: 8200

Answers (1)

JimDaniel
JimDaniel

Reputation: 12703

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();

Upvotes: 8

Related Questions