2013年9月5日星期四

Sister shame Q : QT console program how output Chinese characters ?

Such as the following procedures, the console output is all garbled, how to run ?
#include <iostream>
using namespace std;
#include  <stdlib.h>

struct TreeNode
{
  int data;
  TreeNode *leftchild,*rightchild;
  TreeNode(int x,TreeNode *rc=NULL,TreeNode *lc=NULL){data=x;leftchild=lc;rightchild=rc;}
};
class Tree
{
public:
Tree(int value=0,TreeNode *p=NULL){end=value;root=p;}
void outPut(TreeNode *p);
void Plant(TreeNode *p);
void Treetest();
private:
TreeNode *root;
int end;
};
void Tree::outPut(TreeNode *p)
{
if(p!=NULL)
{
if(p->leftchild!=NULL)
{outPut(p->leftchild);}
else
{
cout<<"左子树空"<<endl;
}
p->data++;
cout<<p->data<<endl;
       if(p->rightchild!=NULL)
{outPut(p->rightchild);}
else
{
cout<<"右子树空"<<endl;
}
  }
else
{
 cout<<"树空"<<endl;
}
}
void Tree::Plant(TreeNode *p)
{
  int item;
  cin>>item;
  if(item!=end)
  {
  p=new TreeNode(item);
  if(p!=NULL)
  {
   Plant(p->leftchild);
   Plant(p->rightchild);
  }
  else
  {
    cerr<<"存储分配错误!"<<endl;
exit(1);
  }
  }
  else
  {
    p=NULL;
  }
}
void Tree::Treetest()
{
   int i;
   cout<<"请输入指令:1.建立二叉树;2.对二叉树操作;3.退出;"<<endl;
   cin>>i;
   if(i==1)
   {
   Plant(root);
   }
   if(i==2)
   {
     outPut(root);
   }
   if(i==3)
   {
     exit(1);
   }
}
int main()
{
Tree t1;
int i=10;
while(i!=100)
{
t1.Treetest();
}
return 0;
}

------ Solution ------------------------------------- -------
what you use compilers , gcc?
This statement does not matter with Qt estimated with the compiler about
I used vc compiler , shows Chinese
------ Solution ------------------------- -------------------
you this code has nothing to do with Qt , is a Chinese input and output of gcc compiler problem
look at this should be solved
http://www.annhe.net/article-1407.html

recommend it, since the use Qt, for all the good uses Qt , Qt input and output is very powerful , you get an entire standard C + + code to take over the running , to learn Qt adverse
- ---- Solution -------------------------------------------- < br> =. =
little relationship with Qt , this and your source file encoding formats and compiler of a relationship.

can give you a suggestion to the above program into Qt version , haha. Steps are as follows ~
1 is qtcreator
2 If it is , check the
Tools - Options - Text Editor - Behavior tab under File Encodings in , default Encoding drop-down box there are no Chinese encoding (gbk, gb2132 or gb18030 , etc. ) . I have here is system

3 in the program
int main (xxxx)
{
QApplication app (xxxx);
QTextCodec :: setCodecForTr (QTextCodec :: codecForName ("system"));
QTextCodec :: setCodecForLocale (QTextCodec :: codecForName ("system"));
QTextCodec :: setCodecForCStrings (QTextCodec :: codecForName ("system"));

qDebug () << QObject :: tr (" Chinese " ) ;
/ / remember the Chinese use QObject :: tr () wrapped up, so you can display Chinese.

app.exec ();
}
------ Solution ----------------------------------- ---------

is QT even the most standard C + + do not support ? ? ?   
 

Just tried it,
I'm here with QtCreator can output Chinese . The procedure is simple
# include
# include
using namespace std;

int main (int argc, char * argv [])
{
cout << " Chinese " ;
}

you put Tools - Options - Text Editor - Behavior tab under File Encodings in , default Encoding
try to change system , provided that your system is the Chinese system ha.

Then once you reopen the file , see if it is garbled, if it is to re- enter the Chinese garbled enough.
then ctrl + R


------ Solution ------------------------------------ --------
qt tab you look there are no encoding settings. To double-byte coding should be fine
------ Solution ----------------------------- ---------------

------ For reference only ------------------------------- --------
your output using cout, with Qt little , add this one

#include <locale.h>

locale::global(locale(""));

------ For reference only ----------------------------------- ----
error : expected constructor, destructor, or type conversion before '(' token
------ For reference only --- ------------------------------------
locale :: global (locale ("" ) ) ;
phrase placed inside the main function which
------ For reference only -------------------------- -------------

ah, ah put inside or distortion output


------ For reference only ---------------------------------- -----

using MINGW compiler

------ For reference only ---------------------------------- -----
Qt4 If so, set the
# include
QTextCodec :: setCodecForTr (QTextCodec :: codecForName ("GBK"));
QTextCodec :: setCodecForLocale (QTextCodec :: codecForName ("GBK"));
QTextCodec :: setCodecForCStrings (QTextCodec :: codecForName ("GBK"));


on the main inside
------ For reference only ------------------------------ ---------

tried, how output also are garbled ah ?
------ For reference only ---------------------------------------

Is QT even most standard C + + do not support ? ? ?


------ For reference only ---------------------------------- -----
vest ?
------ For reference only -------------------------------------- -
11 F :
qDebug () << QObject :: tr (" Chinese " ) ;
/ / remember the Chinese use QObject :: tr () wrapped up, so you can display Chinese.

I side with qDebug () can be directly output Chinese , no QObject :: tr

没有评论:

发表评论