QT-文件

笔记1年前 (2023)更新 Deepincode
868 0 0
QT-文件

QT-文件

界面

QT-文件

使用到的组件

  1. QLineEdit
  2. QPushButton
  3. QPlainTextEdit

实现功能

  1. 打开文件实现QLineEdit中显示文件地址
  2. QPlainTextEdit中显示文件内容

代码

读取文件

读取文件的所有内容

void MainWindow::on_pushButton_clicked()
{
    QString filename = QFileDialog::getOpenFileName(this, "打开一个txt文件", "","TXT (*.txt)");
    qDebug() << "filename: " << filename;

    // 判断文件名是否为空,为空直接退出
    if(filename.isEmpty())
    {
        return;
    }

    this->ui->lineEdit->setText(filename);

    // 使用QFile读取文件
    // 打开文件
    QFile file(filename);
    file.open(QIODevice::ReadOnly);
    QByteArray byteArr = file.readAll();

    // 将QByteArray转化成QString
    QString content = byteArr;

    // 如果需要编码
    // QTextCodec *codec = QTextCodec::codecForName("gbk");
    // QString content = codec->toUnicode(byteArr);

    // 输出到edit上
    ui->plainTextEdit->setPlainText(content);

    // 关闭文件
    file.close();

}

按行读取文件内容

void MainWindow::on_pushButton_clicked()
{
    QString filename = QFileDialog::getOpenFileName(this, "打开一个txt文件", "","TXT (*.txt)");
    qDebug() << "filename: " << filename;

    // 判断文件名是否为空,为空直接退出
    if(filename.isEmpty())
    {
        return;
    }

    this->ui->lineEdit->setText(filename);

    // 使用QFile读取文件
    // 打开文件
    QFile file(filename);
    file.open(QIODevice::ReadOnly);

    QByteArray byteArr;
    do
    {
        // 单行读取
        byteArr += file.readLine();
    } while (!file.atEnd());

    // 将QByteArray转化成QString
    QString content = byteArr;

    // 输出到edit上
    ui->plainTextEdit->setPlainText(content);

    file.close();

}

写入文件

    QFile file("D:\\test001.txt");
    file.open(QIODevice::WriteOnly|QIODevice::Append);
    file.write("喝了咯");
    file.close();
Constant Value Description
QIODeviceBase::NotOpen 0x0000 The device is not open.
QIODeviceBase::ReadOnly 0x0001 The device is open for reading.
QIODeviceBase::WriteOnly 0x0002 The device is open for writing. Note that, for file-system subclasses (e.g. QFile), this mode implies Truncate unless combined with ReadOnly, Append or NewOnly.
QIODeviceBase::ReadWrite ReadOnly | WriteOnly The device is open for reading and writing.
QIODeviceBase::Append 0x0004 The device is opened in append mode so that all data is written to the end of the file.
QIODeviceBase::Truncate 0x0008 If possible, the device is truncated before it is opened. All earlier contents of the device are lost.
QIODeviceBase::Text 0x0010 When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32.
QIODeviceBase::Unbuffered 0x0020 Any buffer in the device is bypassed.
QIODeviceBase::NewOnly 0x0040 Fail if the file to be opened already exists. Create and open the file only if it does not exist. There is a guarantee from the operating system that you are the only one creating and opening the file. Note that this mode implies WriteOnly, and combining it with ReadWrite is allowed. This flag currently only affects QFile. Other classes might use this flag in the future, but until then using this flag with any classes other than QFile may result in undefined behavior. (since Qt 5.11)
QIODeviceBase::ExistingOnly 0x0080 Fail if the file to be opened does not exist. This flag must be specified alongside ReadOnly, WriteOnly, or ReadWrite. Note that using this flag with ReadOnly alone is redundant, as ReadOnly already fails when the file does not exist. This flag currently only affects QFile. Other classes might use this flag in the future, but until then using this flag with any classes other than QFile may result in undefined behavior. (since Qt 5.11)

摘选自:https://doc.qt.io/qt-6/qiodevicebase.html#OpenModeFlag-enum

© 版权声明

相关文章

暂无评论

暂无评论...