1 Star 1 Fork 0

Eureka/omrom

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
plcsetwindow.cpp 2.58 KB
一键复制 编辑 原始数据 按行查看 历史
Eureka 提交于 2024-02-02 19:34 . 2/2 19
#include "plcsetwindow.h"
#include "ui_plcsetwindow.h"
PlcSetWindow::PlcSetWindow(QWidget *parent)
: QWidget(parent)
, ui(new Ui::PlcSetWindow)
{
ui->setupUi(this);
this->setWindowTitle("设置PLC");
QSqlDatabase m_db = SqlServer::getInstance()->m_db;
model = new QSqlTableModel(this, m_db);
initializeModel(model);
ui->tableView->setModel(model);
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
connect(model, &QSqlTableModel::dataChanged,this, &PlcSetWindow::reload);
}
void PlcSetWindow::initializeModel(QSqlTableModel *model)
{
QString tablename = "plc";
if(!SqlServer::getInstance()->CheckIfTableExists(tablename))
{
QString sql = "CREATE TABLE `plc` ("
"`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,"
"`ip` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,"
"`port` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,"
"`change_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci";
if(!SqlServer::getInstance()->ExecuteSql(sql))
qDebug()<<"创建表失败";
}
model->setTable("plc");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model->setHeaderData(0, Qt::Horizontal, QObject::tr("name"));
model->setHeaderData(1, Qt::Horizontal, QObject::tr("ip"));
model->setHeaderData(2, Qt::Horizontal, QObject::tr("port"));
model->setHeaderData(3, Qt::Horizontal, QObject::tr("change_time"));
}
PlcSetWindow::~PlcSetWindow()
{
delete ui;
}
void PlcSetWindow::reload()
{
model->submitAll();
model->select();
}
void PlcSetWindow::on_btn_add_clicked()
{
int row = model->rowCount();
model->insertRow(row);
}
void PlcSetWindow::on_btn_delete_clicked()
{
//获取选中的行
int curRow = ui->tableView->currentIndex().row();
//删除该行
model->removeRow(curRow);
int ret = QMessageBox::warning(this, tr("删除当前行"), tr("你确定要删除该行吗"), QMessageBox::Yes | QMessageBox::No);
if (ret == QMessageBox::Yes)
{
//删除该行 提交到数据库
model->submitAll();
}
else
{
//不删除 撤销操作
model->revertAll();
}
model->select();
}
void PlcSetWindow::on_btn_refresh_clicked()
{
model->select();
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/H1145998948/omrom.git
git@gitee.com:H1145998948/omrom.git
H1145998948
omrom
omrom
master

搜索帮助