QT:图标 中文等


1. 应用程序图标

在Qt助手(assistant.exe)搜索关键字"Setting the Application Icon"就可以看到在各种平台设置Qt程序图标的方法。在windows下:

找个ICO文件,保存到一个程序文件夹下的某个地方,如:~/images/setup.ico下

用记事本创建一个.rc文件,文件名称随意,如:ico.rc,里面只写一句话  IDI_ICON1 ICON DISCARDABLE "images/setup.ico"

在工程文件 .pro 中加入一行 RC_FILE= ico.rc

 程序重新编译后,生成的.exe就有图标了。

2. 程序中引用图标文件

用记事本在应用程序下建立一个.qrc 文件,名称随意,如:pngsfile.qrc ,写入如下内容

<!DOCTYPE RCC><RCC version="1.0">
    <qresource>
     <file>qt_zh_CN.qm</file>
     <file>images/add.png</file>
     <file>images/modify.png</file>
     <file>images/clear.png</file>
     <file>images/over.png</file>
     <file>images/agather.png</file>
     <file>images/delete.png</file>
    </qresource>
</RCC>

在工程文件 .pro 中加入一行 RESOURCES = pngsfile.qrc

程序中引用时,比如设置窗体的图标:

this->setWindowIcon(QIcon(":/images/history.png"));


3. QT中显示中文

在qt的文件下找到qt_zh_CN.qm文件,复制到应用程序文件夹下,并添加到资源文件pngsfile.qrc中。

在main中添加引用,下面是添加后的样子:

#include <QApplication>
#include "myd.h"
#include <QTextCodec>  //qt支持中文
#include <QTranslator> //自动翻译中文

int main(int argc, char *argv[])
{
    //qt支持中文
    QTextCodec::setCodecForTr(QTextCodec::codecForName(QTextCodec::codecForLocale()->name()));
    QTextCodec::setCodecForLocale(QTextCodec::codecForName(QTextCodec::codecForLocale()->name()));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName(QTextCodec::codecForLocale()->name()));

    QApplication a(argc, argv);

    //自动翻译中文
    QTranslator oTranslator;
    oTranslator.load("qt_zh_CN.qm");
    a.installTranslator(&oTranslator);

    myd m;
    m.show();
    
    return a.exec();
}


注意:

如果编译的时候,出现 “当执行构建步骤 'Make'时” ,没有更多地提示,多半是.pro文件哪里写错了。

下面是修改后的样子:

3. 转换.ui为.h文件

在CMD窗口中切换到ui文件所在的文件夹,执行

D:\work\mypython>uic -o hello.h myform.ui

没有错误提示的话,生成hello.h头文件

引用的文件为 mainwindow.h mainwondow.cpp

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDial>
#include "hello.h"

class MainWindow : public QWidget
{
    Q_OBJECT
    
public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:

    //类的名称myForm 和hello.h中,namespace Ui {}定义的对应
    Ui::myForm ui;
};

#endif // MAINWINDOW_H


cpp文件

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);

     //引用图形控件
    ui.lineEdit->setText(tr("name"));
}

MainWindow::~MainWindow()
{
    
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值