QT-导出QTableWidget表的信息并格式对齐


Author:QQ174554431


#pragma once
//TraderInformationTable.h
#include <QtGui/QTableWidget>
#include <QtGui/QHeaderView>

class TraderInformationTable :
		public QTableWidget
{
	Q_OBJECT
public:
	TraderInformationTable(QWidget *parent = 0);
	~TraderInformationTable();
	void InitHeader();
	void FakeTraderRecords();

};



//TraderInformationTable.cpp
#include "TraderInformationTable.h"

TraderInformationTable::TraderInformationTable(QWidget *parent) : QTableWidget(parent)
{

	InitHeader();
	FakeTraderRecords();
	resizeColumnsToContents();
}

void TraderInformationTable::InitHeader()
{
	verticalHeader()->hide();
	QStringList headerList;
	headerList.append("Trader Id");
	headerList.append("Trader Account");
	headerList.append("Region");
	headerList.append("Buying Power");
	headerList.append("Currency");
	headerList.append("Office Name");
	setColumnCount(headerList.size());
	setHorizontalHeaderLabels(headerList);
}

void TraderInformationTable::FakeTraderRecords()
{
	QTableWidgetItem *item;
	int rowSize = 0;
	for(int row = 0 ; row <10; row ++)
	{
		rowSize = rowCount();
		insertRow(rowSize);
		for(int i = 0; i<columnCount(); i++)
		{
			item = new QTableWidgetItem();
			if(i == 0)
			{
				item->setText("OPS501");
			}
			else if(i==1)
			{
				item->setText("OPS5001_0000000001");
			}
			else if(i==2)
			{
				item->setText("Asia");
			}
			else if(i==3)
			{
				item->setText("5000000");
			}

			else if(i==4)
			{
				item->setText("USD");
			}

			else if(i==5)
			{
				item->setText("Asia head office");
			}
			
			setItem(rowSize,i,item);

		}
	}

	//////////////////////////////////////////////////////////

	for(int row = 0 ; row <10; row ++)
	{
		rowSize = rowCount();
		insertRow(rowSize);
		for(int i = 0; i<columnCount(); i++)
		{
			item = new QTableWidgetItem();
			if(i == 0)
			{
				item->setText("OPS503");
			}
			else if(i==1)
			{
				item->setText("OPS5003_0000000005");
			}
			else if(i==2)
			{
				item->setText("North America");
			}
			else if(i==3)
			{
				item->setText("50000000000000000");
			}

			else if(i==4)
			{
				item->setText("CAD");
			}

			else if(i==5)
			{
				item->setText("Canada head office");
			}

			setItem(rowSize,i,item);

		}
	}



	//////////////////////////////////////////////////////////

	for(int row = 0 ; row <10; row ++)
	{
		rowSize = rowCount();
		insertRow(rowSize);
		for(int i = 0; i<columnCount(); i++)
		{
			item = new QTableWidgetItem();
			if(i == 0)
			{
				item->setText("DFV501");
			}
			else if(i==1)
			{
				item->setText("DV5001_0000000002");
			}
			else if(i==2)
			{
				item->setText("South America");
			}
			else if(i==3)
			{
				item->setText("2000000000");
			}

			else if(i==4)
			{
				item->setText("USD");
			}

			else if(i==5)
			{
				item->setText("head office capital in Bolivia");
			}
			setItem(rowSize,i,item);
		}
	}


	//////////////////////////////////////////////////////////

	for(int row = 0 ; row <10; row ++)
	{
		rowSize = rowCount();
		insertRow(rowSize);
		for(int i = 0; i<columnCount(); i++)
		{
			item = new QTableWidgetItem();
			if(i == 0)
			{
				item->setText("DFV502");
			}
			else if(i==1)
			{
				item->setText("DV5002_0000000011");
			}
			else if(i==2)
			{
				item->setText("Europe");
			}
			else if(i==3)
			{
				item->setText("4200000000000000000000000000000000000000000");
			}

			else if(i==4)
			{
				item->setText("EUR");
			}

			else if(i==5)
			{
				item->setText("London office capital in  Britain");
			}
			setItem(rowSize,i,item);
		}
	}
	
}

TraderInformationTable::~TraderInformationTable()
{
}




#ifndef EXPORTFILE_H
#define EXPORTFILE_H
//ExportFile.h
#include <QtGui/QDialog>
#include <QtGui/QHBoxLayout>
#include <QtGui/QAction>
#include <QtGui/QMenu>
#include <QtGui/QFileDialog>
#include <fstream>
#include "TraderInformationTable.h"

class ExportFile : public QDialog
{
	Q_OBJECT

public:
	ExportFile(QWidget *parent = 0, Qt::WFlags flags = 0);
	~ExportFile();
public:
	void ExportTable(QTableWidget *in_tableWidget);
	void GetVisualIndexes( QTableWidget* in_table, std::map< int, int> & out_print_index);
private slots:
	void On_Export();
	void on_ContextMenu(const QPoint &pos);
private:
	TraderInformationTable *m_traderInformationTable;
	QHBoxLayout *m_hlayoutTable;
	QAction *m_ExportAct;

	
};

#endif // EXPORTFILE_H



//ExportFile.cpp
#include "exportfile.h"

ExportFile::ExportFile(QWidget *parent, Qt::WFlags flags)
	: QDialog(parent, flags)
{
	m_traderInformationTable = new TraderInformationTable(this);
	m_hlayoutTable = new QHBoxLayout(this);
	m_hlayoutTable->addWidget(m_traderInformationTable);
	setLayout(m_hlayoutTable);
	m_ExportAct = new QAction(tr("Export"),this);
	QObject::connect(m_ExportAct, SIGNAL(triggered()), this,SLOT(On_Export()));
	setContextMenuPolicy(Qt::CustomContextMenu);
	QObject::connect(this,SIGNAL(customContextMenuRequested(const QPoint &)), this,SLOT(on_ContextMenu(const QPoint &)));
	setMinimumSize(800,500);
}

ExportFile::~ExportFile()
{

}

void ExportFile::on_ContextMenu(const QPoint &pos)
{
	QMenu menu(this);
	menu.addAction(m_ExportAct);
	menu.exec(QCursor::pos());
}


void ExportFile::On_Export()
{
	ExportTable(m_traderInformationTable);
}

void ExportFile::ExportTable(QTableWidget *in_tableWidget)
{
	std::string filename;
	filename = "*.txt";
	QFileDialog fileDlg(this);
	fileDlg.setFileMode(QFileDialog::AnyFile);
	fileDlg.setNameFilter(tr("Text (*.txt)"));
	fileDlg.setViewMode(QFileDialog::Detail);
	fileDlg.setAcceptMode(QFileDialog::AcceptSave);
	fileDlg.selectFile((QString)filename.c_str());
	if (fileDlg.exec())
	{
		QStringList fileNames = fileDlg.selectedFiles();
		filename = fileNames[0].toStdString();
	}
	else
	{
		return;
	}
	std::ofstream TxtFile(filename.c_str());
	std::map< int, int> exportIndexMap;
	GetVisualIndexes( in_tableWidget, exportIndexMap);

	//calculate the max length for each field
	std::map<int, int> colMaxLengthMap;
	int colLength = 0;
	int columnCount = exportIndexMap.size();
	for(unsigned int col = 0 ; col < exportIndexMap.size(); col++)
	{
		colLength = in_tableWidget->horizontalHeaderItem( exportIndexMap[col])->text().toStdString().length();
		if(colLength>colMaxLengthMap[col])
		{
			colMaxLengthMap[col] = colLength;
		}
	}

	int rowCount = in_tableWidget->rowCount();
	for(int row = 0 ; row < rowCount; row++)
	{
		columnCount = exportIndexMap.size();
		for(int col = 0 ; col < columnCount ; col++)
		{
			colLength = in_tableWidget->item(row, exportIndexMap[col])->text().toStdString().length();
			if(colLength>colMaxLengthMap[col])
			{
				colMaxLengthMap[col] = colLength;
			}
		}
	}

	//write the file

	int diff = 0;
	columnCount = exportIndexMap.size();
	for(unsigned int col = 0 ; col < exportIndexMap.size(); col++)
	{
		std::string padText;
		int diff = colMaxLengthMap[col] - in_tableWidget->horizontalHeaderItem( exportIndexMap[col])->text().toStdString().length()-1;
		TxtFile <<  in_tableWidget->horizontalHeaderItem( exportIndexMap[col])->text().toStdString(); 
		for(int i= diff ; i>=0 ; i--)
		{
			padText+=" ";
		}
		TxtFile <<padText<<char(9);
	}
	TxtFile << std::endl;

	rowCount = in_tableWidget->rowCount();
	for(int row = 0 ; row < rowCount; row++)
	{
		columnCount = exportIndexMap.size();
		for(int col = 0 ; col < columnCount; col++)
		{
			std::string padText;
			int diff = colMaxLengthMap[col] - in_tableWidget->item(row, exportIndexMap[col])->text().toStdString().length()-1;
			TxtFile << in_tableWidget->item(row, exportIndexMap[col])->text().toStdString();
			for(int i= diff ; i>=0 ; i--)
			{
				padText+=" ";
			}

			if( col != exportIndexMap.size()-1)
			{
				TxtFile<<padText<< char(9);
			}
			else
			{
				TxtFile<<padText<< std::endl;
			}
		}
	}
	TxtFile.close();
}


void ExportFile::GetVisualIndexes( QTableWidget* in_table, std::map< int, int> & out_print_index)
{
	int ColLen = in_table->columnCount();
	for(int col = 0 ; col < ColLen; col++)
	{
		if( in_table->isColumnHidden( col ))
		{
			continue;
		}
		int VisualIndex = in_table->visualColumn( col);
		out_print_index[ VisualIndex ] = col;
	}
}


你可能感兴趣的:(C++,c,C#,Office,qt)