1.主要代码
//customer.h文件
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <QObject>
#include <string>
class Customer : public QObject
{
Q_OBJECT
public:
explicit Customer(QObject *parent = nullptr);
Customer(const Customer& customer);
void setCompanyName(std::string name);
void setCompanyContact(std::string contact);
void setCompanyContactTel(std::string tel);
void operator =(const Customer& customer);
std::string getCompanyName();
std::string getCompanyContact();
std::string getCompanyContactTel();
private:
std::string m_CompanyName;
std::string m_Contact;
std::string m_ContactTel;
};
#endif // CUSTOMER_H
//customer.cpp文件
#include "customer.h"
Customer::Customer(QObject *parent) : QObject(parent)
{
this->m_CompanyName = "";
this->m_Contact = "";
this->m_ContactTel = "";
}
void Customer::setCompanyName(std::string name)
{
m_CompanyName = name;
}
void Customer::setCompanyContact(std::string contact)
{
m_Contact = contact;
}
void Customer::setCompanyContactTel(std::string tel)
{
m_ContactTel = tel;
}
void Customer::operator =(const Customer &customer)
{
this->m_CompanyName = customer.m_CompanyName;
this->m_Contact = customer.m_Contact;
this->m_ContactTel = customer.m_ContactTel;
}
std::string Customer::getCompanyName()
{
return this->m_CompanyName;
}
std::string Customer::getCompanyContact()
{
return this->m_Contact;
}
std::string Customer::getCompanyContactTel()
{
return this->m_ContactTel;
}
Customer::Customer(const Customer &customer)
{
this->m_CompanyName = customer.m_CompanyName;
this->m_Contact = customer.m_Contact;
this->m_ContactTel = customer.m_ContactTel;
}
#ifndef GOODDILOG_H
#define GOODDILOG_H
#include <QDialog>
#include <QStandardItem>
#include <QList>
#include "goods.h"
namespace Ui {
class GoodDilog;
}
class GoodDilog : public QDialog
{
Q_OBJECT
public:
explicit GoodDilog(QWidget *parent = 0);
~GoodDilog();
QList<QStandardItem*> getGoodsInputParam();
QList<QString> getGoodsInput();
void setUiParam(Goods& g);
private:
Ui::GoodDilog *ui;
};
#endif // GOODDILOG_H
#include "gooddilog.h"
#include "ui_gooddilog.h"
GoodDilog::GoodDilog(QWidget *parent) :
QDialog(parent),
ui(new Ui::GoodDilog)
{
ui->setupUi(this);
}
GoodDilog::~GoodDilog()
{
delete ui;
}
QList<QStandardItem *> GoodDilog::getGoodsInputParam()
{
QList<QStandardItem *> rowList;
rowList << new QStandardItem(ui->lineEditName->text()) << new QStandardItem(ui->lineEditType->text())
<< new QStandardItem(ui->lineEditUnit->text()) << new QStandardItem(ui->lineEditPrice->text());
return rowList;
}
QList<QString> GoodDilog::getGoodsInput()
{
QList<QString> rowList;
rowList << ui->lineEditName->text() << ui->lineEditType->text()
<< ui->lineEditUnit->text() << ui->lineEditPrice->text();
return rowList;
}
void GoodDilog::setUiParam(Goods &g)
{
ui->lineEditName->setText(QString::fromStdString(g.getGoodsName()));
ui->lineEditType->setText(QString::fromStdString(g.getGoodsType()));
ui->lineEditUnit->setText(QString::fromStdString(g.getGoodsUnit()));
ui->lineEditPrice->setText(QString::fromStdString(std::to_string(g.getGoodsUnitPrice())));
}
#ifndef GOODS_H
#define GOODS_H
#include <QObject>
#include <string>
class Goods : public QObject
{
Q_OBJECT
public:
explicit Goods(QObject *parent = nullptr);
Goods(const Goods& goods);
void setGoodsName(std::string name);
void setGoodsType(std::string type);
void setGoodsUnitPrice(double unitprice);
void setGoodsUnit(std::string unit);
void operator =(const Goods& goods);
std::string getGoodsName();
std::string getGoodsType();
double getGoodsUnitPrice();
std::string getGoodsUnit();
private:
std::string m_GoodsName;
std::string m_GoodsType;
double m_UnitPrice;
std::string m_Unit;
};
#endif // GOODS_H
#include "goods.h"
Goods::Goods(QObject *parent) : QObject(parent)
{
this->m_GoodsName = "";
this->m_GoodsType = "";
this->m_Unit = "";
this->m_UnitPrice = 0.0;
}
Goods::Goods(const Goods &goods)
{
this->m_GoodsName = goods.m_GoodsName;
this->m_GoodsType = goods.m_GoodsType;
this->m_Unit = goods.m_Unit;
this->m_UnitPrice = goods.m_UnitPrice;
}
void Goods::setGoodsName(std::string name)
{
m_GoodsName = name;
}
void Goods::setGoodsType(std::string type)
{
m_GoodsType = type;
}
void Goods::setGoodsUnitPrice(double unitprice)
{
m_UnitPrice = unitprice;
}
void Goods::setGoodsUnit(std::string unit)
{
m_Unit = unit;
}
void Goods::operator =(const Goods &goods)
{
this->m_GoodsName = goods.m_GoodsName;
this->m_GoodsType = goods.m_GoodsType;
this->m_Unit = goods.m_Unit;
this->m_UnitPrice = goods.m_UnitPrice;
}
std::string Goods::getGoodsName()
{
return this->m_GoodsName;
}
std::string Goods::getGoodsType()
{
return this->m_GoodsType;
}
double Goods::getGoodsUnitPrice()
{
return this->m_UnitPrice;
}
std::string Goods::getGoodsUnit()
{
return this->m_Unit;
}
#ifndef TABLEVIEWONLYREAD_H
#define TABLEVIEWONLYREAD_H
#include <QItemDelegate>
class TableViewOnlyRead : public QItemDelegate
{
Q_OBJECT
public:
TableViewOnlyRead(QObject *parent = 0): QItemDelegate(parent) { }
QWidget *createEditor(QWidget*parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
return NULL;
}
};
#endif // TABLEVIEWONLYREAD_H
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPrinter>
#include <QPrintDialog>
#include <QPrintPreviewDialog>
#include <QPainter>
#include <QStandardItemModel>
#include <QMenu>
#include <QString>
#include <QItemDelegate>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <QDate>
#include <QMessageBox>
#include "tableviewonlyread.h"
#include "customer.h"
#include "gooddilog.h"
#include "goods.h"
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
//添加列表中的数据和设置布局信息
void AddTableViewData();
void setTableViewColumnOnlyRead();
void setTableViewDataAlignment();
//打印表格信息
void drawDocHeader(QPainter& Painter);
void drawTableAndData(QPainter& Painter);
void drawDocTail(QPainter& Painter);
//判断数据是否存在
bool isExistence(QString info,QString mode);
//更新数据
void UpData();
void onBtnSave();
void setTableStyle();
//小写转大写
QString LowerToUpper(double money);
void ModifyMoney(int row,int column);
void ModifySumMoney();
private slots:
void Preview(QPrinter* print);
void PrintPreview();
void slotContextMenu(QPoint pos);
void SetComboBoxSelctedData(const QString& text);
void InsertTableViewRow();
void DeleteTableViewRow();
void ModifyTableViewRow();
void on_BtnSave_clicked();
void CalTableData();
private:
Ui::Widget* ui;
QStandardItemModel* model;
QMenu* popMenu;
std::vector<Goods> m_GoodsArray; //商品信息
std::vector<Customer> m_CustomerArray;//客户信息
Customer m_Personal; //个人信息
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
/**
* @brief Widget::Widget,构造函数,用于初始化信息
* @param parent
*/
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
//初始化窗口信息
this->setWindowTitle("销售清单打印系统");
this->setMaximumSize(1230,660);
this->setMinimumSize(1230,660);
//创建列表右键菜单
popMenu = new QMenu(ui->tableView);
QAction *InsertRow = new QAction();
QAction *DeleteRow = new QAction();
QAction *ModifyRow = new QAction();
InsertRow -> setText(QString("插入行"));
DeleteRow -> setText(QString("删除行"));
ModifyRow -> setText(QString("修改行"));
popMenu -> addAction(InsertRow);
popMenu -> addAction(DeleteRow);
popMenu -> addAction(ModifyRow);
model = new QStandardItemModel();
//绑定信号和槽
connect(ui->BtnPrintPre,SIGNAL(clicked()),this,SLOT(PrintPreview()));//打印预览
connect(ui->tableView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(slotContextMenu(QPoint)));//TableView右键菜单响应
connect(ui->comboBoxCompany, SIGNAL(currentIndexChanged(QString)), this, SLOT(SetComboBoxSelctedData(QString)));//下拉列表框响应
connect(model,SIGNAL(itemChanged(QStandardItem*)),this,SLOT(CalTableData()));//响应单元格编辑消息
//右键菜单功能绑定槽
connect(InsertRow, SIGNAL(triggered()), this, SLOT(InsertTableViewRow()));
connect(DeleteRow, SIGNAL(triggered()), this, SLOT(DeleteTableViewRow()));
connect(ModifyRow, SIGNAL(triggered()), this, SLOT(ModifyTableViewRow()));
//添加商品数据
Goods goods;
goods.setGoodsName("面向对象C++程序设计");
goods.setGoodsType("必修");
goods.setGoodsUnit("本");
goods.setGoodsUnitPrice(58.5);
this->m_GoodsArray.push_back(goods);
Goods goods1;
goods1.setGoodsName("面向对象C++程序设计");
goods1.setGoodsType("必修");
goods1.setGoodsUnit("本");
goods1.setGoodsUnitPrice(58.5);
this->m_GoodsArray.push_back(goods1);
//设置日期
ui->dateEdit->setDate(QDate::currentDate());
//更新界面控件信息
setTableStyle();
UpData();
}
Widget::~Widget()
{
delete ui;
}
/**
* @brief Widget::AddTableViewData,表格控件添加行和列,数据
*/
void Widget::AddTableViewData()
{
//添加数据
for (int row = 0;row < m_GoodsArray.size();++row)
{
model->setItem(row,0, new QStandardItem(QString::fromStdString(m_GoodsArray[row].getGoodsName())));
model->setItem(row,1, new QStandardItem(QString::fromStdString(m_GoodsArray[row].getGoodsType())));
model->setItem(row,2, new QStandardItem(QString::fromStdString(m_GoodsArray[row].getGoodsUnit())));
model->setItem(row,3, new QStandardItem(QString::fromStdString(std::to_string(m_GoodsArray[row].getGoodsUnitPrice()))));
model->setItem(row,4, new QStandardItem(QString("0")));
model->setItem(row,5, new QStandardItem(QString("0")));
model->setItem(row,6, new QStandardItem(QString("")));
}
//设置最后一行格式
if (m_GoodsArray.size() > 0)
{
model->setItem(m_GoodsArray.size(),0, new QStandardItem(QString("")));
model->setItem(m_GoodsArray.size(),1, new QStandardItem(QString("")));
model->setItem(m_GoodsArray.size(),2, new QStandardItem(QString("")));
model->setItem(m_GoodsArray.size(),3, new QStandardItem(QString("")));
ui->tableView->setSpan(m_GoodsArray.size(),0,m_GoodsArray.size(),5);
model->setItem(m_GoodsArray.size(), 0, new QStandardItem(QString::fromStdString("合计金额(大写):")));
model->setItem(m_GoodsArray.size(), 5, new QStandardItem(QString::fromStdString("¥")));
model->setItem(m_GoodsArray.size(),6, new QStandardItem(QString("")));
}
}
/**
* @brief Widget::setTableViewColumnOnlyRead,设置表格指定列为只读属性
*/
void Widget::setTableViewColumnOnlyRead()
{
for (int row = 0;row < m_GoodsArray.size();++row)
{
ui->tableView->setItemDelegateForColumn(0, new TableViewOnlyRead);
ui->tableView->setItemDelegateForColumn(1, new TableViewOnlyRead);
ui->tableView->setItemDelegateForColumn(2, new TableViewOnlyRead);
ui->tableView->setItemDelegateForColumn(3, new TableViewOnlyRead);
ui->tableView->setItemDelegateForColumn(5, new TableViewOnlyRead);
}
}
/**
* @brief Widget::setTableViewDataAlignment,设置TableView表格的数据的布局方式
*/
void Widget::setTableViewDataAlignment()
{
for (int row = 0;row < m_GoodsArray.size();++row)
{
model ->item(row,0)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
model ->item(row,1)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
model ->item(row,2)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
model ->item(row,3)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
//model ->item(row,4)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
//model ->item(row,5)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
//model ->item(row,6)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
}
if (m_GoodsArray.size() > 0)
{
model ->item(m_GoodsArray.size(),0)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
model ->item(m_GoodsArray.size(),5)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
//model ->item(m_GoodsArray.size(),6)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
}
}
/**
* @brief Widget::drawDocHeader,绘制打印文档头部区域
* @param Painter
*/
void Widget::drawDocHeader(QPainter &Painter)
{
//文档头部区域布局参数
int x1 = 100,y1 =60;
int x2 = 780,y2 =130;
//字体设置
QFont Font;
QFont OldFont = Painter.font();
Font.setPointSize(20);
Painter.setFont(Font);
//绘制个人信息和单号
Painter.drawText(x1 + (x2-x1) / 6 ,y1 ,ui->EditPersonalCompanyName->text());
Painter.setFont(OldFont);
Painter.drawText(x2 - 155,y1 ,"销售送货清单:");
Font.setPointSize(10);
Painter.setFont(Font);
Painter.drawText(x2 - 80,y1 ,ui->EditPersonalListID->text());
Painter.setFont(OldFont);
//绘制客户信息
Painter.drawText(x1,y1 + 40,"客户名称:" + ui->comboBoxCompany->currentText());
Painter.drawText(x2 - 110,y1 + 40 ,"日 期:" + ui->dateEdit->text());
//绘制水平分割线
Painter.drawLine(QPointF(x1, y2 -25), QPointF(x2, y2 -25));
Painter.drawText(x1, y2 -10,"联系人:" + ui->EditCustomerContact->text());
Painter.drawText(x1 + (x2-x1) / 2 ,y2 -10,"联系电话:" + ui->EditCustomerContactTel->text());
}
/**
* @brief Widget::drawTableAndData,绘制打印文档的表格区域
* @param Painter
*/
void Widget::drawTableAndData(QPainter& Painter)
{
//表头数据
QString columnName[] = {"序号","商品名称","规格型号","单位","单价","数量","金额","备注说明"};
//等宽列,列相对于列宽的位置偏移量,用于控制列的宽度
int columnWidth[] = {0,-30,30,30,25,20,20,5,0};
//行列数
int row = 9,col = 8;
//表格起点与终点
int x1 = 100,y1 = 130;
int x2 = 780,y2 =319;
//行高列宽
int rowHeight = (y2 -y1) / row;
int colWidth = (x2-x1) / col;
//表格中数据的内边距
int paddingTop = 5;
int paddingLeft = 15;
for (int i = 0;i <= row;i++)
{
//绘制行
Painter.drawLine(QPointF(x1, y1 + i * rowHeight), QPointF(x2, y1 + i * rowHeight));
for (int j = 0;j <= col;j++)
{
if (i == 0)
{
//用于控制列的终点,绘制不同长短的列
int Type = -1;
if (j == 0 || j > 5)
{
Type = 0;
}
//绘制列
Painter.drawLine(QPointF(x1 + j * colWidth + columnWidth[j], y1), QPointF(x1 + j * colWidth + columnWidth[j], y2 + Type * rowHeight));
//绘制表头
if (i == 0 && j < col)
{
Painter.drawText(QPointF(x1 + j * colWidth + columnWidth[j] + paddingLeft, y1 + rowHeight / 2 + paddingTop), columnName[j]);
}
}
//绘制数据
else if (i >= 1 && i - 1 < m_GoodsArray.size() && i < row -1)
{
QString ColData;
//取出数组中的数据
switch (j)
{
case 0: ColData = QString::fromStdString(std::to_string(i)); break;
case 1: ColData = QString::fromStdString(m_GoodsArray[i-1].getGoodsName()); break;
case 2: ColData = QString::fromStdString(m_GoodsArray[i-1].getGoodsType()); break;
case 3: ColData = QString::fromStdString(m_GoodsArray[i-1].getGoodsUnit()); break;
case 4: ColData = QString::fromStdString(std::to_string(m_GoodsArray[i-1].getGoodsUnitPrice())); break;
default: ColData = nullptr; break;
}
//添加打印表格数据
if (ColData != nullptr)
{
Painter.drawText(QPointF(x1 + j * colWidth + columnWidth[j] + paddingLeft, y1 + i * rowHeight + rowHeight / 2 + paddingTop), ColData);
}
}
//处理列表最后一行
else if (i == row-1)
{
if (j == 0)
{
Painter.drawText(QPointF(x1 + paddingLeft , y1 + i * rowHeight + rowHeight / 2 + paddingTop),model->item(m_GoodsArray.size(), 0)->text());
}
else if (j == 6)
{
Painter.drawText(QPointF(x1 + j * colWidth + columnWidth[j] + paddingLeft, y1 + i * rowHeight + rowHeight / 2 + paddingTop), model->item(m_GoodsArray.size(), 6)->text());
}
}
}
}
}
/**
* @brief Widget::drawDocTail,设置打印文档的尾部区域信息
* @param Painter
*/
void Widget::drawDocTail(QPainter &Painter)
{
//文档底部区域布局参数
int x1 = 100,y1 = 319;
int x2 = 780,y2 =500;
Painter.drawText(QPointF(x1 ,y1 +30 ),"送货单位及经手人(盖章):");
Painter.drawText(QPointF(x1 + (x2-x1) / 3 * 2 ,y1 + 30 ),"收货单位及经手人(盖章):");
QFont Font;
QFont OldFont = Painter.font();
Font.setBold(QFont::Black);
Painter.setFont(Font);
Painter.drawText(QPointF(x1 + (x2-x1) / 4 ,y1 + 70 ),"请核对以上清单,如有问题请在三天之内联系我们,逾期概不负责!");
Painter.setFont(OldFont);
Painter.drawText(QPointF(x1 + (x2-x1) / 9 ,y1 + 100 ),"(一)存根:白");
Painter.drawText(QPointF(x1 + (x2-x1) / 9 * 4,y1 + 100 ),"(二)客户:红");
Painter.drawText(QPointF(x1 + (x2-x1) / 9 * 7,y1 + 100 ),"(三)结算:黄");
Painter.drawText(QPointF(x2 - 120 ,y1 +130 ),"送货电话:" + ui->EditPersonalContactTel->text());
}
/**
* @brief Widget::isExistence,判断用户信息是否存在
* @param info
* @param mode
* @return
*/
bool Widget::isExistence(QString info, QString mode)
{
if (mode.toLower() == "p")
{
if (QString::fromStdString(m_Personal.getCompanyName()) == info)
return true;
}
else if (mode.toLower() == "c")
{
for (int i=0;i< m_CustomerArray.size();i++)
{
if (QString::fromStdString(m_CustomerArray[i].getCompanyName()) == info)
return true;
}
}
return false;
}
/**
* @brief Widget::PrintPreview,打印预览
*/
void Widget::PrintPreview()
{
//打印预览设置
QPrinter Printer;
QPrintPreviewDialog PrintPreview(&Printer,this);
connect(&PrintPreview,SIGNAL(paintRequested(QPrinter*)),this,SLOT(Preview(QPrinter*) ) );
PrintPreview.exec();
}
/**
* @brief Widget::Preview,打印预览,设置纸张大小
* @param Printer
*/
void Widget::Preview(QPrinter *Printer)
{
//设置打印纸的大小
Printer->setPaperSize(QPrinter::Custom);
Printer->setPageSizeMM(QSizeF(241,140));
//打印预览数据设置
QPainter Painter;
Painter.begin(Printer);
drawDocHeader(Painter);
drawTableAndData(Painter);
drawDocTail(Painter);
Painter.end();
}
/**
* @brief Widget::slotContextMenu,TableView控件响应右键菜单槽函数
* @param pos
*/
void Widget::slotContextMenu(QPoint pos)
{
//鼠标点击位置确定,显示右键菜单
auto index = ui->tableView->indexAt(pos);
if (index.isValid())
{
popMenu->exec(QCursor::pos());
}
}
/**
* @brief Widget::SetComboBoxSelctedData,下拉列表框响应选项改变
* @param text
*/
void Widget::SetComboBoxSelctedData(const QString& text)
{
for (int i=0;i<m_CustomerArray.size();++i)
{
if (QString::fromStdString(m_CustomerArray[i].getCompanyName()) == text)
{
ui->EditCustomerContact->setText(QString::fromStdString(m_CustomerArray[i].getCompanyContact()));
ui->EditCustomerContactTel->setText(QString::fromStdString(m_CustomerArray[i].getCompanyContactTel()));
}
}
}
/**
* @brief Widget::InsertTableViewRow,插入行
*/
void Widget::InsertTableViewRow()
{
//获取选中的行
int row = ui->tableView->currentIndex().row();
GoodDilog dlg;
if(dlg.exec() == QDialog::Accepted)
{
model->insertRow(row, dlg.getGoodsInputParam());
if (row >=0)
{
Goods goods;
goods.setGoodsName(model->item(row, 0)->text().toStdString());
goods.setGoodsType(model->item(row, 1)->text().toStdString());
goods.setGoodsUnit(model->item(row, 2)->text().toStdString());
goods.setGoodsUnitPrice(atof(model->item(row, 3)->text().toStdString().c_str()));
if (row < m_GoodsArray.size())
{
m_GoodsArray.insert(m_GoodsArray.begin()+ row,goods);
}
else
{
m_GoodsArray.push_back(goods);
}
setTableViewDataAlignment();
}
}
}
/**
* @brief Widget::DeleteTableViewRow,删除行
*/
void Widget::DeleteTableViewRow()
{
int index = ui->tableView->currentIndex().row();
model->removeRow(index);
if ( index >=0 && index < m_GoodsArray.size())
{
//复制构造函数和重载赋值函数,需要加const,否则在linux中编译不通过
m_GoodsArray.erase(m_GoodsArray.begin() + index);
}
ModifySumMoney();
}
/**
* @brief Widget::ModifyTableViewRow,修改行的内容
*/
void Widget::ModifyTableViewRow()
{
int row = ui->tableView->currentIndex().row();
if ( row >=0 && row <m_GoodsArray.size())
{
GoodDilog dlg;
dlg.setUiParam(m_GoodsArray[row]);
if (dlg.exec()== QDialog::Accepted)
{
if (dlg.getGoodsInputParam().size() == 4)
{
m_GoodsArray[row].setGoodsName(dlg.getGoodsInput().at(0).toStdString());
m_GoodsArray[row].setGoodsType(dlg.getGoodsInput().at(1).toStdString());
m_GoodsArray[row].setGoodsUnit(dlg.getGoodsInput().at(2).toStdString());
m_GoodsArray[row].setGoodsUnitPrice(atof(dlg.getGoodsInput().at(3).toStdString().c_str()));
model->setItem(row, 0, new QStandardItem(dlg.getGoodsInput().at(0)));
model ->item(row, 0)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
model->setItem(row, 1, new QStandardItem(dlg.getGoodsInput().at(1)));
model ->item(row, 1)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
model->setItem(row, 2, new QStandardItem(dlg.getGoodsInput().at(2)));
model ->item(row, 2)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
model->setItem(row, 3, new QStandardItem(dlg.getGoodsInput().at(3)));
model ->item(row, 3)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
ModifyMoney(row,4);
}
}
}
}
/**
* @brief Widget::UpData,跟新界面控件的信息
*/
void Widget::UpData()
{
// //初始化TableView控件信息,设置属性
// model->setHorizontalHeaderLabels({"商品名称","规格型号","单位","单价","数量","金额","备注说明"});
// ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
// ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter);
// ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
// ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
// ui->tableView->setModel(model);
// //ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);//设置列可与拖动,调整列宽
// ui->tableView->show();
//插入数据
this->AddTableViewData();
this->setTableViewColumnOnlyRead();
//设置文本居中显示
this->setTableViewDataAlignment();
//设置个人信息
ui->EditPersonalCompanyName->setText(QString::fromStdString(m_Personal.getCompanyName()));
ui->EditPersonalContactTel->setText(QString::fromStdString(m_Personal.getCompanyContactTel()));
//设置列表框
QString current = ui->comboBoxCompany->currentText();
ui->comboBoxCompany->clear();
//添加客户数据
for (int i=0;i<m_CustomerArray.size();++i)
{
ui->comboBoxCompany->addItem(QString::fromStdString(m_CustomerArray[i].getCompanyName()));
if (QString::fromStdString(m_CustomerArray[i].getCompanyName()) == current)
{
ui->comboBoxCompany->setCurrentIndex(i);
ui->EditCustomerContact->setText(QString::fromStdString(m_CustomerArray[i].getCompanyContact()));
ui->EditCustomerContactTel->setText(QString::fromStdString(m_CustomerArray[i].getCompanyContactTel()));
}
}
}
/**
* @brief Widget::onBtnSave,保存界面左边相关控件信息
*/
void Widget::onBtnSave()
{
bool isSucc = false;
if (!isExistence(ui->comboBoxCompany->currentText(),QString("c")))
{
Customer customer;
customer.setCompanyName(ui->comboBoxCompany->currentText().toStdString());
customer.setCompanyContact(ui->EditCustomerContact->text().toStdString());
customer.setCompanyContactTel(ui->EditCustomerContactTel->text().toStdString());
m_CustomerArray.push_back(customer);
isSucc = true;
}
else
{
int pos = ui->comboBoxCompany->currentIndex();
if (pos >=0 && pos < m_CustomerArray.size())
{
m_CustomerArray[pos].setCompanyContact(ui->EditCustomerContact->text().toStdString());
m_CustomerArray[pos].setCompanyContactTel(ui->EditCustomerContactTel->text().toStdString());
isSucc = true;
}
}
if (!isExistence(ui->EditPersonalCompanyName->text(),QString("p")))
{
m_Personal.setCompanyName(ui->EditPersonalCompanyName->text().toStdString());
m_Personal.setCompanyContactTel(ui->EditPersonalContactTel->text().toStdString());
isSucc = true;
}
UpData();
if(isSucc)
QMessageBox::information(this,tr("提示:"),QStringLiteral("保存成功!"),QMessageBox::Ok);
}
void Widget::setTableStyle()
{
//初始化TableView控件信息,设置属性
model->setHorizontalHeaderLabels({"商品名称","规格型号","单位","单价","数量","金额","备注说明"});
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter);
ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->setModel(model);
//ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);//设置列可与拖动,调整列宽
ui->tableView->show();
}
/**
* @brief Widget::LowerToUpper,小写金额转大写
* @param money
* @return
*/
QString Widget::LowerToUpper(double money)
{
return QString("");
}
void Widget::ModifyMoney(int row, int column)
{
double money = atof(((model->item(row, column)->text()).toStdString()).c_str()) * m_GoodsArray[row].getGoodsUnitPrice();
model->setItem(row, column, new QStandardItem(model->item(row, column)->text()));
model ->item(row, column)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
model->setItem(row,column + 1, new QStandardItem(QString::fromStdString(std::to_string(money))));
model ->item(row, column + 1)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
ModifySumMoney();
}
void Widget::ModifySumMoney()
{
double sum = 0.0;
for (int i=0;i<m_GoodsArray.size();i++)
{
sum += atof((model->item(i, 5)->text()).toStdString().c_str());
}
model->setItem(m_GoodsArray.size(), 5, new QStandardItem(QString("¥") + QString::fromStdString(std::to_string(sum))));
model ->item(m_GoodsArray.size(), 5)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
model->setItem(m_GoodsArray.size(), 0, new QStandardItem(QString("合计金额(大写):") + LowerToUpper(sum)));
model ->item(m_GoodsArray.size(), 0)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
}
void Widget::on_BtnSave_clicked()
{
onBtnSave();
}
void Widget::CalTableData()
{
int column = ui->tableView->currentIndex().column();
if ( column == 4)
{
int row = ui->tableView->currentIndex().row();
//计算金额
if (row >=0 && row <m_GoodsArray.size())
{
ModifyMoney(row,column);
}
}
}
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
2.运行结果
功能还需完善,还不稳定。