开发者

Opening a file from a Qt String

开发者 https://www.devze.com 2023-01-05 00:01 出处:网络
I am making a Qt applica开发者_JS百科tion and I have a button to open a file, which is connected to a custom slot. This is the slot code so far:

I am making a Qt applica开发者_JS百科tion 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.


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

精彩评论

暂无评论...
验证码 换一张
取 消