Qt编写计算器(界面)

看了几个视频,写了个计算器的界面

#include <QApplication>
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget* w = new QWidget(NULL,Qt::WindowCloseButtonHint);//没有最大最小化按钮
    QLineEdit* Le = new QLineEdit(w);
    QPushButton* Button[20] = {0};
    const char* BtnText[20]=
    {
        "7","8","9","+","(",
        "4","5","6","-",")",
        "1","2","3","*","<-",
        "0",".","=","/","C"
    };
    Le->move(10,10);
    Le->resize(240,30);
    Le->setReadOnly(true);//文本框只读,不能通过键盘输入
    for(int i = 0;i<4;i++)
    {
        for(int j = 0;j<5;j++)
        {
            Button[i*5+j] = new QPushButton(w);
            Button[i*5+j]->resize(40,40);
            Button[i*5+j]->move(10 + (10 + 40)*j,(10+40) + (10 + 40)*i);
            Button[i*5+j]->setText(BtnText[i*5+j]);
        }
    }
    w->setWindowTitle("计算器");

    w->show();
    w->setFixedSize(w->width(),w->height());//锁定窗口大小
    /*
     *  w->show();
     *   w->setFixedSize(w->width(),w->height());//锁定窗口大小
     * 这两句顺序不能反
     */

    return a.exec();
}

运行界面如下

原文地址:https://www.cnblogs.com/tianxxl/p/10294901.html