1. 程式人生 > >Qt編寫計算器(界面)

Qt編寫計算器(界面)

eight exe btn tex clas edi 不能 fixed png

看了幾個視頻,寫了個計算器的界面

#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(); }

運行界面如下

技術分享圖片

Qt編寫計算器(界面)