Qt中的字符串类

 

 标准库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()
{

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值