qt creator, 计算器

9 篇文章 0 订阅
5 篇文章 0 订阅

1. 对于一个mainwindow w, w.show()结果显示自动居中,之前加w.setFixedSize()结果仍居中,之后加w.setFixedSize()结果在原起始位置开始,大小变化;

w.showMaximized()结果显示全屏,之后加w.setFixedSize()结果靠右上,再想调整位置至中间,需添加QDesktopWidget库,已使用QApplication::desktop()->screen()-?rect().center()-w.rect().center()作为w.move的pointer参数,改变w 的位置 (QApplication::desktop()->screen()-?rect().center()指widget左上起点为屏幕中心,但整个widget的中心也在屏幕中心的话,需要-w.rect().center()部分)

但其实简单来用,就是w.setFixedSize,和w.show()就可以得到居中结果

#include "mainwindow.h"
#include <QApplication>
#include <QDesktopWidget>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
//    w.showMaximized();
    w.setFixedSize(500,750);
   w.show();
 //  w.move(QApplication::desktop()->screen()->rect().center()-w.rect().center());
    return a.exec();
}
 

2. 编程添加按钮或其他GUI组件时需要添加相应的库,如<QPushButton>,<QLabel>等

3. mainwindow.h中private slots: 下定义相应的动作函数

private: 下定义添加的组件,以指针形式定义如QLabel *label等,相同类型的按钮可以以数组形式定义,如QPushedButton* buttons[10];

4.mainwindow.cpp中定义private slots中函数的实现,并具体添加各private组件

同时需添加库QCoreApplication,以便使用QString等

在构造函数中添加组件,用new方法,如

 showLabel=new QLabel("0",this);
 showLabel->setGeometry(QRect(QPoint(250,50),QSize(50,100)));

要想在组件上显示,必须类型为QString,所以,一般的int型需要类型转换,如

        QString digit=QString::number(i);
        digitButton[i]=new QPushButton(digit,this);
 
定义组件之后,组件的动作SIGNAL与相应的结果反映SLOT的对应关系也需要被设定, 

        connect(operatorButton[i],SIGNAL(released()),this,SLOT(operatorButtonPushed());
    }
这里SIGNAL为组件本身所有特性,SLOT为人为定义函数,当然两者也都可以在.h文件中定义,或使用自带特性

QObject * QObject::sender() 返回产生SIGNAL的组件

	QPushedButton* buttonNow=(QPushButton*)sender();
得到按钮上的字以及显示

    showLabel->setText(buttonNow->text());

其他函数的实现与C++类函数实现类似

QString类型不能用于switch case只有int或枚举型可以;

把int,double等换做QString时用:

QString digit=QString::number(i);
把QString换做int,double时用:

            fnum=result.toDouble();


5. 注意特殊情况,如计算多位数的运算,以及在结果基础上继续计算


6.出现:-1: error: LNK1168: cannot open debug\calculator.exe for writing时,检查之前的结果是否关闭


7. 问题??如何用编程的方式修改按钮上面字符的大小呢??


编程结果:


代码: mainwindow.cpp

#include "mainwindow.h"
//#include "ui_mainwindow.h"
#include <QCoreApplication>
 
QString result="0";//表示计算结果
QString value="";//表示输入值,应对几位数的情况,需要按多个按钮
QString operation="";
double fnum=0.0;
double snum=0.0;
bool consective_with_equal;
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
 
    showLabel=new QLabel("0",this);
    showLabel->setGeometry(QRect(QPoint(250,50),QSize(50,100)));
 
    QString operatorName[6]={"C","+","-","=","*","/"};
 
    for(int i=0;i<4;i++){
        operatorButton[i]=new QPushButton(operatorName[i],this);
        operatorButton[i]->setGeometry(QRect(QPoint(350,300+100*i),QSize(80,80)));
        connect(operatorButton[i],SIGNAL(released()),this,SLOT(operatorButtonPushed()));
    }
    for(int i=4;i<6;i++){
        operatorButton[i]=new QPushButton(operatorName[i],this);
        operatorButton[i]->setGeometry(QRect(QPoint(150+100*(i-4),600),QSize(80,80)));
        connect(operatorButton[i],SIGNAL(released()),this,SLOT(operatorButtonPushed()));
    }
    for(int i=0;i<10;i++){
        QString digit=QString::number(i);
        digitButton[i]=new QPushButton(digit,this);
        digitButton[i]->setGeometry(QRect(QPoint(50+100*(i%3),300+100*(i/3)),QSize(80,80)));
        connect(digitButton[i],SIGNAL(released()),this,SLOT(digitButtonPushed()));
    }
}
void MainWindow::digitButtonPushed(){
    consective_with_equal=false;
    QPushButton* buttonNow=(QPushButton*)sender();
    value+=buttonNow->text();//连续按多个按钮表示几位数
    showLabel->setText(value);
    snum=value.toDouble();
}
void MainWindow::operatorButtonPushed(){
    QPushButton* operatorNow=(QPushButton*)sender();
    value="";
    if(operatorNow->text()=="C"){
        result="0";
        fnum=0.0;
        snum=0.0;
        showLabel->setText(result);
    }
    else if(operatorNow->text()=="="){
        consective_with_equal=true;
        if(operation==""){
 
        }
        else if(operation=="+"){
            result=QString::number(fnum+snum);
            showLabel->setText(result);
 
        }
        else if(operation=="-"){
            result=QString::number(fnum-snum);
            showLabel->setText(result);
            fnum=result.toDouble();
        }
        else if(operation=="*"){
            result=QString::number(fnum*snum);
            showLabel->setText(result);
            fnum=result.toDouble();
        }
         else if(operation=="/"){
            result=QString::number(fnum/snum);
            showLabel->setText(result);
            fnum=result.toDouble();
        }
    }
    else{
        if(consective_with_equal){//在已得结果基础上连续运算
             fnum=result.toDouble();
        }
        else{
            fnum=snum;
        }
        operation=operatorNow->text();
    }
 
}
 
MainWindow::~MainWindow()
{
 
}
 

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
#include <QPushButton>
#include <QLabel>
 
namespace Ui {
class MainWindow;
}
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
 
private:
    QPushButton* digitButton[10];
    QLabel *showLabel;
    QPushButton* operatorButton[6];
 
private slots:
    void digitButtonPushed();
    void operatorButtonPushed();
 
};
 
#endif // MAINWINDOW_H
 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值