#ifndef CALCULATOR_H#define CALCULATOR_H#pragma once#include <QWidget>#include <QStack>class QLCDNumber;class QPushButton;class calculator : public QWidget{ Q_OBJECTprivate: QLCDNumber* m_plcd; QStack<QString> m_stk; QString m_strDisplay;public: calculator(QWidget *parent = 0); ~calculator(); QPushButton* createButton(const QString& str); void calculate ( );/*signals:public slots: void slotButtonClicked();*/};#endif // CALCULATOR_H
#include "calculator.h"#include <QLCDNumber>#include <QGridLayout>#include <QPushButton>#include <QDebug>calculator::calculator(QWidget *parent) : QWidget(parent){ m_plcd = new QLCDNumber(12); m_plcd->setSegmentStyle(QLCDNumber::Flat); m_plcd->setMinimumSize(150, 50); QChar aButtons [4] [4] = {{'7', '8', '9', '/'}, {'4', '5', '6', '*'}, {'1', '2', '3', '-'}, {'0', '.', '=', '+'} }; QGridLayout* ptopLayout = new QGridLayout; ptopLayout->addWidget(m_plcd, 0, 0, 1, 4); ptopLayout->addWidget(createButton("CE"), 1, 3); for (int i=0; i<4; ++i){ for(int j=0; j<4; ++j){ ptopLayout->addWidget(createButton(aButtons[i][j]), i+2, j); } } setLayout(ptopLayout);}calculator::~calculator(){}
#include "calculator.h"#include <QApplication>int main(int argc, char *argv[]){ QApplication a(argc, argv); calculator Calculator; Calculator.setWindowTitle("Calculator"); Calculator.resize(230,200); Calculator.show(); return a.exec();}
#include "calculator.h"#include <QLCDNumber>#include <QGridLayout>#include <QPushButton>#include <QDebug>calculator::calculator(QWidget *parent) : QWidget(parent){ m_plcd = new QLCDNumber(12); m_plcd->setSegmentStyle(QLCDNumber::Flat); m_plcd->setMinimumSize(150, 50); QChar aButtons [4] [4] = {{'7', '8', '9', '/'}, {'4', '5', '6', '*'}, {'1', '2', '3', '-'}, {'0', '.', '=', '+'} }; QGridLayout* ptopLayout = new QGridLayout; ptopLayout->addWidget(m_plcd, 0, 0, 1, 4); ptopLayout->addWidget( new QPushButton("CE"), 1, 3); for (int i=0; i<4; ++i){ for(int j=0; j<4; ++j){ ptopLayout->addWidget(new QPushButton(aButtons[i][j]), i+2, j); } } setLayout(ptopLayout);}calculator::~calculator(){}
C++ (Qt)QPushButton* calculator::createButton(const QString& str){ return ... trololo ...;}
return ... trololo ...;
QPushButton* createButton(const QString& str){ QPushButton* button = new QPushButton(str); return button; }
QPushButton* calculator::createButton(const QString& str){ QPushButton* button = new QPushButton(str); return button; }