Widget.cpp.template 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "%1.h"
  2. #include "MessageException.h"
  3. #include "%2.h"
  4. #include "Application.h"
  5. #include <QMessageBox>
  6. #if _MSC_VER >= 1600
  7. #pragma execution_character_set("utf-8")
  8. #endif
  9. %1::%1(QWidget *parent)
  10. : QWidget(parent)
  11. , m_PageIndex(0)
  12. {
  13. ui = new Ui::%1();
  14. ui->setupUi(this);
  15. init();
  16. doQuery();
  17. }
  18. %1::~%1()
  19. {
  20. delete ui;
  21. }
  22. void %1::init()
  23. {
  24. initTableView();
  25. initModel();
  26. initService();
  27. initPagination();
  28. initFormBuild();
  29. }
  30. void %1::initTableView()
  31. {
  32. ui->tvData->setEditTriggers(QAbstractItemView::NoEditTriggers);
  33. ui->tvData->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
  34. ui->tvData->setSelectionBehavior(QAbstractItemView::SelectRows);
  35. ui->tvData->setAlternatingRowColors(true);
  36. ui->tvData->verticalHeader()->hide();
  37. ui->tvData->setAlternatingRowColors(true);
  38. }
  39. void %1::initModel()
  40. {
  41. m_Model = new %3(this);
  42. ui->tvData->setModel(m_Model);
  43. ui->tvData->hideColumns<%2>();
  44. }
  45. void %1::initService()
  46. {
  47. m_Service = new %4(this);
  48. }
  49. void %1::initFormBuild()
  50. {
  51. m_FormBuilder = new FormBuilder(this);
  52. }
  53. void %1::initPagination()
  54. {
  55. ui->pagination->setPageCount(0);
  56. ui->pagination->setRecordCount(0);
  57. connect(ui->pagination, &Pagination::selectPage, [=](int value) {
  58. doQuery(value);
  59. });
  60. }
  61. void %1::doQuery(int pageIndex /*= 0*/)
  62. {
  63. QString name = ui->leQName->text();
  64. try
  65. {
  66. m_Model->fetchByName(name, pageIndex);
  67. ui->pagination->setRecordCount(m_Model->getTotalCount());
  68. ui->pagination->setPageCount(m_Model->getPageCount());
  69. }
  70. catch (MessageException &e)
  71. {
  72. QMessageBox::critical(this, "错误", e.getMessage());
  73. }
  74. }
  75. void %1::onQuery()
  76. {
  77. doQuery();
  78. }
  79. void %1::onReset()
  80. {
  81. ui->leQName->clear();
  82. doQuery();
  83. }
  84. void %1::onAdd()
  85. {
  86. QWidget *widget = m_FormBuilder->build<%2>("添加%5");
  87. m_FormBuilder->clear();
  88. int result = Application::instance()->showModalDialog(widget, this);
  89. if (result == QDialog::Accepted)
  90. {
  91. %2_ptr obj = m_FormBuilder->getValue<%2>();
  92. try
  93. {
  94. m_Service->add(obj);
  95. }
  96. catch (MessageException &e)
  97. {
  98. QMessageBox::critical(this, "错误", e.getMessage());
  99. return;
  100. }
  101. doQuery();
  102. }
  103. }
  104. void %1::onEdit()
  105. {
  106. QModelIndex index = ui->tvData->currentIndex();
  107. if (!index.isValid())
  108. {
  109. QMessageBox::information(this, "提示", "请选中要修改的数据!");
  110. return;
  111. }
  112. %2_ptr oldObj = m_Model->getCollection()->_get<%2_ptr>(index.row());
  113. QWidget *widget = m_FormBuilder->build<%2>("修改%5");
  114. m_FormBuilder->clear();
  115. m_FormBuilder->fill(oldObj);
  116. int result = Application::instance()->showModalDialog(widget, this);
  117. if (result == QDialog::Accepted)
  118. {
  119. try{
  120. %2_ptr newObj = m_FormBuilder->getValue<%2>();
  121. newObj->setID(oldObj->getID());
  122. m_Service->updateById(newObj);
  123. }
  124. catch (MessageException &e){
  125. QMessageBox::critical(this, "错误", e.getMessage());
  126. return;
  127. }
  128. doQuery();
  129. }
  130. }
  131. void %1::onRemove()
  132. {
  133. QModelIndex index = ui->tvData->currentIndex();
  134. if (!index.isValid())
  135. {
  136. QMessageBox::information(this, "提示", "请选中要删除的数据!");
  137. return;
  138. }
  139. QMessageBox::StandardButton msgBox;
  140. msgBox = QMessageBox::question(this, "提示", "确定删除选中的数据吗?", QMessageBox::Yes | QMessageBox::No);
  141. if (msgBox == QMessageBox::No)
  142. return;
  143. QSqlError error = m_Model->qxDeleteRow(index.row());
  144. if (error.isValid())
  145. {
  146. QMessageBox::critical(this, "错误", error.text());
  147. return;
  148. }
  149. m_Model->removeRow(index.row());
  150. }