QT入门2

1.QT中文输入支持

root用户

yum install ibus -qt
reboot

2.Unicode和UTF-8

作者:uuspider
链接:https://www.zhihu.com/question/23374078/answer/65352538
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
举一个例子:It’s 知乎日报
你看到的unicode字符集是这样的编码表:

I 0049
t 0074
’ 0027
s 0073
0020
知 77e5
乎 4e4e
日 65e5
报 62a5

每一个字符对应一个十六进制数字。
计算机只懂二进制,因此,严格按照unicode的方式(UCS-2),应该这样存储:
I 00000000 01001001
t 00000000 01110100
’ 00000000 00100111
s 00000000 01110011
00000000 00100000
知 01110111 11100101
乎 01001110 01001110
日 01100101 11100101
报 01100010 10100101

这个字符串总共占用了18个字节,但是对比中英文的二进制码,可以发现,英文前9位都是0!浪费啊,浪费硬盘,浪费流量。
怎么办?

UTF。
UTF-8是这样做的:

1. 单字节的字符,字节的第一位设为0,对于英语文本,UTF-8码只占用一个字节,和ASCII码完全相同;
2. n个字节的字符(n>1),第一个字节的前n位设为1,第n+1位设为0,后面字节的前两位都设为10,这n个字节的其余空位填充该字符unicode码,高位用0补足。
这样就形成了如下的UTF-8标记位:

0xxxxxxx
110xxxxx 10xxxxxx
1110xxxx 10xxxxxx 10xxxxxx
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx

… …
于是,”It’s 知乎日报“就变成了:
I 01001001

t 01110100
’ 00100111
s 01110011
00100000
知 11100111 10011111 10100101
乎 11100100 10111001 10001110
日 11100110 10010111 10100101
报 11100110 10001010 10100101

和上边的方案对比一下,英文短了,每个中文字符却多用了一个字节。但是整个字符串只用了17个字节,比上边的18个短了一点点。
下边是课后作业:

请将”It’s 知乎日报“的GB2312和GBK码(自行google)转成二进制。不考虑历史因素,从技术角度解释为什么在unicode和UTF-8大行其道的同时,GB2312和GBK仍在广泛使用。
剧透:一切都是为了节省你的硬盘和流量。

3.QT常用数据类型

1.QChar
用两个字节的Unicode编码来表示一个字符

2.QString
QString ,存储 Unicode 编码的字符串,QString 类可以很方便的存储非 ASCII、非 Latin-1 字符。在 Qt 中存储一个 Unicode 字符是用 QChar 类,那么 QString 就是存储着一个个的 QChar。Unicode 编码格式是双字节存储一个字,所以 QString 类里面存储着一个个的 16-bit QChar 字符,每个 QChar 字符对应着一个 Unicode 4.0 字符。如果字符的编码大于65536时,用两个 QChar 存储这个字符。

QChar c=0x6c49;
qDebug() << c;

QString s=QString(c);
qDebug() << s;

QString s2="中国";
qDebug() << s2;

在这里插入图片描述

4.实例:

计算器
mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QPushButton"
#include"QLabel"
#include"test.h"
#include"QDebug"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->resize(340340);//设置窗口大小

    QLabel* lbl=new QLabel;//qlabel提供一个文本和图片显示 需要头文件QLabel
    lbl->setText("计算器:");//设置label框内的文本
    lbl->setObjectName("calculate");//命名 ObjectName主要是用于外界来访问内部的控件成员的
    lbl->setParent(this);//定义显示的窗口
    lbl->resize(240,30);//大小 宽度 高度
    lbl->move(50,50);//定义位置
    lbl->show();//显示
    lbl->setStyleSheet("QLabel{background:white;color:black;padding:5px;font:bold 20px arial}");//(背景,字体颜色,离边框距离,加粗)

    QString symbols[] = {
      "1", "2", "3", "+",
      "4", "5", "6", "-",
      "7", "8", "9", "*",
      "0", ".", "=", "/"
      };//定义键盘

    int x=50,y=100;

    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            mybutton* btn=new mybutton;//生成一个无名对象
            connect(btn,SIGNAL(onclick2(QChar)),this,SLOT());
            btn->move(x+60*j,y+50*i);//定义按键位置
            btn->resize(60,30);//定义尺寸 宽度 高度
            btn->setText(symbols[i*4+j]);//定义按键上显示的文字
            btn->setParent(this);//显示在这个屏幕
            btn->show();//显示开关
        }
    }

}



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


mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include"QMessageBox"
#include <QMainWindow>
#include"QPushButton"
#include"test.h"
#include"QLabel"
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private:
    Ui::MainWindow *ui;
    QString operand1;
    QString operand2;
    QChar op;

public slots:
    void calcResult(QChar c){
        QLabel* lbl = this->findChild<QLabel*>("lbl",0);

        if((c >= '0' && c <= '9') || c == '.'){
            if(op == ' '){
                operand1.push_back(c);
            }else{
                operand2.push_back(c);
            }
        }else if(c == '+' || c == '-' || c == '*' || c == '/'){
            op = c;
        }
        lbl->setText(operand1 + op + operand2);

        if(c == '='){
            double rs = 0;
            double a = operand1.toDouble();
            double b = operand2.toDouble();
            switch(op.toLatin1()){
            case '+':
                rs = a + b;
                break;
            case '-':
                rs = a - b;
                break;
            case '*':
                rs = a * b;
                break;
            case '/':
                rs = a / b;
                break;
            }



            QString qResult = QString::number(rs);



            lbl->setText(operand1 + op + operand2 + "=" + qResult);

            operand1.clear();
            operand2.clear();
            op = ' ';
        }
    }
};


class MyButton : public QPushButton {
    Q_OBJECT

public:
    MyButton(){
        connect(this, SIGNAL(clicked()), this, SLOT(onclick()));
    }

private:
signals:
    void onClick2(const QChar s);

private slots:
    void onclick(){
        emit onClick2(this->text().at(0));
    }
};

#endif // MAINWINDOW_H

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值