首页 > 其他 > 详细

Qt中的字符串类

时间:2019-12-24 00:48:49      阅读:99      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

 技术分享图片

 标准库STL

技术分享图片

 Qt VS STL

技术分享图片

技术分享图片

Qt中的字符串类
——采用Unicode编码,意味着可以直接支持韩文、日文、中文等等。而STL中的string类不支持Unicode编码,只支持ascII码
——使用隐式共享技术来节省内存和不必要的数据拷贝
——跨平台使用,不必考虑字符串的平台兼容性

注意:隐式共享技术集成了深拷贝和浅拷贝优点于一身的技术。

技术分享图片

 

 技术分享图片

#include "QCalculatorUI.h"
#include <QDebug>

QCalculatorUI::QCalculatorUI(): QWidget(NULL,Qt::WindowCloseButtonHint) //此处QCalculatorUI就是作为顶层窗口存在的,虽然这个地方继承自QWidget,但是赋值为NULL,相当于它是没有父类的(但是实际上还是有的)。
                                                                        //将窗口中的最大化和最小化去掉
{
    //因为QLineEdit与QCalculatorUI以及QPushButton与QCalculatorUI是组合关系,那么就应该同生死,因此需要在构造函数对其定义。因为此处涉及到在堆上申请内存空间,因此需要
    //使用二阶构造

}

bool QCalculatorUI::construct()
{
    bool ret = true;
    const char* btnText[20] =
    {
        "7", "8", "9", "+", "(",
        "4", "5", "6", "-", ")",
        "1", "2", "3", "*", "<-",
        "0", ".", "=", "/", "C",
    };

    m_edit = new QLineEdit(this);

    if(m_edit != NULL)
    {
        m_edit->move(10,10);
        m_edit->resize(240,30);
        m_edit->setReadOnly(true);  //使QLineEdit只读
        m_edit->setAlignment(Qt::AlignRight); //使字符串靠右对齐
    }
    else
    {
        ret = false;
    }

    for(int i=0; (i<4) && ret; i++)
    {
        for(int j=0; (j<5) && ret; j++)
        {
            if(m_buttons[i*5 + j] != NULL)
            {
                m_buttons[i*5 + j] = new QPushButton(this);
                m_buttons[i*5 + j]->move(10 + (10 + 40)*j, 50 + (10 + 40)*i);
                m_buttons[i*5 + j]->resize(40,40);
                m_buttons[i*5 + j]->setText(btnText[i*5 + j]);
                connect(m_buttons[i*5 + j],SIGNAL(clicked()), this, SLOT(onButtonClicked()));
            }
            else
            {
                ret = false;
            }
        }
    }

    return ret;
}

QCalculatorUI* QCalculatorUI::NewInstance()
{
    QCalculatorUI* ret = new QCalculatorUI();

    if((ret == NULL) || !(ret->construct()))
    {
        delete ret;
        ret = NULL;
    }

    return ret;
}

void QCalculatorUI::onButtonClicked()
{
    QPushButton* btn = (QPushButton*)sender();
    QString clickText = btn->text();

    if(clickText == "<-")//此时应该将字符串的最后一个字符去掉。
    {
        QString text = m_edit->text();

        if(text.length() > 0)
        {
            text.remove(text.length()-1,1);
            m_edit->setText(text);
        }
    }
    else if(clickText == "C")
    {
        m_edit->setText( "");
    }
    else if(clickText == "=")
    {
        qDebug() << "= symbol:";
    }
    else
    {
        m_edit->setText(m_edit->text() + clickText);
    }

}
void QCalculatorUI::show()
{
    QWidget::show();
    this->setFixedSize(this->width(),this->height()); //固定窗口的大小
}
QCalculatorUI::~QCalculatorUI()
{

}

 

 

 

Qt中的字符串类

原文:https://www.cnblogs.com/-glb/p/12081692.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!