Qt4:日本語を扱う。


Qt4:百の定石


  • Qt は Unicode
  • Mac は Shift-JIS を使っている。ソースコードも当然 Shift-JIS で保存される。
  • 日本語は Unicode に変換するQObject::tr() を使って常にコーティングする必要がある。
  • QTextCodec?::setCodecForTr?() で tr() が何のコードから Unicode に変換するかを教える。
QTextCodec *codec = QTextCodec::codecForName("Shift-JIS");
QTextCodec::setCodecForTr(codec) ; // main 関数で定義する。
QString str = tr("ここは何処?") ;
QLabel* l = new QLabel(tr("私は誰?")) ;
  • ファイルに保存する時は QTextStream?::setCodec() でコードを指定する。
QFile myFile(”hoge”) ;
if( !myFile.open(QIODevice::WriteOnly| QIODevice::Text ) ){
   QMessageBox::information( 0, "FILE IO Error", "Couldn't openFile." ) ;
   return ;
}
QTextStream fout(&myFile) ;
fout.setCodec("Shift-JIS") ;   //マックではこれが標準
fout << tr("ここは何処") ;
  • ファイルを読む時も tr() でコーティングする。
QFile* myFile = new QFile(”hoge”) ;
if( !myFile->open(QIODevice::ReadOnly| QIODevice::Text) ){
   QMessageBox::information( 0, "FILE IO Error", "Couldn't openFile." ) ;
   return ;
}
QString str = tr(myFile->readLine()) ;

最新の20件

2020-12-25 2007-03-14 2006-10-30 2006-11-09 2007-03-14 2006-11-09 2006-11-10 2006-11-09 2008-05-20 2007-11-07 2007-11-01

今日の20件

  • counter: 1945
  • today: 1
  • yesterday: 0
  • online: 1