【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 ; }}