4 #define CONNECT_NUMBUTTON(button) \
5 connect(button, SIGNAL(clicked(bool)), this, \
6 SLOT(slot_numButtonClicked()))
8 #define CONNECT_OPERBUTTON(button) \
9 connect(button, SIGNAL(clicked(bool)), this, \
10 SLOT(slot_operButtonClicked()))
12 #define DISABLE_BUTTON(button) button->setEnable(false)
13 #define ENABLE_BUTTON(button) button->setEnable(true)
15 MyWidget::MyWidget(QWidget
*parent
)
18 m_ui
= new Ui::MyWidgetBase();
21 CONNECT_NUMBUTTON(m_ui
->m_button0
);
22 CONNECT_NUMBUTTON(m_ui
->m_button1
);
23 CONNECT_NUMBUTTON(m_ui
->m_button2
);
24 CONNECT_NUMBUTTON(m_ui
->m_button3
);
25 CONNECT_NUMBUTTON(m_ui
->m_button4
);
26 CONNECT_NUMBUTTON(m_ui
->m_button5
);
27 CONNECT_NUMBUTTON(m_ui
->m_button6
);
28 CONNECT_NUMBUTTON(m_ui
->m_button7
);
29 CONNECT_NUMBUTTON(m_ui
->m_button8
);
30 CONNECT_NUMBUTTON(m_ui
->m_button9
);
32 CONNECT_OPERBUTTON(m_ui
->m_buttonSum
);
33 CONNECT_OPERBUTTON(m_ui
->m_buttonMult
);
34 CONNECT_OPERBUTTON(m_ui
->m_buttonSub
);
35 CONNECT_OPERBUTTON(m_ui
->m_buttonDiv
);
36 CONNECT_OPERBUTTON(m_ui
->m_buttonRes
);
46 void MyWidget::slot_numButtonClicked()
48 QPushButton
*btn
= dynamic_cast<QPushButton
*>(sender());
52 QString text
= m_ui
->m_display
->text();
59 m_ui
->m_display
->setText(text
);
62 void MyWidget::buttonsEnabled(bool value
)
64 m_ui
->m_buttonSum
->setEnabled(value
);
65 m_ui
->m_buttonMult
->setEnabled(value
);
66 m_ui
->m_buttonSub
->setEnabled(value
);
67 m_ui
->m_buttonDiv
->setEnabled(value
);
68 m_ui
->m_buttonRes
->setEnabled(!value
);
71 void MyWidget::slot_operButtonClicked()
74 QPushButton
*btn
= dynamic_cast<QPushButton
*>(sender());
80 if (btn
->text() == "=") {
81 int lvalue
= m_ui
->m_display
->text().toInt();
83 result
= m_number
+ lvalue
;
84 else if (m_oper
== "-")
85 result
= m_number
- lvalue
;
86 else if (m_oper
== "*")
87 result
= m_number
* lvalue
;
89 result
= m_number
/ lvalue
;
90 m_ui
->m_display
->setText(QString::number(result
));
94 m_number
= m_ui
->m_display
->text().toInt();
95 buttonsEnabled(false);