【C/C++学院】(14)QT布局/四则运算计算器

来源:互联网 发布:模拟飞行软件 编辑:程序博客网 时间:2024/05/29 09:21

1.布局

QVBoxLayout垂直布局

QHBoxLayout水平布局

QGridLayout表格布局



#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QLabel>#include <QLineEdit>#include <QPushButton>class Widget : public QWidget{    Q_OBJECTpublic:    Widget(QWidget *parent = 0);    ~Widget();private:    QLineEdit *edit1, *edit2;    QLabel *label1, *label2;    QPushButton *btn1, *btn2;};#endif // WIDGET_H

#include "widget.h"#include <QVBoxLayout>#include <QHBoxLayout>Widget::Widget(QWidget *parent)    : QWidget(parent){    edit1 = new QLineEdit;    edit2 = new QLineEdit;    label1 = new QLabel;    label2 = new QLabel;    btn1 = new QPushButton;    btn2 = new QPushButton;    label1->setText(tr("姓名"));    label2->setText(tr("年龄"));    btn1->setText(tr("确定"));    btn2->setText(tr("取消"));    QVBoxLayout *layout_main = new QVBoxLayout(this);    QHBoxLayout *layout1 = new QHBoxLayout();    QHBoxLayout *layout2 = new QHBoxLayout();    QHBoxLayout *layout3 = new QHBoxLayout();    layout1->addWidget(label1);    layout1->addWidget(edit1);    layout2->addWidget(label2);    layout2->addWidget(edit2);    layout3->addWidget(btn1);    layout3->addWidget(btn2);    layout_main->addLayout(layout1);    layout_main->addLayout(layout2);    layout_main->addLayout(layout3);}Widget::~Widget(){}


2.QGridLayout表格对齐

#include "widget.h"#include <QVBoxLayout>#include <QHBoxLayout>#include <QGridLayout>Widget::Widget(QWidget *parent)    : QWidget(parent){    edit1 = new QLineEdit;    edit2 = new QLineEdit;    label1 = new QLabel;    label2 = new QLabel;    btn1 = new QPushButton;    btn2 = new QPushButton;    label1->setText(tr("姓名"));    label2->setText(tr("年龄"));    btn1->setText(tr("确定"));    btn2->setText(tr("取消"));    QGridLayout *layout = new QGridLayout(this);    layout->addWidget(label1, 0,0);    layout->addWidget(edit1, 0,1);    layout->addWidget(label2, 1,0);    layout->addWidget(edit2, 1,1);    layout->addWidget(btn1, 2,0);    layout->addWidget(btn2, 2,1);}Widget::~Widget(){}


3.四则运算计算器

#ifndef DIALOG_H#define DIALOG_H#include <QDialog>#include <QLabel>#include <QPushButton>#include <QLineEdit>class Dialog : public QDialog{    Q_OBJECTpublic:    Dialog(QWidget *parent = 0);    ~Dialog();private slots:    void on_click();private:    QLabel *label1, *label2, *label3, *label4, *label5;    QLineEdit *edit1, *edit2, *edit3;    QPushButton *btn1;};#endif // DIALOG_H

#include "dialog.h"#include <QGridLayout>#include <QVBoxLayout>#include <QMessageBox>Dialog::Dialog(QWidget *parent)    : QDialog(parent){    setWindowTitle(tr("计算器"));//设置窗口标题    label1 = new QLabel;    label1->setText(tr("请输入数字"));    label2 = new QLabel;    label2->setText(tr("请输入运算符"));    label3 = new QLabel;    label3->setText(tr("请输入运算符"));    label4 = new QLabel;    label4->setText(tr("计算结果"));    label5 = new QLabel;    edit1 = new QLineEdit;    edit2 = new QLineEdit;    edit3 = new QLineEdit;    btn1 = new QPushButton;    btn1->setText(tr("计算"));    QGridLayout *layout1 = new QGridLayout;    QVBoxLayout *layout2 = new QVBoxLayout(this);    layout1->addWidget(label1, 0, 0);    layout1->addWidget(edit1, 0, 1);    layout1->addWidget(label2, 1, 0);    layout1->addWidget(edit2, 1, 1);    layout1->addWidget(label3, 2, 0);    layout1->addWidget(edit3, 2, 1);    layout1->addWidget(label4, 3, 0);    layout1->addWidget(label5, 3, 1);    layout2->addLayout(layout1);    layout2->addWidget(btn1);    connect(btn1, SIGNAL(clicked()), this, SLOT(on_click()));}Dialog::~Dialog(){}void Dialog::on_click(){    //调试输出    //QMessageBox::information(this, "", "hello, world");    QString str1 = edit1->text();    QString operate = edit2->text();    QString str2 = edit3->text();    if (str1.isEmpty())    {        QMessageBox::information(this, "错误", "请输入数字");        return ;    }    if (operate.isEmpty())    {        QMessageBox::information(this, "错误", "请输入运算符");        return ;    }    if (str2.isEmpty())    {        QMessageBox::information(this, "错误", "请输入数字");        return ;    }    int a = str1.toInt();    int b = str2.toInt();    QByteArray array = operate.toUtf8();//将QString转化为QByteArray    const char c = array[0];//获取运算符    switch(c)    {    case '+':        label5->setText(QString::number(a+b));        break;    case '-':        label5->setText(QString::number(a-b));        break;    case '*':        label5->setText(QString::number(a*b));        break;    case '/':        if(b == 0)        {            QMessageBox::information(this, "错误", "除数不能为0");            break ;        }        label5->setText(QString::number(a/b));        break;    default:        QMessageBox::information(this, "错误", operate+"不是运算符");        break ;    }}




0 0
原创粉丝点击