【C/C++學院】(14)QT布局/四則運算計算機,qt四則運算計算機

來源:互聯網
上載者:User

【C/C++學院】(14)QT布局/四則運算計算機,qt四則運算計算機
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 ;    }}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.