Qtの中Stringクラス

 

 

 標準ライブラリSTL

 QtのVSのSTL

Qtの文字列クラス
-使用してUnicodeエンコーディングを、あなたが直接、というように韓国語、日本語、中国語をサポートできることを意味します。そして、STL文字列クラスは、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()
{

}

 

 

 

おすすめ

転載: www.cnblogs.com/-glb/p/12081692.html