Qt5简单例子和中文乱码问题

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include 
#include 

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(OnMyMsg()));

}
//Qt5简单例子和中文乱码问题
void MainWindow::OnMyMsg()
{
    qDebug()<<"test";
    qDebug()<<"test line";
    qDebug()<toUnicode("我要中文显示!"));

    QMessageBox::information(this, QString::fromLocal8Bit("提示"), QString::fromLocal8Bit("中文提示"));

     QString s1 = "汉语";
     QMessageBox::information(this, "提示", s1);
     QString s2("漢語");
     QMessageBox::information(this, "提示", s2);

     QString s3 = tr("中文");
     QMessageBox::information(this, "提示", s3);


     QString s5 = QString::fromUtf8("中文");
     QMessageBox::information(this, "提示", s5);
}

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

#include "mainwindow.h"
#include 
#include 

int main(int argc, char *argv[])
{
    //QTextCodec::setCodecForLocale(QTextCodec::codecForLocale());
    //QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));

    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

    //QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));

    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void OnMyMsg();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

你可能感兴趣的:(QT,编程,c/c++)