
QT中实现串口通信及数据库操作
5星
- 浏览量: 0
- 大小:None
- 文件类型:None
简介:
本教程详细介绍在Qt框架下如何进行串口通信与数据库操作,涵盖相关类库使用、数据传输和存储技术要点,适合开发者学习实践。
以下是代码的简化版本:
```cpp
void Widget::Select() {
QString name = ui->lineEdit->text();
model->setFilter(QObject::tr(id = %1).arg(name));
model->select();
}
void Widget::DeleteCurrentRow() {
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::AddRecord() {
int rowNum = model->rowCount();
int id = 10;
model->insertRow(rowNum);
model->setData(model->index(rowNum,0),id);
//可以直接提交
}
void Widget::BackToFullTable() {
model->setTable(student);
model->setHeaderData(0, Qt::Horizontal, Time);
model->setHeaderData(1, Qt::Horizontal, Temperature);
model->select();
}
void Widget::AmendChanges() {
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::GetTime() {
QString string;
QTime current_time = QTime::currentTime();
int hour = current_time.hour();
int minute = current_time.minute();
int second = current_time.second();
string=QString(%1).arg(hour)+:+QString(%1).arg(minute) +:+QString(%1).arg(second);
ui->Receive->append(string);
int rowNum = model->rowCount();
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();
string=QString(%1).arg(hour)+:+QString(%1).arg(minute) +:+QString(%1).arg(second);
ui->Receive->append(string);
int rowNum = model->rowCount();
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);
}
```
代码中定义了几个关键的方法,包括查询、删除当前行、添加记录、返回全表显示以及提交修改等功能。每个方法都针对特定的操作进行了详细的实现,并且包含了一些必要的UI反馈和数据库操作。
全部评论 (0)


