60 lines
1.5 KiB
C++
60 lines
1.5 KiB
C++
#include <qtablewidget.h>
|
|
#include <qlayout.h>
|
|
class tableView
|
|
{
|
|
public:
|
|
QTableWidget* tableWidget;
|
|
int index = 0;
|
|
tableView(std::vector<QString>dataNames = {})
|
|
{
|
|
tableWidget = new QTableWidget();
|
|
tableWidget->setStyleSheet("background-color: white;");
|
|
Init(dataNames);
|
|
}
|
|
~tableView()
|
|
{
|
|
// 清理资源(删除项等)如果有必要的话
|
|
delete tableWidget;
|
|
}
|
|
void Init(std::vector<QString>dataNames)
|
|
{
|
|
QStringList header;
|
|
for (int i = 0; i < dataNames.size(); i++)
|
|
{
|
|
header << dataNames[i];
|
|
tableWidget->setColumnWidth(i, dataNames[i].length() * 25);
|
|
}
|
|
tableWidget->setColumnCount(dataNames.size());
|
|
tableWidget->setHorizontalHeaderLabels(header);
|
|
}
|
|
void addRowDataValue(std::vector<QString>&dataValues)
|
|
{
|
|
|
|
//tableWidget->setRowCount(dataValues.size())
|
|
int rowCnt = tableWidget->rowCount();
|
|
tableWidget->setRowCount(rowCnt + 1);
|
|
for (int i = 0; i < dataValues.size(); i++)
|
|
{
|
|
QTableWidgetItem *item = new QTableWidgetItem(dataValues[i]);
|
|
//item->setBackgroundColor(Qt::white);
|
|
tableWidget->setItem(rowCnt, i, item);
|
|
}
|
|
}
|
|
void addRowDataValue(std::vector<std::string>&dataValues)
|
|
{
|
|
|
|
//tableWidget->setRowCount(dataValues.size());
|
|
int rowCnt = tableWidget->rowCount();
|
|
tableWidget->setRowCount(rowCnt + 1);
|
|
for (int i = 0; i < dataValues.size(); i++)
|
|
{
|
|
QTableWidgetItem* item = new QTableWidgetItem(QString::fromStdString(dataValues[i]));
|
|
tableWidget->setItem(rowCnt, i, item);
|
|
}
|
|
}
|
|
void clearAllData()
|
|
{
|
|
tableWidget->setRowCount(0);
|
|
}
|
|
};
|
|
#pragma once |