• Qt QTextStream例子


    widget.h

     1 #ifndef WIDGET_H
     2 #define WIDGET_H
     3 
     4 #include <QWidget>
     5 
     6 namespace Ui {
     7 class Widget;
     8 }
     9 
    10 class Widget : public QWidget
    11 {
    12     Q_OBJECT
    13 
    14 public:
    15     explicit Widget(QWidget *parent = 0);
    16     ~Widget();
    17 
    18 private slots:
    19     void on_but_read_clicked();
    20 
    21     void on_but_wirte_clicked();
    22 
    23     void on_fun_clicked();
    24 
    25 private:
    26     Ui::Widget *ui;
    27 };
    28 
    29 #endif // WIDGET_H

    widget.cpp

      1 #include "widget.h"
      2 #include "ui_widget.h"
      3 #include <QFile>
      4 #include <QFileDialog>
      5 #include <QTextStream>
      6 #include <QDebug>
      7 //宏定义qDebug() 改为 cout,并且修改其输出格式化为:  [...widget.cpp:88] 输出内容
      8 #define cout qDebug() << "[" << __FILE__ << ":" << __LINE__ << "]"
      9 //__FILE__  : 当前执行该代码的文件路径。 __LINE__ : 当前输出代码的行数
     10 
     11 Widget::Widget(QWidget *parent) :
     12     QWidget(parent),
     13     ui(new Ui::Widget)
     14 {
     15     ui->setupUi(this);
     16 }
     17 
     18 Widget::~Widget()
     19 {
     20     delete ui;
     21 }
     22 
     23 
     24 //读数据
     25 void Widget::on_but_read_clicked()
     26 {
     27     QString path = QFileDialog::getOpenFileName(
     28                 this,
     29                 "标题",
     30                 "../",
     31                 "TXT(*.txt)");
     32 
     33     QFile file(path);
     34 
     35     file.open(QIODevice::ReadOnly);
     36 
     37     QTextStream stream(&file);
     38     stream.setCodec("UTF-8");
     39     
     40     QString str;
     41     //文件没有读完
     42     while(stream.atEnd() == false)
     43     {
     44         //读取一行数据
     45          str = stream.readLine();
     46          //将读取的数据显示在textEdit
     47          ui->textEdit->append(str);
     48          //不指定编码的时候需要下面一些转换
     49          //cout << str.toUtf8().data();
     50     }
     51     file.close();
     52 
     53 }
     54 
     55 //写数据
     56 void Widget::on_but_wirte_clicked()
     57 {
     58     QString str = ui->textEdit->toPlainText();
     59 
     60     QString path = QFileDialog::getOpenFileName(
     61                 this,
     62                 "标题",
     63                 "../",
     64                 "TXT(*.txt)");
     65 
     66     QFile file(path);
     67 
     68     file.open(QIODevice::WriteOnly);
     69     
     70     QTextStream stream(&file);
     71     
     72     stream.setCodec("UTF-8");
     73     
     74     stream << str;
     75     file.close();
     76     ui->textEdit->setText("");
     77 }
     78 
     79 
     80 //写数据
     81 void Widget::on_fun_clicked()
     82 {
     83     QString str = ui->textEdit->toPlainText();
     84 
     85     QString path = QFileDialog::getOpenFileName(
     86                 this,
     87                 "标题",
     88                 "../",
     89                 "TXT(*.txt)");
     90     //创建QFile对象
     91     QFile file(path);
     92     //打开方式
     93     file.open(QIODevice::ReadWrite);
     94     //指定设备
     95     QTextStream stream(&file);
     96     //设置编码
     97     stream.setCodec("UTF-8");
     98     //设置字符串宽度
     99     stream.setFieldWidth(30);
    100     //设置对其齐格式
    101     stream.setFieldAlignment(QTextStream::AlignRight);
    102     //设置填充字符
    103     stream.setPadChar('*');
    104     //写入数据
    105     stream << str;
    106     //关闭文件
    107     file.close();
    108     //清空textEdit
    109     ui->textEdit->setText("");
    110 }

    其他的没有贴出来说明是没有变的。

    UI界面:

     

  • 相关阅读:
    ZOJ 3490 String Successor
    ZOJ 3465 The Hive
    ZOJ 3077 Move to Baggage Office
    ZOJ 2836 Number Puzzle
    POJ 2115 C Looooops
    ZOJ 3605 Find the Marble
    扩展欧几里德
    搭配飞行员(网络流)
    最小费用流
    最大流
  • 原文地址:https://www.cnblogs.com/ybqjymy/p/14731578.html
Copyright © 2020-2023  润新知