Qt简易计算器实现复数运算绘图等等

2023-05-16

太久没更新博客了前段时间弄了个计算器
实现了加减乘除复数 带记忆 绘图计算二维图形等功能
先简单讲解下我代码的分块
用图片描述虽然画的丑

在这里插入图片描述
实现效果图片 代码放心使用转载请通知
在这里插入图片描述
在这里插入图片描述
1.Ui
在这里插入图片描述
2.在Maindow 类实现计算
maindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "push.h"

#include <QMainWindow>
#include <QTableWidget>
#include <QLabel>
#include <QRadioButton>
#include <QLineEdit>
#include <QPushButton>
#include <QPainter>
#include <QKeyEvent>
#include <QDebug>
#include <QFormLayout>
#include <QPainterPath>
#include <QtMath>
#include <QStack>
namespace Ui {
class MainWindow;
}
class Comp;
class Push;
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:

    enum Operator {
        NONE,
        ADD,
        MINUS,
        DIV,
        MUL
    };


    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void pluswait(Operator oper = NONE);
    void addwait();
    void reducewait();
    void ridewait();
    void exceptwait();
protected:
    void paintEvent(QPaintEvent *event) override;
    void keyPressEvent(QKeyEvent *key) override;
private slots:
    void on_btn0_clicked();
    void on_btn1_clicked();
    void on_btn2_clicked();
    void on_btn3_clicked();
    void on_btn4_clicked();
    void on_btn5_clicked();
    void on_btn6_clicked();
    void on_btn7_clicked();
    void on_btn8_clicked();
    void on_btn9_clicked();
    void on_btnCe_clicked();
    void on_btnAdd_clicked();
    void on_btnDiv_clicked();
    void on_btnDot_clicked();
    void on_btnMul_clicked();
    void on_btnPop_clicked();
    void on_btnCalc_clicked();
    void on_btnSign_clicked();
    void on_btnClear_clicked();
    void on_btnMinus_clicked();

    void on_compButton_clicked();

    void on_x1Button_clicked();

    void on_x2Button_clicked();

    void on_leftButton_clicked();

    void on_rightButton_clicked();

private:
    Ui::MainWindow *ui;

    double result;
    double mNumber1;
    Operator mOperator;
    bool mNeedClear;
    QString str;
    QTabWidget *tab1;
    void pushNumber(char n);
    void reset();
    QString getCurrentNumberStr() const;
    int width = 500;
    int height = 200;
//    int mistype = -1;
    bool mistype = false;
    bool miscomp = false;
    Push *push;
    Comp *comp1;
    Comp *comp2;
    QStack<QString>*stack;
    QStack<QString>*sta;
};

class Comp{

public:
    Comp(double com=0,double con=0):com(com),con(con){}
    void setCom(double n) { com = n; }
    void setCon(double n) { con = n; }
    double getCom() { return com; }
    double getCon() { return con; }

    Comp *operator+(Comp *comp2){
        this->com = this->com+comp2->com;
        this->con = this->con+comp2->con;
        return this;
    }
    Comp *operator-(Comp *comp2){
        this->com = this->com-comp2->com;
        this->con = this->con-comp2->con;
        return this;
    }
    Comp *operator*(Comp *comp2){
        this->com = this->com*comp2->com;
        this->con = this->con*comp2->con;
        return this;
    }
    Comp *operator/(Comp *comp2){
        this->com = this->com/comp2->com;
        this->con = this->con/comp2->con;
        return this;
    }
private:
    double com,con;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#define PI 3.14159265
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
  ,push(new Push())
  ,comp1(new Comp(0,0))
  ,comp2(new Comp(0,0))
  ,stack(new QStack<QString>)
  ,sta(new QStack<QString>)
  , mOperator(NONE)
  , mNeedClear(false)
{
    ui->setupUi(this);  // 构造Ui内容
    reset();
    tab1 = new QTabWidget(this);
    tab1->addTab(ui->centralWidget,tr("计算器"));
    tab1->addTab(push->getWidget1(),tr("圆"));
    tab1->addTab(push->getWidget2(),tr("三角形"));
    tab1->addTab(push->getWidget3(),tr("正方形长方形"));
    tab1->addTab(push->getWidget4(),tr("梯形"));
    tab1->setGeometry(0,0,200,200);
    resize(width++,height++);
//    setWindowFlags(windowFlags()&~Qt::WindowMaximizeButtonHint);
//    setFixedSize(this->width(),this->height());
//    tab2 = new QTabWidget(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::pluswait(Operator oper) // =
{
    if(mistype){
        double number = ui->edtDisplay->text().toDouble();
        mistype = false;
        if(number){
//        result = 0;
        switch (mOperator) {
        case ADD:
            result = mNumber1 + number;
            break;
        case MINUS:
            result = mNumber1 - number;
            break;
        case MUL:
            result = mNumber1 * number;
            break;
        case DIV:
            result = mNumber1 / number;
            break;
        case NONE:
        default:
            break;
        }
        str.append('=');
        ui->showlabel->setText(str);
        ui->edtDisplay->setText(QString::number(result));
        str = str.number(result);
        }
    }
    else {
        if(!miscomp && comp1->getCom() && comp1->getCon() && comp2->getCom() && comp2->getCon()){
            switch (oper) {
            case ADD:comp1->operator+(comp2);   break;
            case MINUS:comp1->operator-(comp2); break;
            case MUL:comp1->operator*(comp2);   break;
            case DIV:comp1->operator/(comp2);   break;
            }
            ui->edtDisplay->setText(tr("实部:")+QString::number(comp1->getCom())+tr("虚部:")+QString::number(comp1->getCon()));
            comp1->setCom(0);
            comp1->setCon(0);
            comp2->setCom(0);
            comp2->setCon(0);
        }
    }
    stack->append(str);
//    sta->append(str);
}

void MainWindow::addwait()
{
    mOperator = ADD;
    mNumber1 = ui->edtDisplay->text().toDouble();
    mNeedClear = true;

    str.append('+');
    ui->edtDisplay->setText(str);
}

void MainWindow::paintEvent(QPaintEvent *event) // 重绘事件
{
    QPainter paint(this);
    QPainterPath path;
    if(push->getCurrentPaint() == push->getCurrentRound()){
        paint.drawRoundedRect(250,50,push->getmdou()/2,push->getmdou()/2,999,999);
        paint.drawText(400,180,tr("胡鑫1412"));
    }else if(push->getCurrentPaint() == push->getCurrentTriangle()){
        paint.drawLine(250,50,250+push->gettrinw(),50+push->gettrinw());
        paint.drawLine(250+push->gettrinw(),50+push->gettrinw(),250-push->gettrinw(),50+push->gettrinw());
        paint.drawLine(250-push->gettrinw(),50+push->gettrinw(),250,50);
        paint.drawText(400,180,tr("胡鑫1412"));
    }else if(push->getCurrentPaint() == push->getCurrentRect()){
        paint.drawRect(250,50,push->getrectw(),push->getrectw());
        paint.drawText(400,180,tr("胡鑫1412"));
    }else if(push->getCurrentPaint() == push->getCurrentMany()){
        paint.drawLine(250,50,250+push->getmanyup(),50);
        paint.drawLine(250+push->getmanyup(),50,250+push->getmanyh(),50+push->getmanyh());
        paint.drawLine(250+push->getmanyh(),50+push->getmanyh(),250-push->getmanydown(),50+push->getmanyh());
        paint.drawLine(250-push->getmanydown(),50+push->getmanyh(),250,50);
        paint.drawText(400,180,tr("胡鑫1412"));
    }
}

void MainWindow::keyPressEvent(QKeyEvent *key)
{
    if(key->key() == Qt::Key_Plus && key->modifiers() == Qt::ControlModifier)
        addwait();
}

void MainWindow::on_btn0_clicked() // 当点击Ui中的0按钮
{
    pushNumber('0');
}

void MainWindow::on_btn1_clicked()
{
    pushNumber('1');
}

void MainWindow::on_btn2_clicked()
{
    pushNumber('2');
}

void MainWindow::on_btn3_clicked()
{
    pushNumber('3');
}

void MainWindow::on_btn4_clicked()
{
    pushNumber('4');
}

void MainWindow::on_btn5_clicked()
{
    pushNumber('5');
}

void MainWindow::on_btn6_clicked()
{
    pushNumber('6');
}

void MainWindow::on_btn7_clicked()
{
    pushNumber('7');
}

void MainWindow::on_btn8_clicked()
{
    pushNumber('8');
}

void MainWindow::on_btn9_clicked()
{
    pushNumber('9');
}

void MainWindow::on_btnCe_clicked() // 点击Ce清除
{
    str = "";
    ui->showlabel->setText(str);
    reset();
}

void MainWindow::on_btnAdd_clicked() // +  1+1+1    21   2+1
{
//    switch (mOperator) {
//        case ADD:mOperator = ADD; break;
//        case MINUS:mOperator = MINUS; break;
//        case DIV:mOperator = DIV; break;
//        case MUL:mOperator = MUL; break;
//    default: mOperator = ADD; break;
//    }
    if(!mistype ||!mOperator)
        mOperator = ADD;
    if(mistype)
        pluswait();
    else if(!ui->compButton->isChecked() && !mistype){
        mNumber1 = ui->edtDisplay->text().toDouble();
        mNeedClear = true;
        mistype = true;
        str.append('+');
    }
    else {
        if(!miscomp && !comp1->getCom()){
            comp1->setCom(ui->edtDisplay->text().toDouble());
            str.append('+');
            miscomp = true;
        }
        else if(miscomp && !comp1->getCon()){
            comp1->setCon(ui->edtDisplay->text().toDouble());
            str.append('+');
            miscomp = false;
        }
        else if(!miscomp && !comp2->getCom()){
            comp2->setCom(ui->edtDisplay->text().toDouble());
            str.append('+');
            miscomp = true;
        }
        else if(miscomp && !comp2->getCon()){
            comp2->setCon(ui->edtDisplay->text().toDouble());
            miscomp = false;
            pluswait(ADD);
        }

    }
    ui->showlabel->setText(str);

}

void MainWindow::on_btnMinus_clicked() // -
{
//    switch (mOperator) {
//        case ADD:mOperator = ADD; break;
//        case MINUS:mOperator = MINUS; break;
//        case DIV:mOperator = DIV; break;
//        case MUL:mOperator = MUL; break;
//    default: mOperator = MINUS; break;
//    }

    if(!mistype ||!mOperator)
        mOperator = MINUS;
    if(mistype)
        pluswait();
    else if(!ui->compButton->isChecked() && !mistype){
        mNumber1 = ui->edtDisplay->text().toDouble();
        mNeedClear = true;
        mistype = true;
        str.append('-');
    }
    else {
        if(!miscomp && !comp1->getCom()){
            comp1->setCom(ui->edtDisplay->text().toDouble());
            str.append('-');
            miscomp = true;
        }
        else if(miscomp && !comp1->getCon()){
            comp1->setCon(ui->edtDisplay->text().toDouble());
            str.append('-');
            miscomp = false;
        }
        else if(!miscomp && !comp2->getCom()){
            comp2->setCom(ui->edtDisplay->text().toDouble());
            str.append('-');
            miscomp = true;
        }
        else if(miscomp && !comp2->getCon()){
            comp2->setCon(ui->edtDisplay->text().toDouble());
            miscomp = false;
            pluswait(MINUS);
        }

    }

    ui->showlabel->setText(str);
}

void MainWindow::on_btnDiv_clicked() // /
{
//    switch (mOperator) {
//        case ADD:mOperator = ADD; break;
//        case MINUS:mOperator = MINUS; break;
//        case DIV:mOperator = DIV; break;
//        case MUL:mOperator = MUL; break;
//    default: mOperator = DIV; break;
//    }
    if(!mistype ||!mOperator)
        mOperator = DIV;
    if(mistype)
        pluswait();
    else if(!ui->compButton->isChecked() && !mistype){
        mNumber1 = ui->edtDisplay->text().toDouble();
        mNeedClear = true;
        mistype = true;
        str.append('/');
    }
    else {
        if(!miscomp && !comp1->getCom()){
            comp1->setCom(ui->edtDisplay->text().toDouble());
            str.append('/');
            miscomp = true;
        }
        else if(miscomp && !comp1->getCon()){
            comp1->setCon(ui->edtDisplay->text().toDouble());
            str.append('/');
            miscomp = false;
        }
        else if(!miscomp && !comp2->getCom()){
            comp2->setCom(ui->edtDisplay->text().toDouble());
            str.append('/');
            miscomp = true;
        }
        else if(miscomp && !comp2->getCon()){
            comp2->setCon(ui->edtDisplay->text().toDouble());
            miscomp = false;
            pluswait(DIV);
        }
    }
    ui->showlabel->setText(str);
//    ui->edtDisplay->setText(str);
}

void MainWindow::on_btnDot_clicked() // 小数
{
    pushNumber('.');
}

void MainWindow::on_btnMul_clicked() // *
{
//    switch (mOperator) {
//        case ADD:mOperator = ADD; break;
//        case MINUS:mOperator = MINUS; break;
//        case DIV:mOperator = DIV; break;
//        case MUL:mOperator = MUL; break;
//    default: mOperator = MUL; break;
//    }
    if(!mistype ||!mOperator)
        mOperator = MUL;
    if(mistype)
        pluswait();
    else if(!ui->compButton->isChecked() && !mistype){
        mNumber1 = ui->edtDisplay->text().toDouble();
        mNeedClear = true;
        mistype = true;
        str.append('x');
    }
    else {
        if(!miscomp && !comp1->getCom()){
            comp1->setCom(ui->edtDisplay->text().toDouble());
            str.append('x');
            miscomp = true;
        }
        else if(miscomp && !comp1->getCon()){
            comp1->setCon(ui->edtDisplay->text().toDouble());
            str.append('x');
            miscomp = false;
        }
        else if(!miscomp && !comp2->getCom()){
            comp2->setCom(ui->edtDisplay->text().toDouble());
            str.append('x');
            miscomp = true;
        }
        else if(miscomp && !comp2->getCon()){
            comp2->setCon(ui->edtDisplay->text().toDouble());
            miscomp = false;
            pluswait(MUL);
        }

    }
    ui->showlabel->setText(str);
//    ui->edtDisplay->setText(str);
}

void MainWindow::on_btnPop_clicked() // 左移
{
    QString s = ui->edtDisplay->text();
    s.remove(s.length()-1,1);
    str.remove(s.length(),1);
    ui->showlabel->setText(str);
    ui->edtDisplay->setText(s);
    mNeedClear = false;
}

void MainWindow::on_btnCalc_clicked() // =
{
    if(mOperator == NONE)
        return;

    qDebug() << str;
    if(!ui->compButton->isChecked()){
        pluswait();
    }
    else {
        comp2->setCon(ui->edtDisplay->text().toDouble());
        miscomp = false;
        pluswait(mOperator);
    }
    mOperator = NONE;
    mNeedClear = true;
}

void MainWindow::on_btnSign_clicked() // 正负号
{
    if(mNeedClear) { // 默认为false 当触发+-*/会为true
        reset();
    }

    str = getCurrentNumberStr();
    if(!str.isEmpty() && str.at(0)=='-')
        str.remove(0, 1);
    else
        str.prepend('-');
    ui->edtDisplay->setText(str);
}

void MainWindow::on_btnClear_clicked() // up
{
//    reset();
//    ui->edtDisplay->setText(QString::number(result));
    if(!stack->isEmpty()){
        sta->append(stack->top());
        str = stack->top();
        ui->edtDisplay->setText(stack->top());
        ui->showlabel->setText(stack->top());
        stack->pop();
    }
    else{
        while (!sta->isEmpty()){
            stack->append(sta->top());
            sta->pop();
        }
    }

}

void MainWindow::pushNumber(char n)
{
    if(mNeedClear) {
        reset();
    }
    QString numberStr;
    if(!ui->compButton->isChecked()){ // 普通模式
        numberStr = getCurrentNumberStr();
        if(n=='0' && numberStr.isEmpty())
            return;
    }
    else  {// 选中复数模式

    }

    numberStr.append(n);//将字符串追加到末尾
    str += n;
    ui->showlabel->setText(str);
    ui->edtDisplay->setText(numberStr);
}

void MainWindow::reset()
{
    ui->edtDisplay->setText("0");
//    str = "";
//    mistype = true;
    mNeedClear = false;
}

QString MainWindow::getCurrentNumberStr() const
{
    QString numberStr = ui->edtDisplay->text();
    if(!numberStr.isEmpty() && numberStr.at(0)=='0')
        numberStr.remove(0, 1);
    return numberStr;
}


void MainWindow::on_compButton_clicked() // 复数模式
{
    miscomp = false;
    mistype = false;
}

void MainWindow::on_x1Button_clicked()
{
    QString s = ui->edtDisplay->text();
    double x1 = s.toDouble();
    x1 = 1/x1;
    str = str.number(x1);
    ui->showlabel->setText(str);
    ui->edtDisplay->setText(QString("%1").arg(x1));
}

void MainWindow::on_x2Button_clicked()
{
    QString s = ui->edtDisplay->text();
    double x2 = s.toDouble();
    x2 = x2*x2;
    str = str.number(x2);
    ui->showlabel->setText(str);
    ui->edtDisplay->setText(QString("%1").arg(x2));
}

3.Widget 这个类是继承Widget的
他被我用作一个纯虚类派生给push类实现的

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QLabel>
#include <QRadioButton>
#include <QLineEdit>
#include <QPushButton>
#include <QPainter>

#include <QDebug>
#include <QFormLayout>
#include <QPainterPath>
#include <QtMath>
#include <QWidget>

class Widget : public QWidget
{
    Q_OBJECT
public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

    enum Paint{
        Round,
        Triangle,
        Rect,
        Many
    };
    virtual void playRound() = 0;
    virtual void playTriangle() = 0;
    virtual void playRect() = 0;
    virtual void playMany() = 0;
//protected:
//    virtual void paintEvent(QPaintEvent *event) = 0;

};

#endif // WIDGET_H

widget.cpp

#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
}
Widget::~Widget()
{
}

5.push类继承widget类 实现里面的虚函数 这里面是绘画类容
push.h

#ifndef PUSH_H
#define PUSH_H

#include "widget.h"
#include <QPaintEvent>

class Push :public Widget
{
    Q_OBJECT
public:
    Push(QWidget *parent = nullptr);
    ~Push();

    virtual void playRound()override;
    virtual void playTriangle()override;
    virtual void playRect()override;
    virtual void playMany()override;
    QWidget *getWidget1(){return widget;}
    QWidget *getWidget2(){return widget2;}
    QWidget *getWidget3(){return widget3;}
    QWidget *getWidget4(){return widget4;}
    double getmdou(){ return mdou;};
    double gettrinw(){ return trinw;};
    double getrectw(){ return rectw;};
    double getrecth(){ return recth;};
    double getmanyup(){ return manyup;};
    double getmanydown(){ return manydown;};
    double getmanyh(){ return manyh;};

    Paint getCurrentPaint(){ return mpaint;}
    Paint getCurrentRound()const{ return Round;}
    Paint getCurrentTriangle()const{ return Triangle;}
    Paint getCurrentRect()const{ return Rect;}
    Paint getCurrentMany()const{ return Many;}

    void setCurrentPaint(Paint paint);
protected:
    void paintEvent(QPaintEvent *event) override;
//    bool eventFilter(QObject *obj,QEvent *event) override;

private slots:
    void slotRound();
    void slotTringle();
    void slotRect();
    void slotMany();

private:
    QLabel *roundlabel1;
    QLabel *roundlabel2;
    QLabel *roundlabel3;
//    QLabel *label4;
    QLineEdit *roundline1;
    QLineEdit *roundline2;
    QLineEdit *roundline3;

    QLineEdit *trinline1;
    QLineEdit *trinline2;
    QLineEdit *trinline3;
    QLineEdit *trinline4;

    QLineEdit *rectline1;
    QLineEdit *rectline2;
    QLineEdit *rectline3;
    QLineEdit *rectline4;

    QLineEdit *manyline1;
    QLineEdit *manyline2;
    QLineEdit *manyline3;
    QLineEdit *manyline4;
    QLineEdit *manyline5;

    QPushButton *roundbtn;
    QPushButton *trinbtn;
    QPushButton *rectbtn;
    QPushButton *manybtn;

    QWidget *widget;
    QWidget *widget2;
    QWidget *widget3;
    QWidget *widget4;
    Paint mpaint;

    double mdou;
    double trinw;
    double rectw;
    double recth;
    double manyup;
    double manydown;
    double manyh;

    int width = 500;
    int height = 200;
};

#endif // PUSH_H

push.cpp

#include "push.h"

#define PI 3.14159265

Push::Push(QWidget *parent)
    : Widget(parent)
{
    widget = new QWidget();
    widget2 = new QWidget();
    widget3 = new QWidget();
    widget4 = new QWidget();
    playRound();
    playTriangle();
    playRect();
    playMany();
    connect(roundbtn,SIGNAL(clicked()),this,SLOT(slotRound()));
    connect(trinbtn,SIGNAL(clicked()),this,SLOT(slotTringle()));
    connect(rectbtn,SIGNAL(clicked()),this,SLOT(slotRect()));
    connect(manybtn,SIGNAL(clicked()),this,SLOT(slotMany()));
//    installEventFilter(this);
}

Push::~Push()
{

}

void Push::playRound()
{
    QFormLayout *form = new QFormLayout(widget);
    roundline1 = new QLineEdit(widget);
    roundline2 = new QLineEdit(widget);
    roundline3 = new QLineEdit(widget);

    roundlabel1 = new QLabel(tr("直径:"),widget);
    roundlabel2 = new QLabel(tr("周长:"),widget);
    roundlabel3 = new QLabel(tr("面积:"),widget);

    roundline2->setReadOnly(true);
    roundline3->setReadOnly(true);

    roundline2->setHidden(true);
    roundline3->setHidden(true);
    roundlabel2->setHidden(true);
    roundlabel3->setHidden(true);

    roundbtn = new QPushButton(tr("Play"),widget);
    form->addRow(roundlabel1,roundline1);
    form->addRow(roundlabel2,roundline2);
    form->addRow(roundlabel3,roundline3);
    form->addWidget(roundbtn);
}

void Push::playTriangle()
{
    QFormLayout *form = new QFormLayout(widget2);
    trinline1 = new QLineEdit(widget2);
    trinline2 = new QLineEdit(widget2);
    trinline3 = new QLineEdit(widget2);
    trinline4 = new QLineEdit(widget2);

    trinline3->setReadOnly(true);
    trinline4->setReadOnly(true);
    trinline3->setHidden(true);
    trinline4->setHidden(true);

    form->addRow(tr("底:"),trinline1);
    form->addRow(tr("高:"),trinline2);
    form->addRow(tr("周长:"),trinline3);
    form->addRow(tr("面积:"),trinline4);
    trinbtn = new QPushButton(tr("Play"),widget2);
    form->addWidget(trinbtn);
}

void Push::playRect()
{
    QFormLayout *form = new QFormLayout(widget3);
    rectline1 = new QLineEdit(widget3);
    rectline2 = new QLineEdit(widget3);
    rectline3 = new QLineEdit(widget3);
    rectline4 = new QLineEdit(widget3);

    rectline3->setReadOnly(true);
    rectline4->setReadOnly(true);
    rectline3->setHidden(true);
    rectline4->setHidden(true);

    form->addRow(tr("长:"),rectline1);
    form->addRow(tr("宽:"),rectline2);
    form->addRow(tr("周长:"),rectline3);
    form->addRow(tr("面积:"),rectline4);

    rectbtn = new QPushButton(tr("Play"),widget3);
    form->addWidget(rectbtn);
}

void Push::playMany()
{
        QFormLayout *form = new QFormLayout(widget4);
        manyline1 = new QLineEdit(widget4);
        manyline2 = new QLineEdit(widget4);
        manyline3 = new QLineEdit(widget4);
        manyline4 = new QLineEdit(widget4);
        manyline5 = new QLineEdit(widget4);

        manyline4->setReadOnly(true);
        manyline5->setReadOnly(true);
        manyline4->setHidden(true);
        manyline5->setHidden(true);

        form->addRow(tr("上底"),manyline1);
        form->addRow(tr("下底:"),manyline2);
        form->addRow(tr("高:"),manyline3);
        form->addRow(tr("周长:"),manyline4);
        form->addRow(tr("面积:"),manyline5);

        manybtn = new QPushButton(tr("Play"),widget4);
        form->addWidget(manybtn);
//        mpaint = Many;
//        update();
}

void Push::setCurrentPaint(Widget::Paint paint)
{
    mpaint = paint;
}

void Push::paintEvent(QPaintEvent *event)
{
    QPainter paint(this);
    switch (mpaint) {
    case Round:
        paint.drawRoundedRect(250,50,mdou/2,mdou/2,999,999);
        paint.drawText(400,180,tr("胡鑫1412"));
        mdou = 0;
        break;
    case Triangle:
        paint.drawLine(250,50,250+trinw,50+trinw);
        paint.drawLine(250+trinw,50+trinw,250-trinw,50+trinw);
        paint.drawLine(250-trinw,50+trinw,250,50);
        paint.drawText(400,180,tr("胡鑫1412"));
        trinw = 0;
        break;
    case Rect:
        paint.drawRect(250,50,rectw,recth);
        paint.drawText(400,180,tr("胡鑫1412"));
        rectw = 0;
        recth = 0;
        break;
    case Many:
        paint.drawLine(250,50,250+manyup,50);
        paint.drawLine(250+manyup,50,250+manyh,50+manyh);
        paint.drawLine(250+manyh,50+manyh,250-manydown,50+manyh);
        paint.drawLine(250-manydown,50+manyh,250,50);
        paint.drawText(400,180,tr("胡鑫1412"));
        manyh = 0;
        manyup = 0;
        manydown = 0;
        break;
    default:
        break;
    }
}

//bool Push::eventFilter(QObject *obj, QEvent *event)
//{
//    QPaintEvent *eve = static_cast<QPaintEvent*>(event);
//    this->paintEvent(eve);
//}

void Push::slotRound()
{
    roundline2->setHidden(false);
    roundline3->setHidden(false);
    roundlabel2->setHidden(false);
    roundlabel3->setHidden(false);

    QString s = roundline1->text();
    mdou = s.toDouble();
    double r = mdou/2;
    double length = mdou*PI;
    double size = PI*(r*r);
    roundline2->setText(QString::number(length));
    roundline3->setText(QString::number(size));
    mpaint = Round;
    update();
}

void Push::slotTringle()
{
        trinline3->setHidden(false);
        trinline4->setHidden(false);
        QString s = trinline1->text();
        QString s2 = trinline2->text();
        trinw = s.toDouble();
        double h = s.toDouble();
        double length = trinw+trinw+trinw;
        double size = (trinw*h)/2;
        trinline3->setText(QString::number(length));
        trinline4->setText(QString::number(size));
        mpaint = Triangle;
        update();
}

void Push::slotRect()
{
        rectline3->setHidden(false);
        rectline4->setHidden(false);

        QString s = rectline1->text();
        QString s2 = rectline2->text();
        rectw = s.toDouble();
        recth = s.toDouble();

        double length = (rectw+recth)*2;
        double size = rectw*recth;

        rectline3->setText(QString::number(length));
        rectline4->setText(QString::number(size));
        mpaint = Rect;
        update();
}

void Push::slotMany()
{
        manyline4->setHidden(false);
        manyline5->setHidden(false);

        QString s = manyline1->text();
        QString s2 = manyline2->text();
        QString s3 = manyline2->text();
        manyup = s.toDouble();
        manydown = s2.toDouble();
        manyh = s3.toDouble();
        double length = manyup+manydown+manyh*2;
        double size = (manyup+manydown)*manyh/2;

        manyline4->setText(QString::number(length));
        manyline5->setText(QString::number(size));
        mpaint = Many;
        update();
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Qt简易计算器实现复数运算绘图等等 的相关文章

随机推荐

  • 两台ubuntu之间文件互传方法

    1 查找本机ip地址 终端输入 xff08 不是自己的电脑 xff0c 是服务其的IP xff09 xff1a ifconfig span class token operator span a 查看inet 192打头的地址 2 查找本机
  • 单片机通过串口发送浮点类型数据

    单片机通过串口向串口助手或者其他器件发送数据时是不能直接发送浮点类型数据的 xff0c 通过printf 34 f 34 1 28 发送浮点数据 在串口助手看似是小数 xff0c 其实是字符串类型 xff0c 它们是各符号对应的ASSCII
  • 制作系统启动盘和制作windows+Ubuntu双系统启动盘

    制作windows 43 Ubuntu双系统启动盘 前言1 准备软件和工具 xff1a 开始1 软件和系统镜像自行在网上下载 2 下载好系统镜像之后 xff0c 开始制作ubuntu系统盘 xff1a 3 制作好ubuntu启动盘之后 xf
  • 虚拟机分配的硬盘太大怎么办?

    虚拟机分配的硬盘太大怎么办 xff1f 虚拟机分配的硬盘太大怎么办 xff1f 文章来源说明 xff1a 开始1 首先需要将虚拟机中多余的硬盘空间删除 xff0c 在此电脑中右键管理 xff0c 然后在磁盘管理中右键有多余空间的磁盘进行压缩
  • QT界面窗口的显示和隐藏,关闭

    这里写目录标题 QT窗口的显示和隐藏 xff1a 1 隐藏窗口1 1 介绍 xff1a 2 显示窗口2 1 介绍 xff1a 3 关闭窗口常用函数有 xff1a QT窗口的显示和隐藏 xff1a 1 隐藏窗口 span class toke
  • QT的中文显示乱码问题解决

    QT的中文显示乱码问题解决 QT的中文显示乱码问题解决 xff1a 1 查看源文件的编码格式 xff0c 有必要的话转换源文件的编码格式再重新编译运行尝试汉字能否正常显示 2 在代码里面解决 xff1a 3 用法 xff1a QT的中文显示
  • 0.9 - GPIO寄存器的C语言映射与STM32库函数雏形构建思路

    首先 xff0c 从参考手册可知 xff0c 程序存储器 xff08 flash xff09 数据存储器 SRAM 寄存器 外设控制 和输入输出端口被组织在同一个4GB的线性地址空间内 数据字节以小端格式存放在存储器中 一个字里的最低地址字
  • Linux文件/文件夹建立软硬链接

    建立软连接 xff1a sudo ln s home spike Downloads redis src redis server usr local bin redis server 源文件 链接到 目标文件 xff0c 这两个文件目录都
  • Linux中记录终端(Terminal)输出到文本文件四种方法

    Linux中记录终端 xff08 Terminal xff09 输出到文本文件 Linux中记录终端 xff08 Terminal xff09 输出到文本文件 一 xff0c 如何把命令运行的结果保存到文件当中 二 command gt f
  • C++ 标准库头文件汇集:

    C 43 43 标准库头文件汇集 xff1a C 43 43 标准库的接口由下列头文件的汇集定义 C 43 43 标准库头文件汇集 xff1a 概念库协程库工具库动态内存管理数值极限错误处理字符串库容器库迭代器库范围库算法库数值库本地化库输
  • C++开源库列表总结记录

    开源 C 43 43 库列表 前言包管理器库音频 音频指纹格式标签CD 性能测试 通信 并发 配置 XMLJSONYAMLTOMLHOCONCSS容器 密码学 数据库 嵌入语言绑定 嵌入式 实时 文件元数据 金融计算 游戏引擎架构 通用多媒
  • C++转义序列和操作符优先级

    C 43 43 转义序列和操作符优先级 1 转义序列 xff1a 2 C 43 43 运算符优先级注解 1 转义序列 xff1a 转义序列 描述 表示 简单转义序列 39 单引号 ASCII 编码中为字节 0x27 34 双引号 ASCII
  • PX4:Policy “CMP0097“ is not known to this version of CMake.

    make px4 fmu v3 时报的错 CMake版本的问题 由https blog csdn net zhizhengguan article details 118380965推测 xff0c 删除cmake policy也没事 ma
  • 安装Anaconda3后再安装ROS(用于PX4)

    记录一下安装anaconda后再安装ROS xff0c 方便未来找错误 出现no module wstool 仔细观察报错信息发现调用的库都在anaconda3 lib python3 7里面了 随后按照https www cnblogs
  • Offboard仿真时出现CMD: Unexpected command 176, result 0

    在用PX4 43 gazebo 43 ROS仿真offboard例程时 xff0c 如果出现以下问题 xff1a 运行以下命令 xff1a roslaunch mavros px4 launch fcu url span class tok
  • Offboard例程的python版本

    因为python的库很丰富 xff0c 有现成的解方程库 xff0c 比如sympy 自己写了一份python版本的自动起飞2m的程序 假设自定义的包名为offb xff0c 在offb下新建scripts文件夹 xff0c 将py文件放在
  • 子类与父类构造函数调用顺序

    子类与父类构造函数调用顺序 构造函数 当创建子类对象时 构造函数的调用顺序 xff1a 静态数据成员的构造函数 gt 父类的构造函数 gt 非静态的数据成员的构造函数 gt 自己的构造函数 注意 无论创建几个对象 该类的静态成员只构建一次
  • 0.1- 机械加工工艺-----切削加工基础

    1 钳工 xff1a 通过工人手持工具进行切削加工 机械加工 xff1a 采用不同的机床 xff08 如车床 铣 床 刨床 磨床 钻床等 xff09 对工 件进行切削加工 2 零件几何参数 xff1a 1 宏观几何参数 xff1a 包括 x
  • 判断当前机器的字节序是大端还是小端?

    字节序 字节序是指多字节数据在计算机内存中存储或者网络传输时各字节的存储顺序 大端字节序 xff08 Big endian xff09 大端存储模式是指数据的高字节保存在内存的低地址中 xff0c 而数据的低字节保存在内存的高地址中 小端字
  • Qt简易计算器实现复数运算绘图等等

    太久没更新博客了前段时间弄了个计算器 实现了加减乘除复数 带记忆 绘图计算二维图形等功能 先简单讲解下我代码的分块 用图片描述虽然画的丑 实现效果图片 代码放心使用转载请通知 1 Ui 2 在Maindow 类实现计算 maindow h