QT清单打印程序

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_OBJECTpublic: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_OBJECTpublic: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.运行结果
功能还需完善,还不稳定。
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/484104.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

图灵奖得主杨立昆:人工智能比你更聪明吗?

来源&#xff1a;混沌巡洋舰人工智能常常被认为是一项将要颠覆世界的技术&#xff0c;从这一概念诞生至今的65年中&#xff0c;无数电影与小说塑造了各种经典的人工智能角色&#xff0c;AI &#xff08;Artificial Intelligence&#xff09;也很快成为人类未来世界蓝图中的重要…

(转)Windows系统、Linux系统 和 Mac OS操作系统 历史由来 与 区别?

目录 1 UNIX 由来 2 Linux 由来 3 Windows与Linux的主要区别 4 关于Linux的一些疑惑 参考资料 目前常见的三大操作系统&#xff1a;Windows系统、Linux系统 和 Mac OS操作系统。 首先&#xff0c;不管是Windows操作系统、Linux系统还是苹果的Mac OS操作系统&#xff0c;甚…

国务院公布《关键信息基础设施安全保护条例》

来源&#xff1a;中国政府网编辑&#xff1a;蒲蒲据中国政府网8月17日消息&#xff0c;《关键信息基础设施安全保护条例》已经2021年4月27日国务院第133次常务会议通过&#xff0c;现予公布&#xff0c;自2021年9月1日起施行。条例指出&#xff0c;国家对关键信息基础设施实行重…

文章推荐 | 城市规划中城市信息学的研究进展

来源&#xff1a;北京城市实验室BCL随着计算机技术的飞速发展&#xff0c;城市信息学作为城市规划领域的一门新兴学科&#xff0c;逐渐引起学术界的关注。城市信息学的兴起给城市规划带来了新的压力&#xff0c;但它也提供了新的城市分析视角。在此背景下&#xff0c;专家小组概…

Matlab基础

一.入门基础 1.基本知识 1.1 输入命令 以分号结尾不会打印变量的值 x 1 y x;1.2 变量命名规则&#xff1a;以字母开头&#xff0c;并且仅包含字母、数字和下划线。 1.3 使用save命令将工作区中的变量保存到MAT文件的MATLAB特定格式文件中,文件名为datafile.mat。可以指定保…

3年规模翻7倍统治 5G、IoT时代,化合物半导体材料深度报告

来源 华西证券编辑&#xff1a;智东西内参作者&#xff1a;吴吉森 等随着 5G、IoT 物联网时代的来临&#xff0c;以砷化镓&#xff08;GaAs&#xff09;、氮化镓&#xff08;GaN&#xff09;、碳化硅&#xff08;SiC&#xff09;为代表的化合物半导体市场有望快速崛起。其中&am…

SpringBoot笔记整理(二)

SpringBoot笔记整理&#xff08;一&#xff09; SpringBoot笔记整理&#xff08;二&#xff09; SpringBoot笔记整理&#xff08;三&#xff09; SpringBoot笔记整理&#xff08;四&#xff09; Spring Boot与日志&#xff08;日志框架、日志配置&#xff09; 1、市面上的日志…

MATLAB图像处理基础

1.导入数据 1.1 使用readtable("")导入数据&#xff0c;并存储在表格中&#xff0c;使用axis equal可以校正坐标轴纵横比。 letter readtable("M.txt"); plot(letter.X,letter.Y) axis equal1.2 range(x)函数返回x的值的范围&#xff0c;即max(x) - min(…

BBWebImage 设计思路

BBWebImage 设计思路 BBWebImage 是高性能 Swift 图片组件&#xff0c;用于图片下载、缓存、编解码、编辑与展示。 GitHub 地址&#xff1a; https://github.com/Silence-GitHub/BBWebImage 效果图 下载、展示并缓存原图 下载、渐进式解码、编辑图片&#xff0c;缓存编辑后的图…

清华本科、港科大准博士被指论文抄袭,网友:这是有技巧的“洗稿”

来源&#xff1a;整理自新智元、Reddit、知乎等不是吧&#xff1f;清华自动化本科&#xff0c;香港科技大学硕士生发表的顶会论文竟然是抄的&#xff1f;而且抄袭对象还是另一篇顶会论文&#xff1f;近日&#xff0c;眼尖的网友发现两篇分别发表在 ICML 2021 和 ICCV 2021 两大…

MATLAB深度学习入门

1. 加载图像 1.1 使用imread函数加载图像&#xff0c;可以加载GIF、JPEG、PNG等大多数标准文件格式图像。 Import an image img imread("file.jpg")1.2 采用**imshow()**来显示图像。 imshow(img)1.3 采用alexnet函数可以创建预定义的深度网络AlexNet的副本。 de…

SpringBoot笔记整理(三)

SpringBoot笔记整理&#xff08;一&#xff09; SpringBoot笔记整理&#xff08;二&#xff09; SpringBoot笔记整理&#xff08;三&#xff09; SpringBoot笔记整理&#xff08;四&#xff09; Web开发 1、使用SpringBoot&#xff1a; 1&#xff09;创建SpringBoot应用&…

图神经网络,如何变深?

来源&#xff1a;智源社区版式制作&#xff1a;Camel撰文&#xff1a;侯振宇编辑&#xff1a;贾 伟现实世界中很多很多任务可以描述为图(Graph)问题&#xff0c;比如社交网络&#xff0c;蛋白质结构&#xff0c;交通路网数据&#xff0c;图是一个很值得研究的领域。近年来&am…

机器学习分类与性能度量

机器学习分类与性能度量 数据集:训练集(Training Set)、验证集(Validation Set)和测试集(Test Set) 1、 当模型无需人为设定超参数&#xff0c;所用参数都通过学习得到&#xff0c;则不需要验证集。验证集适合多个不同超参数训练多个模型&#xff0c;通过验证集&#xff0c;…

中国人民大学孟小峰教授:“科学数据智能——人工智能在科学发现中的机遇与挑战”...

来源&#xff1a;国家自然科学基金委员会微信公号 孟小峰 博士&#xff0c;中国人民大学教授&#xff0c;博士生导师&#xff0c;CCF会士。主要研究方向为数据库理论与系统、大数据管理系统、大数据隐私保护、大数据融合与智能、大数据实时分析、社会计算等。摘 要随着全球各科…

Java 面向对象语言基础

面向过程 就是分析出解决问题所需要的步骤&#xff0c;然后用函数把这些步骤一步一步实现&#xff0c;使用的时候一个一个依次调用就可以了。 面向对象 是把构成问题事务分解成各个对象&#xff0c;建立对象的目的不是为了完成一个步骤&#xff0c;而是为了描叙某个事物在整个解…

全球首例!这台机器人发明了两项新技术,已获专利授权!

文章来源&#xff1a;EETOP几天前前人工智能系统专利申请权有最新发展。南非率先成为第一个授予人工智能专利权的国家&#xff0c;承认人工智能机器人DABUS为「发明者」&#xff0c;6日澳大利亚联邦法院也做出裁决&#xff1a;发明者可以是非人类。 这算是有历史里程碑意义的判…

成员变量、局部变量、实例变量、静态变量、类变量、常量

成员变量&#xff1a; 直接在类中声明的变量叫成员变量(又称全局变量) ▲ 初始化&#xff1a; 如果未对成员变量设置初始值&#xff0c;则系统会根据成员变量的类型自动分配初始值&#xff1a;int分配初始值0、boolean分配初始值false&#xff0c;而自定义类型则分配初始值null…

行业洞见 | 一文了解自动驾驶汽车

文章来源&#xff1a;ancholabs.medium.com文字翻译&#xff1a; 新能源情报局封面配图&#xff1a;原作者驾驶是人类日常从事的活动中最困难的之一。遵循交通规则&#xff0c;通过眼神与其他司机沟通交流、对天气状况做出反应是成为优秀司机的先决条件。在自动驾驶汽车中&…

MATLAB高光谱图像处理基础

介绍高光谱图像的基本知识&#xff0c;便通过MATLAB对高光谱图像进行基本的处理。 文章目录前言一、高光谱图像二、MATLAB高光谱图像处理1.加载.MAT文件数据2.图像的显示3.图像维度变换总结前言 高光谱图像是一个立方体结构&#xff0c;维度为M x N x B&#xff0c;M为水平方向…