mainwindow.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QFile>
  4. #include <QTextStream>
  5. #include <QDebug>
  6. #include <QMessageBox>
  7. #include "ExcelTable.h"
  8. #include "ProfessionInfo.h"
  9. MainWindow::MainWindow(QWidget *parent)
  10. : QMainWindow(parent)
  11. , ui(new Ui::MainWindow)
  12. {
  13. ui->setupUi(this);
  14. QStringList evaluate;
  15. evaluate << "姓名";
  16. evaluate << "单位";
  17. evaluate << "职务";
  18. InitTableWidget(evaluate);
  19. }
  20. MainWindow::~MainWindow()
  21. {
  22. delete ui;
  23. }
  24. void MainWindow::InitTableWidget(QStringList header)
  25. {
  26. //设置整行选中的方式
  27. ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectItems);
  28. ui->tableWidget->setColumnCount(header.count());
  29. ui->tableWidget->setRowCount(header.count());
  30. ui->tableWidget->setHorizontalHeaderLabels(header);
  31. ui->tableWidget->setVerticalHeaderLabels(header);
  32. // ui->tableWidget->verticalHeader()->setHidden(true);
  33. // ui->tableWidget->horizontalHeader()->setHidden(true);
  34. ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); //填充表格
  35. // ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents); //然后设置要根据内容使用宽度的列(其他没设置的列自动缩放)
  36. return;
  37. }
  38. QStringList MainWindow::GetTabelContent(QString professionalName, QString nodeLevel)
  39. {
  40. //采取下面方式获取
  41. QStringList tableContents;
  42. int rowCount = ui->tableWidget->rowCount();
  43. int colCount = ui->tableWidget->columnCount();
  44. for (int row = 0; row < rowCount; row++) {
  45. QString rowName = ui->tableWidget->verticalHeaderItem(row)->text();
  46. QString rowNode = QString("%1").arg(row);
  47. for(int col = 0; col < colCount; col++)
  48. {
  49. QString colName = ui->tableWidget->horizontalHeaderItem(col)->text();
  50. if(ui->tableWidget->item(row, col) == NULL)
  51. {
  52. qDebug() << row << col;
  53. QMessageBox::information(NULL, "警告", "表格有空值!!!");
  54. return QStringList();
  55. }
  56. if(ui->tableWidget->item(row,col) != NULL) //一定要先判断非空,否则会报错
  57. {
  58. QString node_Name = nodeLevel+"."+rowNode;
  59. qDebug() << "node_Name = " << node_Name;
  60. QString tableText = ui->tableWidget->item(row,col)->text();
  61. tableContents << professionalName << node_Name << rowName << colName << tableText;
  62. }
  63. }
  64. }
  65. return tableContents;
  66. }
  67. void MainWindow::SetTableContent(QTableWidget* tabWidget, QStringList values)
  68. {
  69. int i = 0;
  70. for(int row = 0; row < tabWidget->rowCount(); row++)
  71. {
  72. for(int col = 0; col < tabWidget->columnCount(); col++)
  73. {
  74. tabWidget->item(row,col)->setText(values[i]);
  75. i++;
  76. }
  77. }
  78. }
  79. void MainWindow::on_restoreBtn_clicked()
  80. {
  81. QStringList values = GetTabelContent("专家1", "1");
  82. qDebug() << values;
  83. // QString fileName;
  84. // QFile myfile(fileName + "/testResult.txt");
  85. // if (myfile.open(QIODevice::WriteOnly | QIODevice::Text)) // 以文本文式写入
  86. // {
  87. // QTextStream out(&myfile);
  88. // foreach (auto wave, temp) {
  89. //// out << param.name << " " << param.value << " " << param.unit;
  90. // }
  91. // out << "\n";
  92. // }
  93. // }
  94. }
  95. void MainWindow::on_pushButton_clicked()
  96. {
  97. ExcelTable * excelTable = new ExcelTable(this);
  98. excelTable->show();
  99. }
  100. void MainWindow::on_pushButton_2_clicked()
  101. {
  102. ProfessionalInfo* professionalInfo = new ProfessionalInfo(this);
  103. professionalInfo->show();
  104. }