【文件属性】:
文件名称:QT串口通讯并进行数据库操作
文件大小:1.84MB
文件格式:RAR
更新时间:2018-12-05 07:39:56
QT串口数据库
void Widget::Select() //查询
{
QString name = ui->lineEdit->text();
model->setFilter(QObject::tr("id = '%1'").arg(name)); //根据姓名进行筛选
model->select(); //显示结果
}
void Widget::Delect() //删除当前行
{
int curRow = ui->tableView->currentIndex().row();
//获取选中的行
model->removeRow(curRow);
//删除该行
int ok = QMessageBox::warning(this,tr("删除当前行!"),tr("你确定"
"删除当前行吗?"),
QMessageBox::Yes,QMessageBox::No);
if(ok == QMessageBox::No)
{
model->revertAll(); //如果不删除,则撤销
}
else
model->submitAll(); //否则提交,在数据库中删除该行
}
void Widget::Add() //插入记录
{
int rowNum = model->rowCount(); //获得表的行数
int id = 10;
model->insertRow(rowNum); //添加一行
model->setData(model->index(rowNum,0),id);
//model->submitAll(); //可以直接提交
}
void Widget::Back() //返回全表
{
model->setTable("student"); //重新关联表
model->setHeaderData(0, Qt::Horizontal, "Time");
model->setHeaderData(1, Qt::Horizontal, "Temperature");
model->select(); //这样才能再次显示整个表的内容
}
void Widget::Amend() //提交修改
{
model->database().transaction(); //开始事务操作
if (model->submitAll())
{
model->database().commit(); //提交
}
else
{
model->database().rollback(); //回滚
QMessageBox::warning(this, tr("tableModel"),
tr("数据库错误: %1").arg(model->lastError().text()));
}
}
void Widget::Get_time()
{
QString string;
QTime current_time = QTime::currentTime();
int hour = current_time.hour();
int minute = current_time.minute();
int second = current_time.second();
// int msec = current_time.msec();
string=QString("%1").arg(hour)+":"+QString("%1").arg(minute)
+":"+QString("%1").arg(second);
ui->Receive->append(string);
//qDebug() << string;
int rowNum = model->rowCount(); //获得表的行数
// int id = 10;
model->insertRow(rowNum); //添加一行
model->setData(model->index(rowNum,0),string);
model->submitAll();
}
void Widget::readMyCom()
{
QByteArray temp = myCom->readAll();
if(temp.size()!=0)
{
QString string;
QTime current_time = QTime::currentTime();
int hour = current_time.hour();
int minute = current_time.minute();
int second = current_time.second();
// int msec = current_time.msec();
string=QString("%1").arg(hour)+":"+QString("%1").arg(minute)
+":"+QString("%1").arg(second);
ui->Receive->append(string);
//qDebug() << string;
int rowNum = model->rowCount(); //获得表的行数
// int id = 10;
model->insertRow(rowNum); //添加一行
model->setData(model->index(rowNum,0),string);
model->setData(model->index(rowNum,1),temp);
model->submitAll();
data_light=temp.toInt();
}
ui->Receive->append(temp);
}
void Widget::openCom()
{
QString portName = ui->portNameComboBox->currentText();
myCom = new Win_QextSerialPort(portName,QextSerialBase::EventDriven);
myCom ->open(QIODevice::ReadWrite);
if(ui->baudRateComboBox->currentText()==tr("9600"))
myCom->setBaudRate(BAUD9600);
else if(ui->baudRateComboBox->currentText()==tr("115200"))
myCom->setBaudRate(BAUD115200);
myCom->setFlowControl(FLOW_OFF);
myCom->setTimeout(500);
connect(myCom,SIGNAL(readyRead()),this,SLOT(readMyCom()));
ui->openMyComBtn->setEnabled(false);
ui->closeMyComBtn->setEnabled(true);
ui->baudRateComboBox->setEnabled(false);
ui->portNameComboBox->setEnabled(false);
}
【文件预览】:
数据库+串口
----tableModel()
--------ui_widget.h(8KB)
--------tableModel()
--------widget.ui(7KB)
--------qextserialbase.cpp(6KB)
--------release()
--------database.h(659B)
--------debug()
--------tableModel.pro(561B)
--------main.cpp(335B)
--------tableModel.pro.user.1c5edd4(3KB)
--------Makefile(5KB)
--------win_qextserialport.cpp(34KB)
--------tableModel.pro.user(18KB)
--------widget.cpp(7KB)
--------Makefile.Debug(8KB)
--------qextserialport.h(867B)
--------widget.h(1012B)
--------win_qextserialport.h(4KB)
--------Makefile.Release(8KB)
--------QT笔记_数据库总结(三)之SQL模型类-QSqlTableModel模型 - Leo Chin - 博客园_files()
--------qextserialport.cpp(3KB)
--------qextserialbase.h(7KB)
网友评论
- 正在学习中