将输入改成摆放九个按钮
时间: 2023-09-08 07:14:52 浏览: 36
好的,根据您的要求,以下是将输入改为九个按钮的示例代码:
mainwindow.ui文件:
在界面设计器中,您需要移除原来的QLineEdit控件,并添加九个QPushButton控件,分别显示1到9的数字。
```xml
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Calculator</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="button1">
<property name="geometry">
<rect>
<x>30</x>
<y>60</y>
<width>75</width>
<height>30</height>
</rect>
</property>
<property name="text">
<string>1</string>
</property>
</widget>
<widget class="QPushButton" name="button2">
<property name="geometry">
<rect>
<x>120</x>
<y>60</y>
<width>75</width>
<height>30</height>
</rect>
</property>
<property name="text">
<string>2</string>
</property>
</widget>
<widget class="QPushButton" name="button3">
<property name="geometry">
<rect>
<x>210</x>
<y>60</y>
<width>75</width>
<height>30</height>
</rect>
</property>
<property name="text">
<string>3</string>
</property>
</widget>
<widget class="QPushButton" name="button4">
<property name="geometry">
<rect>
<x>30</x>
<y>100</y>
<width>75</width>
<height>30</height>
</rect>
</property>
<property name="text">
<string>4</string>
</property>
</widget>
<widget class="QPushButton" name="button5">
<property name="geometry">
<rect>
<x>120</x>
<y>100</y>
<width>75</width>
<height>30</height>
</rect>
</property>
<property name="text">
<string>5</string>
</property>
</widget>
<widget class="QPushButton" name="button6">
<property name="geometry">
<rect>
<x>210</x>
<y>100</y>
<width>75</width>
<height>30</height>
</rect>
</property>
<property name="text">
<string>6</string>
</property>
</widget>
<widget class="QPushButton" name="button7">
<property name="geometry">
<rect>
<x>30</x>
<y>140</y>
<width>75</width>
<height>30</height>
</rect>
</property>
<property name="text">
<string>7</string>
</property>
</widget>
<widget class="QPushButton" name="button8">
<property name="geometry">
<rect>
<x>120</x>
<y>140</y>
<width>75</width>
<height>30</height>
</rect>
</property>
<property name="text">
<string>8</string>
</property>
</widget>
<widget class="QPushButton" name="button9">
<property name="geometry">
<rect>
<x>210</x>
<y>140</y>
<width>75</width>
<height>30</height>
</rect>
</property>
<property name="text">
<string>9</string>
</property>
</widget>
<widget class="QPushButton" name="calculateButton">
<property name="geometry">
<rect>
<x>300</x>
<y>60</y>
<width>75</width>
<height>110</height>
</rect>
</property>
<property name="text">
<string>=</string>
</property>
</widget>
<widget class="QLabel" name="resultLabel">
<property name="geometry">
<rect>
<x>30</x>
<y>20</y>
<width>341</width>
<height>31</height>
</rect>
</property>
<property name="font">
<font>
<pointsize>16</pointsize>
</font>
</property>
<property name="text">
<string>0</string>
</property>
<property name="alignment">
<set>Qt::AlignRight</set>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>
```
mainwindow.h文件:
```c++
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_button1_clicked();
void on_button2_clicked();
void on_button3_clicked();
void on_button4_clicked();
void on_button5_clicked();
void on_button6_clicked();
void on_button7_clicked();
void on_button8_clicked();
void on_button9_clicked();
void on_calculateButton_clicked();
private:
Ui::MainWindow *ui;
QString input; // 记录输入的字符串
};
#endif // MAINWINDOW_H
```
mainwindow.cpp文件:
```c++
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
input = ""; // 初始化输入字符串为空
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_button1_clicked()
{
input += "1";
ui->resultLabel->setText(input);
}
void MainWindow::on_button2_clicked()
{
input += "2";
ui->resultLabel->setText(input);
}
void MainWindow::on_button3_clicked()
{
input += "3";
ui->resultLabel->setText(input);
}
void MainWindow::on_button4_clicked()
{
input += "4";
ui->resultLabel->setText(input);
}
void MainWindow::on_button5_clicked()
{
input += "5";
ui->resultLabel->setText(input);
}
void MainWindow::on_button6_clicked()
{
input += "6";
ui->resultLabel->setText(input);
}
void MainWindow::on_button7_clicked()
{
input += "7";
ui->resultLabel->setText(input);
}
void MainWindow::on_button8_clicked()
{
input += "8";
ui->resultLabel->setText(input);
}
void MainWindow::on_button9_clicked()
{
input += "9";
ui->resultLabel->setText(input);
}
void MainWindow::on_calculateButton_clicked()
{
// 计算
QString result = QString::number(eval(input.toStdString().c_str()));
// 输出结果
ui->resultLabel->setText(result);
// 清空输入
input = "";
}
```
eval函数的实现可以与之前保持一致,这里不再赘述。
这样,您就可以在九个按钮上输入数字,点击“=”按钮进行计算了。