创建初始helloworld项目项目名和路径都不能出现中文。
文本模式写入main.cpp代码。
#include<QApplication>
#include<QDialog>
#include<QLabel>
#include<QTextCodec>
int main(int argc,char* argv[])
{
QApplication a(argc,argv);//该类用以管理应用程序资源
QTextCodec::setCodecForTr(QTextCodec::codecForLocal());//提供文本码转换功能,否则中文将是乱码
//QTextCodec::codecForLocal使用系统指定字符集可更改
QDialog w;//建立对话框类
w.resize(400,300);//设置对话框的高和宽
QLabel label(&w);
label.move(120,120);//设置对话框的起始位置
label.setText(QObject::tr("hello world!你好!"));//QObject::tr进行编码转换,对应QTextCodec
w.show();//窗口显示
return a.exec();//使QApplication对象进入循环,等待接受产生的事件}
//
.ui后缀的界面文件,实质生成了隐藏头文件,文件名为ui_对象名.h。
如ui_hellodialog.h
ifndef UI_HELLODIALOG_H
#define UI_HELLODIALOG_H
#include<QtCore/QVariant>
#include<QtGui/QAction>
#include<QtGui/QApplication>
#include<QtGui/QButtonGroup>
#include<QtGui/QDialog>
#include<QtGui/QHeaderView>
#include<QtGui/QLabel>
QT_BEGIN_NAMESPACE
class Ui_HelloDialog{
public:
QLabel* label;
void setupUi(QDialog* HelloDialog) //该函数用以生成界面
{
if(HelloDialog->objectName().isEmpty())
HelloDialog->setObjectName(QString::fromUtf8("HelloDialog"));
HelloDialog->resize(400,300);
label=new QLabel(HelloDialog);
label->setObjectName(QString::fromUtf8("label"));
label->setGeometry(QRect(120,120,121,16));
retranslateUi(HelloDialog);//对窗口里的字符串进行编码转换
QMetaObject::connectSlotsByName(HelloDialog);//按照对象名进行信号和槽的关联