[C/C ++ school] (14) QT layout/arithmetic calculator, arithmetic Calculator

Source: Internet
Author: User

[C/C ++ school] (14) QT layout/arithmetic calculator, arithmetic Calculator
1. Layout

QVBoxLayout vertical layout

QHBoxLayout horizontal layout

QGridLayout table layout



#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 ("name ")); label2-> setText (tr ("Age"); btn1-> setText (tr ("OK"); btn2-> setText (tr ("canceled ")); QVBoxLayout * layout_main = new QVBoxLayout (this ); QHBoxLayout * layout1 = new QHBoxLayout (); QHBoxLayout * layout2 = new QHBoxLayout (); Parameters * layout3 = new QHBoxLayout (); layout1-> addWidget (label1 ); layout1-> addWidget (edit1); layout2-> addWidget (label2); layout2-> addWidget (edit2); layout3-> pull (btn1); layout3-> addWidget (btn2 ); layout_main-> addLayout (layout1); layout_main-> addLayout (layout2); layout_main-> addLayout (layout3);} Widget ::~ Widget (){}


2. QGridLayout table alignment

# 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 ("name ")); label2-> setText (tr ("Age"); btn1-> setText (tr ("OK"); btn2-> setText (tr ("canceled ")); QGridLayout * layout = new QGridLayout (this); layout-> addWidget (label1,); layout-> addWidget (edit1,); layout-> addWidget (label2 ); layout-> addWidget (edit2,); layout-> addWidget (btn1,); layout-> addWidget (btn2,);} Widget ::~ Widget (){}


3. Arithmetic Calculator

#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 ("Calculator"); // set the window title label1 = new QLabel; label1-> setText (tr ("enter a number ")); label2 = new QLabel; label2-> setText (tr ("Enter operator"); label3 = new QLabel; label3-> setText (tr ("Enter operator ")); label4 = new QLabel; label4-> setText (tr ("calculation result"); label5 = new QLabel; edit 1 = new QLineEdit; edit2 = new QLineEdit; edit3 = new QLineEdit; btn1 = new QPushButton; btn1-> setText (tr ("computing"); 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 () {// debug the output // QMessageBox: information (this, "", "hello, world "); QString str1 = edit1-> text (); QString operate = edit2-> text (); QString str2 = edit3-> text (); if (str1.isEmpty () {QMessageBox :: information (this, "error", "enter a number"); return;} if (operate. isEmpty () {QMessageBox: information (this, "error", "Enter the operator"); return;} if (str2.isEmpty () {QMessageBox: information (this, "error", "enter a number"); return;} int a = str1.toInt (); int B = str2.toInt (); QByteArray array = operate. toUtf8 (); // convert QString to QByteArray const char c = array [0]; // obtain the operator 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, "error", "divisor cannot be 0"); break;} label5-> setText (QString: number (a/B); break; default: QMessageBox: information (this, "error", operate + "not operator"); break ;}}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.