QT学习篇: 入门(二)
库存订单界面:
(1)包括序号、订单代码、订单名称、产品代码、产品类型、计划加工数量、最晚完工日期。
(2)model增加临时map,用于存储库存订单。
(3)后台计算完成后,会将库存订单推送给前台,弹出该界面,若是人工确认的话,则用户可以在该界面进行编辑,确认后进行插单排程;若是自动的话,则自动进行插单排程。
(4)插单排程成功后,“制造订单表”中以“客户关系”来区别哪些订单是库存订单,“客户关系”是工厂本身自己的都是库存订单。
头文件:
#ifndef _CSTOCKORDERSET_H
#define _CSTOCKORDERSET_H
#include "ui_CStockOrderSet.h"
#include "QtGui/QDialog"
#include "QtGui/QStandardItemModel"
#include <QTableView>
//#include <QGroupBox>
#include <QDateTime>
#include <QAction>
typedef struct StockOrder
{
int index; //序号
QString strOrderCode; //订单代码
QString strOrderName; //订单名称
QString strGoodsCode; //产品代码
QString strGoodsType; //产品类型
double dbPlanNum; //计划加工数量
QDateTime tLaterestTime; //最晚完工日期, 格式:yyyy-MM-dd HH:mm:ss
int iProjectID; //产品序列号
}StockOrder_t;
class CTableStockOrder : public QDialog
{
Q_OBJECT
public:
CTableStockOrder(QWidget *parent = 0, Qt::WFlags flags = 0);
~CTableStockOrder();
private:
void initLayout();
void setDelegate();
void setItemData();
void insertBlankEntry();
public:
void setData(const QMap<int, StockOrder_t>& orderMode);
QMap<int, StockOrder_t> getData();
private:
Ui::StockOrder_Dialog ui;
QStandardItemModel* StockOrder_Model;
QMap<int, StockOrder_t> m_orderMode;
int m_iInsertIndex;
int m_iBlankID;
int m_iMaxKey;
QTableView* m_pOrderTableView;
bool m_pFlag; //判断单元格数据是否被修改,第一次加载的数据不算修改
bool m_isAutoMation; //判断是人工确认还是自动,人工确认-false 自动-true
QAction* m_pActionCopy;
QAction* m_pActionInsert;
QAction* m_pActionRemove;
QMap<int, StockOrder_t> m_mapClipboard;
private slots:
void OnItemChanged(QStandardItem* item);
void OnCustomContextMenuRequested(const QPoint &pos);
void OnInsertEntry();
void OnCopyEntry();
void OnRemoveEntry();
void OnOk();
void OnCancel();
};
#endif
源cpp文件
#include "CTableStockOrder.h"
#include "QtGui/QGridLayout"
#include <QModelIndex>
#include <QMenu>
#include <QItemSelectionModel>
#include <QPushButton>
const int OrderColumnCount = 8;
CTableStockOrder::CTableStockOrder(QWidget *parent, Qt::WFlags flags)
:QDialog(parent, flags),
m_pFlag(false),
m_isAutoMation(false),
m_iBlankID(0),
m_iInsertIndex(0),
m_iMaxKey(1)
{
ui.setupUi(this);
m_pOrderTableView = ui.ordertableView;
setWindowTitle(tr("库存订单界面"));
setFixedSize(800, 650);
initLayout();
setData(m_orderMode);
}
CTableStockOrder::~CTableStockOrder()
{
}
void CTableStockOrder::initLayout()
{
ui.okButton->setText(tr("确定"));
ui.cancelButton->setText(tr("取消"));
connect(ui.okButton, SIGNAL(clicked()), this, SLOT(OnOk()));
connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(OnCancel()));
QString strProperty[OrderColumnCount] =
{
tr("序号"),tr("订单代码"),tr("订单名称"), tr("产品代码"), tr("产品类型"),
tr("计划加工数量"), tr("最晚完工日期"), tr("产品序号")
};
StockOrder_Model = new QStandardItemModel(this);
StockOrder_Model->setColumnCount(OrderColumnCount);
for (int i = 0; i < OrderColumnCount; i++)
{
StockOrder_Model->setHeaderData(i, Qt::Horizontal, strProperty[i]);
}
connect(StockOrder_Model, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(OnItemChanged(QStandardItem* item)));
m_pOrderTableView->setModel(StockOrder_Model);
m_pOrderTableView->setSortingEnabled(true);
m_pOrderTableView->resizeColumnsToContents();
m_pOrderTableView->verticalHeader()->setResizeMode(QHeaderView::Fixed);
m_pOrderTableView->setAlternatingRowColors(true);
m_pOrderTableView->verticalHeader()->setDefaultSectionSize(25);
if (m_isAutoMation)
{
m_pOrderTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
}
else
{
QHeaderView* pVHeaderView = m_pOrderTableView->verticalHeader();
pVHeaderView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(pVHeaderView, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(OnCustomContextMenuRequested(const QPoint&)));
m_pActionCopy = new QAction(tr("复制"), this);
m_pActionCopy->setEnabled(false);
connect(m_pActionCopy, SIGNAL(triggered()), this, SLOT(OnCopyEntry()));
m_pActionInsert = new QAction(tr("插入"), this);
m_pActionInsert->setEnabled(false);
connect(m_pActionInsert, SIGNAL(triggered()), this, SLOT(OnInsertEntry()));
m_pActionRemove = new QAction(tr("删除"), this);
m_pActionRemove->setEnabled(false);
connect(m_pActionRemove, SIGNAL(triggered()), this, SLOT(OnRemoveEntry()));
}
}
void CTableStockOrder::setData(const QMap<int, StockOrder_t>& orderMode)
{
m_orderMode = orderMode;
QDateTime time = QDateTime::currentDateTime();//获取系统现在的时间
QString tLaterestTime = time.toString("yyyy-MM-dd hh:mm:ss ddd"); //设置显示格式
StockOrder_t item;
item.index = 1;
item.strOrderCode = tr("O01");
item.strOrderName = tr("P01");
item.strGoodsCode = "APS01";
item.strGoodsType = "TY01";
item.dbPlanNum = 1000;
item.tLaterestTime = QDateTime::fromString(tr("2015-06-23 10:19:05"), tr("yyyy-MM-dd HH:mm:ss"));
item.iProjectID = 10;
m_orderMode.insert(1, item);
//对表格数据设置代理
setDelegate();
setItemData();
//插入空白行
insertBlankEntry();
}
void CTableStockOrder::setDelegate()
{
}
void CTableStockOrder::setItemData()
{
m_pFlag = false;
//清空表
StockOrder_Model->removeRows(0, StockOrder_Model->rowCount());
//重新插入
int iRow = 0;
QMap<int, StockOrder_t>::const_iterator it;
for (it = m_orderMode.begin(); it != m_orderMode.end(); ++it)
{
StockOrder_Model->insertRows(iRow, 1, QModelIndex());
StockOrder_Model->setData(StockOrder_Model->index(iRow, 0), it->index);
StockOrder_Model->setData(StockOrder_Model->index(iRow, 1), it->strOrderCode);
StockOrder_Model->setData(StockOrder_Model->index(iRow, 2), it->strOrderName);
StockOrder_Model->setData(StockOrder_Model->index(iRow, 3), it->strGoodsCode);
StockOrder_Model->setData(StockOrder_Model->index(iRow, 4), it->strGoodsType);
StockOrder_Model->setData(StockOrder_Model->index(iRow, 5), it->dbPlanNum);
StockOrder_Model->setData(StockOrder_Model->index(iRow, 6), it->tLaterestTime);
StockOrder_Model->setData(StockOrder_Model->index(iRow, 7), it->iProjectID);
iRow++;
}
m_pFlag = true;
}
void CTableStockOrder::insertBlankEntry()
{
m_pFlag = false;
int iRow = StockOrder_Model->rowCount();
StockOrder_Model->insertRows(iRow, 1, QModelIndex());
StockOrder_Model->setData(StockOrder_Model->index(iRow, 0), QString(""));
StockOrder_Model->setData(StockOrder_Model->index(iRow, 1), QString(""));
StockOrder_Model->setData(StockOrder_Model->index(iRow, 2), QString(""));
StockOrder_Model->setData(StockOrder_Model->index(iRow, 3), QString(""));
StockOrder_Model->setData(StockOrder_Model->index(iRow, 4), QString(""));
StockOrder_Model->setData(StockOrder_Model->index(iRow, 5), QString(""));
StockOrder_Model->setData(StockOrder_Model->index(iRow, 6), QVariant("").toDateTime());
StockOrder_Model->setData(StockOrder_Model->index(iRow, 7), QString(""));
m_pFlag = true;
}
void CTableStockOrder::OnItemChanged(QStandardItem* item)
{
if (item == NULL || !m_pFlag)
{
return;
}
int iRow = item->index().row();
int iColumn = item->index().column();
int iID = StockOrder_Model->data(StockOrder_Model->index(iRow, 0)).toInt();
if (iID == m_iBlankID)
{
QString strCode = StockOrder_Model->data(StockOrder_Model->index(iRow, 1)).toString();
if (!strCode.isEmpty())
{
StockOrder_t opt;
opt.index = m_iMaxKey;
opt.strOrderCode = StockOrder_Model->data(StockOrder_Model->index(iRow, 1)).toString();
opt.strOrderName = StockOrder_Model->data(StockOrder_Model->index(iRow, 2)).toString();
opt.strGoodsCode = StockOrder_Model->data(StockOrder_Model->index(iRow, 3)).toString();
opt.strGoodsType = StockOrder_Model->data(StockOrder_Model->index(iRow, 4)).toString();
opt.dbPlanNum = StockOrder_Model->data(StockOrder_Model->index(iRow, 5)).toDouble();
opt.tLaterestTime = StockOrder_Model->data(StockOrder_Model->index(iRow, 6)).toDateTime();
opt.iProjectID = StockOrder_Model->data(StockOrder_Model->index(iRow, 7)).toInt();
m_iMaxKey++;
insertBlankEntry();
}
}
switch(iColumn)
{
case 0:
{
m_orderMode[iID].index = StockOrder_Model->data(StockOrder_Model->index(iRow, 0)).toInt();
break;
}
case 1:
{
m_orderMode[iID].strOrderCode = StockOrder_Model->data(StockOrder_Model->index(iRow, 1)).toString();
break;
}
case 2:
{
m_orderMode[iID].strOrderName = StockOrder_Model->data(StockOrder_Model->index(iRow, 2)).toString();
break;
}
case 3:
{
m_orderMode[iID].strGoodsCode = StockOrder_Model->data(StockOrder_Model->index(iRow, 3)).toString();
break;
}
case 4:
{
m_orderMode[iID].strGoodsType = StockOrder_Model->data(StockOrder_Model->index(iRow, 4)).toString();
break;
}
case 5:
{
m_orderMode[iID].dbPlanNum = StockOrder_Model->data(StockOrder_Model->index(iRow, 5)).toDouble();
break;
}
case 6:
{
m_orderMode[iID].tLaterestTime = StockOrder_Model->data(StockOrder_Model->index(iRow, 6)).toDateTime();
break;
}
case 7:
{
m_orderMode[iID].iProjectID = StockOrder_Model->data(StockOrder_Model->index(iRow, 7)).toInt();
break;
}
default:
break;
}
}
QMap<int, StockOrder_t> CTableStockOrder::getData()
{
return m_orderMode;
}
void CTableStockOrder::OnCopyEntry()
{
QItemSelectionModel *selections = m_pOrderTableView->selectionModel();
QModelIndexList selectedIndex = selections->selectedRows();
m_mapClipboard.clear();
m_iInsertIndex = 0;
foreach (QModelIndex index, selectedIndex)
{
int iID = StockOrder_Model->data(StockOrder_Model->index(index.row(), 0)).toInt();
m_mapClipboard[iID] = m_orderMode[iID];
}
}
void CTableStockOrder::OnInsertEntry()
{
QModelIndex currentIndex = m_pOrderTableView->currentIndex();
m_pFlag = false;
int iRow = currentIndex.row()+1;
QMap<int, StockOrder_t>::iterator it;
for (it = m_mapClipboard.begin(); it != m_mapClipboard.end(); it++)
{
StockOrder_Model->insertRows(iRow, 1, QModelIndex());
StockOrder_t opt = it.value();
opt.index = ++m_iMaxKey;
// sales.SetResID(QString("%1_%2").arg(it->GetMachineCode()).arg(m_iInsertIndex));
StockOrder_Model->setData(StockOrder_Model->index(iRow, 0), opt.index);
StockOrder_Model->setData(StockOrder_Model->index(iRow, 1), opt.strOrderCode);
StockOrder_Model->setData(StockOrder_Model->index(iRow, 2), opt.strOrderName);
StockOrder_Model->setData(StockOrder_Model->index(iRow, 3), opt.strGoodsCode);
StockOrder_Model->setData(StockOrder_Model->index(iRow, 4), opt.strGoodsType);
StockOrder_Model->setData(StockOrder_Model->index(iRow, 5), opt.dbPlanNum);
StockOrder_Model->setData(StockOrder_Model->index(iRow, 6), opt.tLaterestTime);
StockOrder_Model->setData(StockOrder_Model->index(iRow, 7), opt.iProjectID);
//StockOrder_Model->item(iRow, 0)->setEditable(false); // ID不可修改
//StockOrder_Model->item(iRow, 5)->setEditable(false); // 方案ID不可修改
m_orderMode[opt.index] = opt;
iRow++;
}
m_iInsertIndex++;
m_pFlag = true;
}
void CTableStockOrder::OnRemoveEntry()
{
QItemSelectionModel *selections = m_pOrderTableView->selectionModel();
QModelIndexList selectedIndex = selections->selectedRows();
QMap<int, int> mapRows; // 自动排序,方便表多行删除
foreach (QModelIndex index, selectedIndex)
{
int iID = StockOrder_Model->data(StockOrder_Model->index(index.row(), 0)).toInt();
m_orderMode.remove(iID);
mapRows[index.row()] = index.row();
}
QList<int> lstRows;
QMap<int, int>::iterator it = mapRows.begin();
for (; it != mapRows.end(); it++)
{
lstRows.prepend(it.value());
}
for (int i = 0; i < lstRows.size(); i++)
{
StockOrder_Model->removeRows(lstRows.at(i), 1);
}
}
void CTableStockOrder::OnCustomContextMenuRequested(const QPoint &pos)
{
int iCurrentRow = m_pOrderTableView->verticalHeader()->logicalIndexAt(pos);
if(iCurrentRow == -1)
{
return;
}
//空白行不弹出右键菜单
int iID = StockOrder_Model->data(StockOrder_Model->index(iCurrentRow, 0)).toUInt();
if (iID == m_iBlankID)
{
return;
}
//获取选中行
QItemSelectionModel *selections = m_pOrderTableView->selectionModel();
QModelIndexList selectedIndex = selections->selectedRows();
if (!selectedIndex.isEmpty())
{
bool bSelected = false;
foreach (QModelIndex index, selectedIndex)
{
if (index.row() == iCurrentRow)
{
bSelected = true;
break;
}
}
if (!bSelected)
{
//选中行
m_pOrderTableView->selectRow(iCurrentRow);
}
//若空白行被选中,则取消选中
/*foreach (QModelIndex index, selectedIndex)
{
iID = StockOrder_Model->data(StockOrder_Model->index(index.row()).toInt();
if (iID == m_iBlankID)
{
selections->clearSelection();
break;
}
}*/
m_pActionCopy->setEnabled(true);
m_pActionRemove->setEnabled(true);
if (!m_mapClipboard.isEmpty())
{
m_pActionInsert->setEnabled(true);
}
else
{
m_pActionInsert->setEnabled(false);
}
}
else
{
m_pActionCopy->setEnabled(false);
m_pActionInsert->setEnabled(false);
m_pActionRemove->setEnabled(false);
}
QMenu *menu = new QMenu;
menu->addAction(m_pActionCopy);
menu->addAction(m_pActionInsert);
menu->addAction(m_pActionRemove);
menu->exec(QCursor::pos());
}
void CTableStockOrder::OnOk()
{
this->accept();
}
void CTableStockOrder::OnCancel()
{
this->reject();
}
效果图