QT学习篇:入门(1)
第一个为管理界面:
(1)安全库存的设置,包括序号、物品代码、物品类型、最大库存量、最小库存量、最大库存比率、最小库存比率;
(2)计算频率设置,包括:实时,定时(单位分为:分钟、小时、天),来自globalPara表,该表增加字段;
(3)库存订单确认方式,包括:自动,人工,来自globalPara表,该表增加字段;
(4)通知设置,包括:手机号码,邮箱,来自globalPara表,该表增加字段。手机和邮箱可以设置多个。
#ifndef _REPERTORY_H
#define _REPERTORY_H
#include "ui_CStockManagerSet.h"
#include "QtGui/QDialog"
#include "QtGui/QStandardItemModel"
#include <QComboBox>
#include <QTableView>
#include <QGroupBox>
#include <QPushButton>
#include <QRadioButton>
#include <QLineEdit>
#include <QLabel>
typedef struct CStockMode
{
int index; //ID
QString strRes; //代码
int iResType; //类型
double iMaxStock; //最大库存量(新增安全库存)
double iMinStock; //最小库存量(新增安全库存)
double iMaxStockPercent; //最大库存比率,单位:% (新增安全库存)
double iMinStockPercent; //最小库存比率
}CStockMode_t;
typedef struct ItemMode
{
int iCalculatedRateType; //计算频率类型,1-实时,2-定时
int iCalculatedRateUnit; //计算频率定时单位,1-分钟,2-小时,3-天
int iCalculatedRateTime; //计算频率定时时间
bool bStockOrderConfirmation; //库存订单确认方式, true-自动, false-人工
QList<QString> strMessagePhone; //手机通知方式
QList<QString> strMessageEmail; //邮箱通知方式
}ItemMode_t;
class CStockManagerSet : public QDialog
{
Q_OBJECT
public:
CStockManagerSet(QWidget *parent = 0, Qt::WFlags flags = 0);
~CStockManagerSet();
private:
void initLayout();
void setData(const QMap<int, CStockMode_t>& mapMode,const ItemMode_t& item);
/*void setData(const QMap<int, CStockMode_t>& mapMode);*/
void setDelegate();
void setItemData();
public:
QPair<QMap<int, CStockMode_t>, ItemMode_t> getdata();
private:
Ui::Repertory_Dialog ui;
QStandardItemModel* Repertory_Model;
QMap<int, CStockMode_t> m_mapMode;
QTableView* m_pStockTableView;
ItemMode_t m_itemMode;
bool m_bFlag; //判断是否单元格数据被修改,第一次加载的数据不算修改
private slots:
void OnRealTimeClicked();
void OnSetTimeClicked();
void OnItemChanged(QStandardItem* item);
void OnTimeReturned();
void OnAutomaticClicked();
void OnArtificialClicked();
void OnPhoneNumberReturned();
void OnEmailReturned();
void OnOk();
void OnCancel();
void OnComboBoxReturned(const QString& strText);
};
#endif
源文件
#include "CStockManagerSet.h"
#include "QtGui/QGridLayout"
#include <QModelIndex>
const int RepertoryColumnCount = 7;
CStockManagerSet::CStockManagerSet(QWidget *parent, Qt::WFlags flags)
: QDialog(parent, flags),
m_bFlag(false)
{
ui.setupUi(this);
m_pStockTableView = ui.StockTableView;
setWindowTitle(tr("安全库存管理界面"));
setFixedSize(700,600);
initLayout();
setData(m_mapMode, m_itemMode);
}
CStockManagerSet::~CStockManagerSet()
{
}
void CStockManagerSet::initLayout()
{
ui.SetSafestock->setTitle(tr("安全库存"));
ui.CalFrequency->setTitle(tr("计算步骤"));
ui.StockConfirm->setTitle(tr("库存订单确认"));
ui.SetNotification->setTitle(tr("通知设置"));
ui.RealTime->setText(tr("实时"));
ui.SetTime->setText(tr("定时"));
ui.Automatic->setText(tr("自动"));
ui.Artificial->setText(tr("人工手动确认"));
ui.ComboBox->insertItem(1,tr("天数"));
ui.ComboBox->insertItem(2,tr("小时"));
ui.ComboBox->insertItem(3,tr("分钟"));
ui.PhoneNumberButton->setText(tr("手机号"));
ui.EmailButton->setText(tr("邮箱"));
ui.OkButton->setText(tr("确定"));
ui.CancelButton->setText(tr("取消"));
ui.FrequencyDisplay->setText(tr("定时数量"));
ui.SetType->setText(tr("定时类型"));
ui.PhoneNumberEdit->setPlaceholderText(tr("多个输入时以;分隔"));
ui.EmailEdit->setPlaceholderText(tr("多个输入时以;分隔"));
connect(ui.RealTime, SIGNAL(clicked()), this, SLOT(OnRealTimeClicked()));
connect(ui.SetTime, SIGNAL(clicked()), this, SLOT(OnSetTimeClicked()));
connect(ui.TimeEdit, SIGNAL(editingFinished()), this, SLOT(OnTimeReturned()));
connect(ui.Automatic, SIGNAL(clicked()), this, SLOT(OnAutomaticClicked()));
connect(ui.Artificial, SIGNAL(clicked()), this, SLOT(OnArtificialClicked()));
connect(ui.PhoneNumberEdit, SIGNAL(editingFinished()), this, SLOT(OnPhoneNumberReturned()));
connect(ui.EmailEdit, SIGNAL(editingFinished()), this, SLOT(OnEmailReturned()));
connect(ui.ComboBox, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(OnComboBoxReturned(const QString&)));
connect(ui.OkButton, SIGNAL(clicked()), this, SLOT(OnOk()));
connect(ui.CancelButton, SIGNAL(clicked()), this, SLOT(OnCancel()));
QString strProperty[RepertoryColumnCount] =
{
tr("序号"), tr("物品代码"),tr("物品类型"), tr("最大库存量"), tr("最小库存量"),
tr("最大库存比率"), tr("最小库存比率")
};
Repertory_Model = new QStandardItemModel(this);
Repertory_Model->setColumnCount(RepertoryColumnCount);
for (int i = 0; i < RepertoryColumnCount; i++)
{
Repertory_Model->setHeaderData(i, Qt::Horizontal, strProperty[i]);
}
connect(Repertory_Model, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(OnItemChanged(QStandardItem*)));
m_pStockTableView->setModel(Repertory_Model);
m_pStockTableView->setSortingEnabled(true);
m_pStockTableView->resizeColumnsToContents();
m_pStockTableView->verticalHeader()->setResizeMode(QHeaderView::Fixed);
m_pStockTableView->setAlternatingRowColors(true);
m_pStockTableView->verticalHeader()->setDefaultSectionSize(25);
}
//void CStockManagerSet::setData(const QMap<int, CStockMode_t>& mapMode)
//{
// m_mapMode = mapMode;
// CStockMode_t item;
// item.index = 1;
// item.strRes = "APS01";
// item.iResType = 0;
// item.iMaxStock = 1000;
// item.iMinStock = 100;
// item.iMaxStockPercent = 87.8;
// item.iMinStockPercent = 54.3;
// m_mapMode.insert(1, item);
//
// CStockMode_t item1;
// item1.index = 2;
// item1.strRes = "APS03";
// item1.iResType = 1;
// item1.iMaxStock = 999;
// item1.iMinStock = 90;
// item1.iMaxStockPercent = 88.8;
// item1.iMinStockPercent = 33.3;
// m_mapMode.insert(2, item1);
//
// CStockMode_t item2;
// item1.index = 3;
// item1.strRes = "APS03";
// item1.iResType = 0;
// item1.iMaxStock = 198;
// item1.iMinStock = 67;
// item1.iMaxStockPercent = 79.9;
// item1.iMinStockPercent = 55.8;
//
// m_mapMode.insert(3, item1);
//
// setDelegate();
// setItemData();
//
//}
void CStockManagerSet::setData(const QMap<int, CStockMode_t>& mapMode,const ItemMode_t& item)
{
m_mapMode = mapMode;
m_itemMode = item;
setDelegate();
setItemData();
}
QPair<QMap<int, CStockMode_t>, ItemMode_t> CStockManagerSet::getdata()
{
return qMakePair(m_mapMode, m_itemMode);
}
void CStockManagerSet::setDelegate()
{
}
void CStockManagerSet::setItemData()
{
m_bFlag = false;
//清空表
Repertory_Model->removeRows(0, Repertory_Model->rowCount());
//重新插入
int iRow = 0;
QMap<int, CStockMode_t>::const_iterator it;
for (it = m_mapMode.begin(); it != m_mapMode.end(); ++it)
{
Repertory_Model->insertRows(iRow, 1, QModelIndex());
Repertory_Model->setData(Repertory_Model->index(iRow, 0), it->index);
Repertory_Model->setData(Repertory_Model->index(iRow, 1), it->strRes);
Repertory_Model->setData(Repertory_Model->index(iRow, 2), it->iResType);
Repertory_Model->setData(Repertory_Model->index(iRow, 3), it->iMaxStock);
Repertory_Model->setData(Repertory_Model->index(iRow, 4), it->iMinStock);
Repertory_Model->setData(Repertory_Model->index(iRow, 5), it->iMaxStockPercent);
Repertory_Model->setData(Repertory_Model->index(iRow, 6), it->iMinStockPercent);
iRow++;
}
m_bFlag = true;
}
void CStockManagerSet::OnRealTimeClicked()
{
if (ui.RealTime->isChecked())
{
m_itemMode.iCalculatedRateType = 1;
}
ui.TimeEdit->setEnabled(false);
ui.ComboBox->setEnabled(false);
}
void CStockManagerSet::OnSetTimeClicked()
{
if (ui.SetTime->isChecked())
{
m_itemMode.iCalculatedRateType = 2;
}
ui.TimeEdit->setEnabled(true);
ui.ComboBox->setEnabled(true);
}
void CStockManagerSet::OnTimeReturned()
{
int iTime = ui.TimeEdit->text().toInt();
m_itemMode.iCalculatedRateTime = iTime;
}
void CStockManagerSet::OnPhoneNumberReturned()
{ //电话添加
QString iPhoneNumber = ui.PhoneNumberEdit->text();
QStringList lstPhoneNumber = iPhoneNumber.split(tr(";"));
QList<QString>::const_iterator it;
for (it = lstPhoneNumber.begin(); it != lstPhoneNumber.end(); ++it)
{
m_itemMode.strMessagePhone.append(*it);
}
}
void CStockManagerSet::OnEmailReturned()
{ //邮箱添加
QString iEmail = ui.EmailEdit->text();
QStringList lstEmail = iEmail.split(tr(";"));
QList<QString>::const_iterator it;
for (it = lstEmail.begin(); it != lstEmail.end(); ++it)
{
m_itemMode.strMessageEmail.append(*it);
}
}
void CStockManagerSet::OnAutomaticClicked()
{
if (ui.Automatic->isChecked())
{
m_itemMode.bStockOrderConfirmation = true;
}
ui.Artificial->setEnabled(false);
}
void CStockManagerSet::OnArtificialClicked()
{
if (ui.Artificial->isChecked())
{
m_itemMode.bStockOrderConfirmation = false;
}
ui.Automatic->setEnabled(false);
}
void CStockManagerSet::OnComboBoxReturned(const QString& strText)
{
//ui.ComboBox->text();
if (strText == tr("分钟"))
{
m_itemMode.iCalculatedRateUnit = 1;
}
else if (strText == tr("小时"))
{
m_itemMode.iCalculatedRateUnit = 2;
}
else if (strText == tr("天数"))
{
m_itemMode.iCalculatedRateUnit = 3;
}
}
void CStockManagerSet::OnItemChanged(QStandardItem *item)
{
if(item == NULL || !m_bFlag)
{
return;
}
int iRow = item->index().row();
int iColumn = item->index().column();
int iID = Repertory_Model->data(Repertory_Model->index(iRow, 0)).toInt();
switch(iColumn)
{
case 0:
{
m_mapMode[iID].index = Repertory_Model->data(Repertory_Model->index(iRow, 0)).toInt();
break;
}
case 1:
{
m_mapMode[iID].strRes = Repertory_Model->data(Repertory_Model->index(iRow, 1)).toString();
break;
}
case 2:
{
m_mapMode[iID].iResType = Repertory_Model->data(Repertory_Model->index(iRow, 2)).toInt();
break;
}
case 3:
{
m_mapMode[iID].iMaxStock = Repertory_Model->data(Repertory_Model->index(iRow, 3)).toInt();
break;
}
case 4:
{
m_mapMode[iID].iMinStock = Repertory_Model->data(Repertory_Model->index(iRow, 4)).toDouble();
break;
}
case 5:
{
m_mapMode[iID].iMaxStockPercent = Repertory_Model->data(Repertory_Model->index(iRow, 5)).toDouble();
break;
}
case 6:
{
m_mapMode[iID].iMinStockPercent = Repertory_Model->data(Repertory_Model->index(iRow, 6)).toDouble();
break;
}
default:
break;
}
}
void CStockManagerSet::OnOk()
{
this->accept();
}
void CStockManagerSet::OnCancel()
{
this->reject();
}
效果图