• 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()
    {
    
    }

     

  • 相关阅读:
    IBatis学习总结之动态拼sql
    IBatis学习总结
    帝都残暴的.net 之旅 (Martin Fowler 有留言哦)
    简洁实用的WordPress模板
    发现一位同行特牛
    app爬虫--mitmproxy用法梳理
    [转]数据库设计中的14个技巧
    [转] sql数据类型 varchar与nvarchar的区别
    web工作原理
    Xcode6 LaunchImage尺寸
  • 原文地址:https://www.cnblogs.com/-glb/p/12081692.html
Copyright © 2020-2023  润新知