代码拉取完成,页面将自动刷新
#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();
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。