Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Japanese
  4. Qt上でCSVを作成しTextEditに表示したい
Qt 6.12 LTS is out! See what's new in the release blog

Qt上でCSVを作成しTextEditに表示したい

Scheduled Pinned Locked Moved Solved Japanese
3 Posts 2 Posters 6.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Toju
    wrote on last edited by
    #1

    お世話になっております。

    今回Qt上でCSVファイルを作成したものをTextEditに表示をさせたいのですが、
    その際に日本語・漢字が文字化けた状態で表示されてしまいます。

    実際のコードは以下のようになります。
    ・ファイルの作成
    下記の「s」は引数に設定しております。
    出力したい文字列を「s」に格納し下記の処理をしています。

        QString File = (PATH);
        QFile textfile(File);
        if (QFile::exists(File)) textfile.open(QIODevice::Append); 
    	else textfile.open(QIODevice::WriteOnly);
    	QTextStream t(&textfile);
        t.setCodec(QTextCodec::codecForName("utf-8"));
        t << s;
    	textfile.close();
    

    ・ファイルの読み込み・TextEditに読み込み

            QString file =(PATH);
            QFile textfile(file); 
            if (!textfile.open(QIODevice::ReadOnly)) {
                QString errStr = "File Open Error:" + textfile.errorString();
                qDebug() << errStr;
                return;
            }
            QTextStream in(&textfile);
            QString Comment = QString::fromLocal8Bit("");
    
            while (!in.atEnd()) {
                QString ReadTx = in.readLine();
                QStringList list = ReadTx.split(",");
                Comment += QString("%1 %2 %3 %4\r\n").arg(list[0]).arg(list[1]).arg(list[2]).arg(list[3]);
            }
            textfile.close();
            ui->textEdit_3->setText(Comment);
    

    少々わかりにくいコードではありますが
    ご教授いただければ幸いです。

    Qt Ver:4.8.4
    Qt Creator Ver:2.7.0
    Qtのversionですが、諸事情によりアップデートは出来ません。

    よろしくお願いいたします。

    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      YAMAMOTO Atsushi
      wrote on last edited by YAMAMOTO Atsushi
      #2

      ファイルを読み込む時にもコーデックを指定する必要がありますね。

      QTextStream in(&textfile);
      in.setCodec(QTextCodec::codecForName("utf-8"));

      です。

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Toju
        wrote on last edited by
        #3

        ご返信ありがとうございました。

        無事出力することが出来ました。
        また何かありましたらよろしくお願いいたします。

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt
        • Unsolved