#include "mainwindow.h" #include "ui_mainwindow.h" #include #include #include #include #include "ExcelTable.h" #include "ProfessionInfo.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QStringList evaluate; evaluate << "姓名"; evaluate << "单位"; evaluate << "职务"; InitTableWidget(evaluate); } MainWindow::~MainWindow() { delete ui; } void MainWindow::InitTableWidget(QStringList header) { //设置整行选中的方式 ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectItems); ui->tableWidget->setColumnCount(header.count()); ui->tableWidget->setRowCount(header.count()); ui->tableWidget->setHorizontalHeaderLabels(header); ui->tableWidget->setVerticalHeaderLabels(header); // ui->tableWidget->verticalHeader()->setHidden(true); // ui->tableWidget->horizontalHeader()->setHidden(true); ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); //填充表格 // ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents); //然后设置要根据内容使用宽度的列(其他没设置的列自动缩放) return; } QStringList MainWindow::GetTabelContent(QString professionalName, QString nodeLevel) { //采取下面方式获取 QStringList tableContents; int rowCount = ui->tableWidget->rowCount(); int colCount = ui->tableWidget->columnCount(); for (int row = 0; row < rowCount; row++) { QString rowName = ui->tableWidget->verticalHeaderItem(row)->text(); QString rowNode = QString("%1").arg(row); for(int col = 0; col < colCount; col++) { QString colName = ui->tableWidget->horizontalHeaderItem(col)->text(); if(ui->tableWidget->item(row, col) == NULL) { qDebug() << row << col; QMessageBox::information(NULL, "警告", "表格有空值!!!"); return QStringList(); } if(ui->tableWidget->item(row,col) != NULL) //一定要先判断非空,否则会报错 { QString node_Name = nodeLevel+"."+rowNode; qDebug() << "node_Name = " << node_Name; QString tableText = ui->tableWidget->item(row,col)->text(); tableContents << professionalName << node_Name << rowName << colName << tableText; } } } return tableContents; } void MainWindow::SetTableContent(QTableWidget* tabWidget, QStringList values) { int i = 0; for(int row = 0; row < tabWidget->rowCount(); row++) { for(int col = 0; col < tabWidget->columnCount(); col++) { tabWidget->item(row,col)->setText(values[i]); i++; } } } void MainWindow::on_restoreBtn_clicked() { QStringList values = GetTabelContent("专家1", "1"); qDebug() << values; // QString fileName; // QFile myfile(fileName + "/testResult.txt"); // if (myfile.open(QIODevice::WriteOnly | QIODevice::Text)) // 以文本文式写入 // { // QTextStream out(&myfile); // foreach (auto wave, temp) { //// out << param.name << " " << param.value << " " << param.unit; // } // out << "\n"; // } // } } void MainWindow::on_pushButton_clicked() { ExcelTable * excelTable = new ExcelTable(this); excelTable->show(); } void MainWindow::on_pushButton_2_clicked() { ProfessionalInfo* professionalInfo = new ProfessionalInfo(this); professionalInfo->show(); }