QT 点击按钮呼出新窗口,加载文件数据至QTableWidget

读取文件内容并写入QTableWidget
读取文件类

#pragma once
#include <QString>


class TableData
{
public:
		TableData() {
	}
	TableData(QString  time, QString longitude, QString latitude, QString altitude, QString fnunmberlayout1, QString fnunmberlayout2, QString fnunmberlayout3, QString fnunmberlayout4)
	{
		Time = time;
		Longitude = longitude;
		Latitude = latitude;
		Altitude = altitude;
		Fnunmberlayout1 = fnunmberlayout1;
		Fnunmberlayout2 = fnunmberlayout2;
		Fnunmberlayout3 = fnunmberlayout3;
		Fnunmberlayout4 = fnunmberlayout4;
	}

	QString getTime() {
		return QString("%1").arg(Time);
	}

	QString getLongitude() {
		return QString("%1").arg(Longitude);
	}
	QString getLatitude() {
		return QString("%1").arg(Latitude);
	}
	QString getAltitude() {
		return QString("%1").arg(Altitude);
	}
	QString getFnunmberlayout1() {
		return QString("%1").arg(Fnunmberlayout1);
	}
	QString getFnunmberlayout2() {
		return QString("%1").arg(Fnunmberlayout2);
	}
	QString getFnunmberlayout3() {
		return QString("%1").arg(Fnunmberlayout3);
	}
	QString getFnunmberlayout4() {
		return QString("%1").arg(Fnunmberlayout4);
	}


private:
	QString Time;
	QString Longitude;
	QString Latitude;
	QString Altitude;
	QString Fnunmberlayout1;
	QString Fnunmberlayout2;
	QString Fnunmberlayout3;
	QString Fnunmberlayout4;
};

窗口cpp文件

#include "pathwindow.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QTableWidget>
#include <QPushButton>
#include <QFile>
#include <QTextStream>
#include <QFileDialog>
#include <QMessageBox>
#include <QHeaderView>
Pathwindow::Pathwindow(QWidget *parent)
	: QDialog(parent)
{
	initLayout();
}



void Pathwindow::initLayout()
{
	this->setWindowTitle(QStringLiteral("路径数据"));

	QVBoxLayout* vmainlayout = new QVBoxLayout(this);
	QWidget* widget = new QWidget();
	vmainlayout->addWidget(widget);


	QHBoxLayout* mainlayout = new QHBoxLayout();
	QVBoxLayout* vlayout = new QVBoxLayout();

	tableWidget = new QTableWidget();
	tableWidget->setColumnCount(7);
	QStringList header;
	header << QStringLiteral("时间") << QStringLiteral("经度")<< QStringLiteral("纬度")<< QStringLiteral("高程")<< QStringLiteral("四元数1")
		<< QStringLiteral("四元数2")<< QStringLiteral("四元数3")<< QStringLiteral("四元数4");
	tableWidget->setHorizontalHeaderLabels(header);

	QHeaderView* headerview = tableWidget->horizontalHeader();
	headerview->setSectionResizeMode(QHeaderView::Stretch);

	mainlayout->addWidget(tableWidget);


	QHBoxLayout* buttonlayout = new QHBoxLayout();

	QPushButton* open = new QPushButton(QStringLiteral("打开"));
	QObject::connect(open, SIGNAL(clicked()), this, SLOT(popenFile()));
	QPushButton* save = new QPushButton(QStringLiteral("保存"));
	QObject::connect(save, SIGNAL(clicked()), this, SLOT(saveFile()));
	QPushButton* saveas = new QPushButton(QStringLiteral("另存为"));
	QObject::connect(save, SIGNAL(clicked()), this, SLOT(saveasFile()));
	QPushButton* confirm = new QPushButton(QStringLiteral("关闭"));
	QObject::connect(confirm, SIGNAL(clicked()), this, SLOT(closePathWidget()));

	buttonlayout->addStretch();
	buttonlayout->addWidget(open);
	buttonlayout->addWidget(save);
	buttonlayout->addWidget(saveas);
	buttonlayout->addWidget(confirm);

	QWidget* confirmWidget = new QWidget();
	confirmWidget->setLayout(buttonlayout);
	vmainlayout->addWidget(confirmWidget);

	mainlayout->setMargin(10);
	mainlayout->setSpacing(5);
	widget->setLayout(mainlayout);

}

void Pathwindow::closePathWidget() {
	this->close();
}



void Pathwindow::popenFile() {


	filename = QFileDialog::getOpenFileName(this, QStringLiteral("选择path文件"), ".", QStringLiteral("Image Files(*.path)"));

	if (filename.isEmpty()) {
		QMessageBox::warning(this, "Warning", QStringLiteral("未选择打开文件"));
		return;
	};
	QFile file(filename);
	std::vector<TableData> vec;
	if (file.open(QIODevice::ReadOnly | QIODevice::Text))
	{
		QTextStream stream(&file);
		//循环节点
		while (!stream.atEnd())
		{
			
			QStringList list = stream.readLine().split(QRegExp(" "));
			TableData tabledata(static_cast<QString>(list.at(0)), static_cast<QString>(list.at(1)),
				static_cast<QString>(list.at(2)), static_cast<QString>(list.at(3)),
				static_cast<QString>(list.at(4)), static_cast<QString>(list.at(5)),
				static_cast<QString>(list.at(6)), static_cast<QString>(list.at(7)));
			vec.push_back(tabledata);

		}

	}


	tableWidget->clear();
	tableWidget->setRowCount(vec.size());
	for (int i = 0; i < vec.size(); i++)
	{
		tableWidget->setItem(i, 0, new QTableWidgetItem(vec[i].getTime()));
		tableWidget->setItem(i, 1, new QTableWidgetItem(vec[i].getLongitude()));
		tableWidget->setItem(i, 2, new QTableWidgetItem(vec[i].getLatitude()));
		tableWidget->setItem(i, 3, new QTableWidgetItem(vec[i].getAltitude()));
		tableWidget->setItem(i, 4, new QTableWidgetItem(vec[i].getFnunmberlayout1()));
		tableWidget->setItem(i, 5, new QTableWidgetItem(vec[i].getFnunmberlayout2()));
		tableWidget->setItem(i, 6, new QTableWidgetItem(vec[i].getFnunmberlayout3()));
		tableWidget->setItem(i, 7, new QTableWidgetItem(vec[i].getFnunmberlayout4()));
	}
	file.close();
}

void Pathwindow::saveFile()
{

	if (filename.isEmpty()) {
		filename = QFileDialog::getSaveFileName(this, QStringLiteral("保存"), ".", QStringLiteral("工程文件(*.path)"));
		if (filename.isEmpty()) {
			QMessageBox::warning(this, "Warning", QStringLiteral("保存失败"));
			return;
		}
	}

	QFile file(filename);
	if (!file.open(QIODevice::WriteOnly)) {
		QMessageBox::warning(this, "Warning", QStringLiteral("保存失败") + file.errorString());
		return;
	}
	QTextStream out(&file);
	QString text;
	out << text;
	file.close();
	QMessageBox::about(NULL, QStringLiteral("提示"), QStringLiteral("保存成功"));
	}



void Pathwindow::saveasFile()
{
	filename = QFileDialog::getSaveFileName(this, QStringLiteral("保存"), ".", QStringLiteral("工程文件(*.path)"));
	if (filename.isEmpty()) {
			QMessageBox::warning(this, "Warning", QStringLiteral("保存失败"));
			return;
		
	}

	QFile file(filename);
	if (!file.open(QIODevice::WriteOnly)) {
		QMessageBox::warning(this, "Warning", QStringLiteral("保存失败") + file.errorString());
		return;
	}
	QTextStream out(&file);
	QString text;
	out << text;
	file.close();
	QMessageBox::about(NULL, QStringLiteral("提示"), QStringLiteral("保存成功"));
}

窗口头文件

#pragma once
#include <QPushButton>
#include <QtWidgets/QMainWindow>
#include <QLabel>
#include "pathwindow.h"
//#include "ui_mainwindow.h"

class mainwindow2 : public QMainWindow
{
	Q_OBJECT

public:
	mainwindow2(QWidget *parent = Q_NULLPTR);
signals:
	void signalOSGEarth(bool);

private slots:
	void getButtonText();
	void getButtonPause();
	void getButtonStart();
	void setButtonStart();
	void setButtonStop();
	void setButtonReload();
	void reloadPath();

private:
	bool _start = false;
	bool _pause = false;
	bool _stop = false;
	bool _hide = true;
	QPushButton* pushbutton1;
	QPushButton* pushbutton2;
	QPushButton* pushbutton3;
	QPushButton* pushbutton4;
	QPushButton* pushbutton5;
	QPushButton* pushbutton6;
	QLabel* label1;
	//Ui::mainwindowClass ui;
	void openFile();
	Pathwindow* pathwindow;
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值