QT二进制流方式读写文件

ui内容为两个按钮加一个文本编辑控件,以字节流方式保存一个文件,然后以流方式打开并通过转码显示。

#include "mywidget.h"
#include "ui_mywidget.h"
#include 
MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyWidget)
{
    ui->setupUi(this);
    ui->textEdit->hide();
}

MyWidget::~MyWidget()
{
    delete ui;
}

void MyWidget::on_pushButton_clicked()
{
    QString path = QFileDialog::getSaveFileName(this,"保存文件","/myfile/code/qt/","txt (*.txt)");
    if(path.isEmpty() == false)
    {
        QFile file(path);
        bool ret = file.open(QIODevice::WriteOnly);
        if(ret == true)
        {
            QString str("主要看气质!");
            QDataStream stream(&file);
            //把文件内容通过流的方式写入
            stream<textEdit->show();
    QString path = QFileDialog::getOpenFileName(this,"打开文件","/myfile/code/qt/","txt (*.txt)");
    if(path.isEmpty() == false)
    {
        QFile file(path);
        bool ret = file.open(QIODevice::ReadOnly);
        if(ret == true)
        {
            //关联文件
            QDataStream stream(&file);
            int a;
            QString tmp;
            //将文件内容读出
            stream>>tmp>>a;
            //显示文件内容
            ui->textEdit->setText(tmp.toUtf8().data());
            //打印文件内容
            qDebug()<
#ifndef MYWIDGET_H
#define MYWIDGET_H

#include 
#include 
#include 
#include 
namespace Ui {
class MyWidget;
}

class MyWidget : public QWidget
{
    Q_OBJECT

public:
    explicit MyWidget(QWidget *parent = 0);
    ~MyWidget();

private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

private:
    Ui::MyWidget *ui;
};

#endif // MYWIDGET_H

你可能感兴趣的:(个人笔记,随笔,Qt,c++)