5 Star 17 Fork 18

sj0830 / qt_project

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
frmplayback.cpp 11.14 KB
一键复制 编辑 原始数据 按行查看 历史
sj0830 提交于 2020-02-18 23:34 . 首次提交,建立基础开发版本;
#include "frmplayback.h"
#include "ui_frmplayback.h"
#include "myhelper.h"
#include "iconhelper.h"
#include "myapp.h"
#include "excelhelper.h"
#pragma execution_character_set("utf-8")
frmPlayback::frmPlayback(QWidget *parent) :
QDialog(parent),
ui(new Ui::frmPlayback)
{
ui->setupUi(this);
this->InitStyle();
this->InitForm();
}
frmPlayback::~frmPlayback()
{
delete ui;
}
void frmPlayback::InitStyle()
{
this->setProperty("Form", true);
this->setGeometry(qApp->desktop()->availableGeometry());
//设置窗体标题栏隐藏
this->setWindowFlags(Qt::FramelessWindowHint |
Qt::WindowSystemMenuHint |
Qt::WindowMinMaxButtonsHint);
//设置图形字体
IconHelper::Instance()->SetIcon(ui->lab_Ico, QChar(0xf03d), 11);
IconHelper::Instance()->SetIcon(ui->btnMenu_Close, QChar(0xf00d), 10);
//关联关闭按钮
connect(ui->btnMenu_Close, SIGNAL(clicked()), this, SLOT(close()));
}
void frmPlayback::InitForm()
{
for (int i = 1; i <= 255; i++) {
QString tempNVRID;
if (i < 10) {
tempNVRID = QString("00000%1").arg(i);
} else if (i < 100) {
tempNVRID = QString("0000%1").arg(i);
} else if (i < 1000) {
tempNVRID = QString("000%1").arg(i);
}
ui->cboxNVRID->addItem(tempNVRID);
}
QStringList tempNVRType = myApp::NVRType.split(";");
foreach (QString nvrType, tempNVRType) {
if (nvrType.trimmed() != "") {
ui->cboxNVRType->addItem(nvrType);
}
}
//最后一列自动填充
ui->tableMain->horizontalHeader()->setStretchLastSection(true);
//奇数偶数行不同背景色
ui->tableMain->setAlternatingRowColors(true);
//选中整行,每次只允许选中一行
ui->tableMain->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableMain->setSelectionMode(QAbstractItemView::SingleSelection);
queryModule = new QSqlQueryModel(this);
LoadNVRInfo();
QString columnNames[8]; //列名数组
int columnWidths[8]; //列宽数组
//初始化表格列名和列宽
columnNames[0] = "编号";
columnNames[1] = "名称";
columnNames[2] = "地址";
columnNames[3] = "IP地址";
columnNames[4] = "类型";
columnNames[5] = "用户名";
columnNames[6] = "密码";
columnNames[7] = "状态";
int width = myApp::DeskWidth - 246;
columnWidths[0] = width * 0.08;
columnWidths[1] = width * 0.16;
columnWidths[2] = width * 0.20;
columnWidths[3] = width * 0.12;
columnWidths[4] = width * 0.08;
columnWidths[5] = width * 0.12;
columnWidths[6] = width * 0.12;
columnWidths[7] = width * 0.08;
//依次设置列标题列宽
for (int i = 0; i < 8; i++) {
queryModule->setHeaderData(i, Qt::Horizontal, columnNames[i]);
ui->tableMain->setColumnWidth(i, columnWidths[i]);
}
}
void frmPlayback::LoadNVRInfo()
{
QString sql = "select * from [NVRInfo] order by [NVRID] asc";
queryModule->setQuery(sql);
ui->tableMain->setModel(queryModule);
}
bool frmPlayback::IsExistNVRID(QString NVRID)
{
QSqlQuery query;
QString sql = "select [NVRID] from [NVRInfo]";
sql += " where [NVRID]='" + NVRID + "'";
query.exec(sql);
return query.next();
}
bool frmPlayback::IsExistNVRIP(QString NVRIP)
{
QSqlQuery query;
QString sql = "select [NVRIP] from [NVRInfo]";
sql += " where [NVRIP]='" + NVRIP + "'";
query.exec(sql);
return query.next();
}
void frmPlayback::on_btnAdd_clicked()
{
QString NVRID = ui->cboxNVRID->currentText();
QString NVRName = ui->txtNVRName->text();
QString NVRAddr = ui->txtNVRAddr->text();
QString NVRIP = ui->txtNVRIP->text();
QString NVRType = ui->cboxNVRType->currentText();
QString NVRUserName = ui->txtNVRUserName->text();
QString NVRUserPwd = ui->txtNVRUserPwd->text();
QString NVRUse = ui->cboxNVRUse->currentText();
if (NVRName == "") {
myHelper::ShowMessageBoxError("名称不能为空,请重新填写!");
ui->txtNVRName->setFocus();
return;
}
if (NVRIP == "") {
myHelper::ShowMessageBoxError("IP地址不能为空,请重新填写!");
ui->txtNVRIP->setFocus();
return;
}
if (!myHelper::IsIP(NVRIP)) {
myHelper::ShowMessageBoxError("IP地址不合法,请重新填写!");
ui->txtNVRIP->setFocus();
return;
}
//检测编号和IP是否唯一
if (IsExistNVRID(NVRID)) {
myHelper::ShowMessageBoxError("编号已经存在,请重新选择!");
return;
}
if (IsExistNVRIP(NVRIP)) {
myHelper::ShowMessageBoxError("IP地址已经存在,请重新填写!");
ui->txtNVRIP->setFocus();
return;
}
QSqlQuery query;
QString sql = "insert into [NVRInfo](";
sql += "[NVRID],[NVRName],[NVRAddr],[NVRIP],";
sql += "[NVRType],[NVRUserName],[NVRUserPwd],[NVRUse])";
sql += "values('";
sql += NVRID + "','";
sql += NVRName + "','";
sql += NVRAddr + "','";
sql += NVRIP + "','";
sql += NVRType + "','";
sql += NVRUserName + "','";
sql += NVRUserPwd + "','";
sql += NVRUse + "')";
query.exec(sql);
LoadNVRInfo();
ui->cboxNVRID->setCurrentIndex(ui->cboxNVRID->currentIndex() + 1);
}
void frmPlayback::on_btnDelete_clicked()
{
if (ui->tableMain->currentIndex().row() < 0) {
myHelper::ShowMessageBoxError("请选择要删除的硬盘录像机!");
return;
}
QString tempNVRID = queryModule->record(
ui->tableMain->currentIndex().row())
.value(0).toString();
if (tempNVRID == "000255") {
myHelper::ShowMessageBoxError("默认NVR不能删除!");
return;
}
if (myHelper::ShowMessageBoxQuesion("确定要删除硬盘录像机吗?对应摄像机将同步删除!") == 1) {
QSqlQuery query;
QString sql = "delete from [NVRInfo] where [NVRID]='" + tempNVRID + "'";
query.exec(sql);
myHelper::Sleep(100);
//同步删除对应摄像机信息
sql = "delete from [IPCInfo] where [NVRID]='" + tempNVRID + "'";
query.exec(sql);
myHelper::Sleep(100);
//同步删除对应轮询表信息
sql = "delete from [PollInfo] where [NVRID]='" + tempNVRID + "'";
query.exec(sql);
myHelper::Sleep(100);
LoadNVRInfo();
}
}
void frmPlayback::on_btnUpdate_clicked()
{
if (ui->tableMain->currentIndex().row() < 0) {
myHelper::ShowMessageBoxError("请选择要修改的硬盘录像机!");
return;
}
//获取原有NVRID
QString tempNVRID = queryModule->record(
ui->tableMain->currentIndex().row())
.value(0).toString();
QString tempNVRIP = queryModule->record(
ui->tableMain->currentIndex().row())
.value(3).toString();
if (tempNVRID == "000255") {
myHelper::ShowMessageBoxError("默认NVR不能修改!");
return;
}
QString NVRID = ui->cboxNVRID->currentText();
QString NVRName = ui->txtNVRName->text();
QString NVRAddr = ui->txtNVRAddr->text();
QString NVRIP = ui->txtNVRIP->text();
QString NVRType = ui->cboxNVRType->currentText();
QString NVRUserName = ui->txtNVRUserName->text();
QString NVRUserPwd = ui->txtNVRUserPwd->text();
QString NVRUse = ui->cboxNVRUse->currentText();
if (NVRID != tempNVRID) {
//检测编号是否和已经存在的除自己之外的编号相同
if (IsExistNVRID(NVRID)) {
myHelper::ShowMessageBoxError("编号已经存在,请重新选择!");
return;
}
}
if (NVRIP != tempNVRIP) {
//检测IP地址是否和已经存在的除自己之外的IP地址相同
if (IsExistNVRIP(NVRIP)) {
myHelper::ShowMessageBoxError("IP地址已经存在,请重新填写!");
ui->txtNVRIP->setFocus();
return;
}
}
if (!myHelper::IsIP(NVRIP)) {
myHelper::ShowMessageBoxError("IP地址不合法,请重新填写!");
ui->txtNVRIP->setFocus();
return;
}
QSqlQuery query;
QString sql = "update [NVRInfo] set";
sql += " [NVRID]='" + NVRID;
sql += "',[NVRName]='" + NVRName;
sql += "',[NVRAddr]='" + NVRAddr;
sql += "',[NVRIP]='" + NVRIP;
sql += "',[NVRType]='" + NVRType;
sql += "',[NVRUserName]='" + NVRUserName;
sql += "',[NVRUserPwd]='" + NVRUserPwd;
sql += "',[NVRUse]='" + NVRUse;
sql += "' where [NVRID]='" + tempNVRID + "'";
query.exec(sql);
myHelper::Sleep(100);
//同步更新摄像机对应硬盘录像机信息
sql = "update [IPCInfo] set";
sql += " [NVRID]='" + NVRID;
sql += "',[NVRName]='" + NVRName;
sql += "' where [NVRID]='" + tempNVRID + "'";
query.exec(sql);
myHelper::Sleep(100);
//同步更新轮询表信息
sql = "update [PollInfo] set";
sql += " [NVRID]='" + NVRID;
sql += "',[NVRName]='" + NVRName;
sql += "' where [NVRID]='" + tempNVRID + "'";
query.exec(sql);
myHelper::Sleep(100);
LoadNVRInfo();
}
void frmPlayback::on_btnExcel_clicked()
{
QString columnNames[8];
int columnWidths[8];
columnNames[0] = "编号";
columnNames[1] = "名称";
columnNames[2] = "地址";
columnNames[3] = "IP地址";
columnNames[4] = "类型";
columnNames[5] = "用户名";
columnNames[6] = "密码";
columnNames[7] = "状态";
columnWidths[0] = 80;
columnWidths[1] = 120;
columnWidths[2] = 150;
columnWidths[3] = 120;
columnWidths[4] = 60;
columnWidths[5] = 60;
columnWidths[6] = 60;
columnWidths[7] = 60;
QStringList content;
QSqlQuery query;
QString sql = "select * from [NVRInfo] order by [NVRID] asc";
query.exec(sql);
int columnCount = query.record().count();
//循环遍历数据,存储到QStringList中
while (query.next()) {
QString temp = "";
for (int i = 0; i < columnCount; i++) {
temp += query.value(i).toString() + ";";
}
content << temp.mid(0, temp.length() - 1); //去掉末尾的分号
}
//调用导出数据函数
QString title = "硬盘录像机信息";
ExcelHelper::Instance()->ToExcel(myApp::AppPath + "DB/" + title + ".xls", title, title, columnNames, columnWidths, columnCount, content);
if (myHelper::ShowMessageBoxQuesion("导出数据到Excel成功,现在就打开吗?") == 1) {
QDesktopServices::openUrl(QUrl::fromLocalFile(myApp::AppPath + "DB/" + title + ".xls"));
}
}
void frmPlayback::on_tableMain_pressed(const QModelIndex &index)
{
QSqlRecord record = queryModule->record(index.row());
ui->cboxNVRID->setCurrentIndex(ui->cboxNVRID->findText(record.value(0).toString()));
ui->txtNVRName->setText(record.value(1).toString());
ui->txtNVRAddr->setText(record.value(2).toString());
ui->txtNVRIP->setText(record.value(3).toString());
ui->cboxNVRType->setCurrentIndex(ui->cboxNVRType->findText(record.value(4).toString()));
ui->txtNVRUserName->setText(record.value(5).toString());
ui->txtNVRUserPwd->setText(record.value(6).toString());
ui->cboxNVRUse->setCurrentIndex(ui->cboxNVRUse->findText(record.value(7).toString()));
}
C/C++
1
https://gitee.com/sjsun/qt_project.git
git@gitee.com:sjsun/qt_project.git
sjsun
qt_project
qt_project
master

搜索帮助