![请添加图片描述](https://i-blog.csdnimg.cn/blog_migrate/a65a61dde46fd20894e27c084091273e.png)
第一步
在ui界面放入一个TableWidget和三个pushbutton按钮如下图所示
窗口布局和按钮位置可以随便摆放,但是按钮名称不能变
第二步
在头文件中声明函数
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QKeyEvent>
#include <QPaintEvent>
#include <QMouseEvent>
#include <QTableWidgetItem>
#include <QList>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void connectSlots();
//void keyPressEvent(QKeyEvent *);
void paintEvent(QPaintEvent *);
void mousePressEvent(QMouseEvent *);
private slots:
void on_btnAdd_clicked(); //按钮点击增加事件
void on_btnDelete_clicked(); //点击删除事件
void on_btnResult_clicked(); //获取计算的结果
private:
Ui::MainWindow *ui;
int row =3; //初始化行数
double a=0;
double b=0;
double result =0;
int cursor;
QTableWidgetItem *item;
QTableWidgetItem *itemOne;
QTableWidgetItem *itemTwo;
};
#endif // MAINWINDOW_H
第三步
函数的实现,这里采用了paintEvent事件去设置第三列禁止被编辑和选中,根据增减的内容进行实时更新
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHeaderView>
#include <QTableWidgetItem>
#include <QDebug>
#include <QTableWidget>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableWidget->setColumnCount(3);
ui->tableWidget->setRowCount(row);
ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "操作数1" << "操作数2" << "结果" );
ui->tableWidget->verticalHeader()->setVisible(true);
ui->tableWidget->horizontalHeaderItem(0)->setBackgroundColor(QColor(255,60,10)); //设置单元格背景颜色
item = new QTableWidgetItem();
connectSlots();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::connectSlots()
{
connect(ui->btnAdd,SIGNAL(clicked()),this,SLOT(on_btnAdd_clicked()),Qt::UniqueConnection);
connect(ui->btnDelete,SIGNAL(clicked()),this,SLOT(on_btnDelete_clicked()),Qt::UniqueConnection);
connect(ui->btnResult,SIGNAL(clicked()),this,SLOT(on_btnResult_clicked()),Qt::UniqueConnection);
}
void MainWindow::paintEvent(QPaintEvent *)
{
for(int i = 0; i< ui->tableWidget->rowCount();i++)
{
item = ui->tableWidget->item(i, 2);
if(item)
{
item->setFlags(item->flags() & (~Qt::ItemIsEditable&~Qt::ItemIsSelectable));
}
} //设置第三列禁止被编辑
}
void MainWindow::mousePressEvent(QMouseEvent *)
{
}
void MainWindow::on_btnAdd_clicked()
{
cursor = ui->tableWidget->currentRow();
ui->tableWidget->insertRow(cursor+1);
//qDebug() << cursor;
}
void MainWindow::on_btnDelete_clicked()
{
cursor = ui->tableWidget->currentRow(); //获取当前选中行的位置
if(cursor != -1) //若单元格为空,则不进行删除
{
ui->tableWidget->removeRow(cursor);
}
}
void MainWindow::on_btnResult_clicked()
{
for (int i = 0; i < ui->tableWidget->rowCount(); i++){
itemOne = ui->tableWidget->item(i, 0); //获取每一行第一个单元格对象
itemTwo = ui->tableWidget->item(i, 1); //第二个
if(itemOne && itemTwo)
{
a = ui->tableWidget->item(i, 0)->text().toDouble();
b = ui->tableWidget->item(i, 1)->text().toDouble();
if (a < 0) //输入为负数时,单元格设置为红色
ui->tableWidget->item(i, 0)->setBackgroundColor(Qt::red);
result = a / b;
qDebug() << "a=" << a << "b=" << b << "result=" << result;
ui->tableWidget->setItem(i, 2, new QTableWidgetItem(QString::number(result, 'g', 2)));
}
}
}