
这个参照了一个大佬的代码,时间太久,不记得出处了。
#include "AttitudeWidget.h"
#include "ui_AttitudeWidget.h"
#include "AttitudeWorker.h"
#include <QFileDialog>
#include <QThreadPool>
#include "QCustomPlot/XxwCustomPlot.h"
AttitudeWidget::AttitudeWidget(QSettings* poSet, QWidget* parent) :
QWidget(parent),
ui(new Ui::AttitudeWidget), poSet(poSet)
{
ui->setupUi(this);
db = QSqlDatabase::addDatabase("QSQLITE", "Main");
db.setDatabaseName("SIG.db"); //设置数据库名称
if(!db.isOpen())
{
if(!db.open())
{
qDebugV0() << "open error:" << db.lastError().text();
this->recvMsg(db.lastError().text());
return;
}
else
{
qDebugV0() << "database connect OK!";
}
}
QSqlQuery query(db);
query.exec("CREATE TABLE IF NOT EXISTS Attitude(DevId TEXT, "
"TaskName TEXT, CreateTime TEXT, Timestamp TEXT, "
"pitch REAL, roll REAL, yaw REAL)");
this->refresh();
ui->splitterH->setStretchFactor(0, 2);
ui->splitterH->setStretchFactor(1, 1);
ui->splitterV->setStretchFactor(0, 4);
ui->splitterV->setStretchFactor(1, 1);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
}
AttitudeWidget::~AttitudeWidget()
{
delete ui;
db.close();
}
void AttitudeWidget::on_pushButtonImport_clicked()
{
QString oStrPath = poSet->value("PATH_OPEN").toString();
if(oStrPath.isEmpty())
{
oStrPath = QCoreApplication::applicationDirPath();
}
QStringList aoStrFileName = QFileDialog::getOpenFileNames(this, tr("打开姿态文件"),
oStrPath,
tr("Raw(*_atti*.dat)"));
foreach(QString oStrFileName, aoStrFileName)
{
AttitudeWorker* poWorker = new AttitudeWorker(oStrFileName);
poWorker->setParent(nullptr);
connect(poWorker, &AttitudeWorker::sigMsg, this, &AttitudeWidget::recvMsg);
QThreadPool::globalInstance()->start(poWorker);
}
if(!aoStrFileName.isEmpty())
{
QFileInfo oFileInfo( aoStrFileName.first() );
if(! oFileInfo.absolutePath().isEmpty())
{
poSet->setValue("PATH_OPEN", oFileInfo.absolutePath());
}
}
}
void AttitudeWidget::on_pushButtonClear_clicked()
{
QMessageBox::StandardButton standarButton = QMessageBox::question(this,
"警告",
"谨慎操作!",
QMessageBox::Ok | QMessageBox::Cancel,
QMessageBox::Cancel);
switch (standarButton)
{
case QMessageBox::Ok:
if(poModel != nullptr)
{
poModel->setQuery("delete from Attitude");
poModel->submit();
}
break;
case QMessageBox::Cancel:
break;
default:
break;
}
}
void AttitudeWidget::on_pushButtonRefresh_clicked()
{
this->refresh();
}
void AttitudeWidget::recvMsg(QString oStrMsg)
{
ui->textBrowser->append(oStrMsg);

文章描述了一个使用Qt库进行数据库操作(SQLite)的AttitudeWidget类,包括数据导入、清除、刷新、搜索和姿态数据的绘制。展示了如何使用QComboBox、QTableView、线程以及QCustomPlot进行界面交互和数据处理。
最低0.47元/天 解锁文章
4660

被折叠的 条评论
为什么被折叠?



