dialog.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include "dialog.h"
  2. #include "ui_dialog.h"
  3. #include <QTimer> //定时器类
  4. #include <windows.h>
  5. #include<iostream>
  6. #include<qcursor.h>
  7. Dialog::Dialog(QWidget *parent) :
  8. QDialog(parent),
  9. ui(new Ui::Dialog)
  10. {
  11. Qt::WindowFlags windowFlag = Qt::Dialog;
  12. windowFlag |= Qt::WindowMinimizeButtonHint;
  13. windowFlag |= Qt::WindowMaximizeButtonHint;
  14. windowFlag |= Qt::WindowCloseButtonHint;
  15. setWindowFlags(windowFlag);
  16. ui->setupUi(this);
  17. QFile file("./prjfiles/cfg/sxkyprj.cfg");//.cfg为文本文件
  18. QByteArray jsonData;
  19. if(file.open(QIODevice::ReadOnly)){
  20. jsonData = file.readAll();
  21. file.close();
  22. // QMessageBox::information(this,"正确","打开配置文件正确.");
  23. }
  24. else {
  25. QMessageBox::critical(this,"错误","打开配置文件错误,软件无法正常运行,请联系厂家");//默认是模态的
  26. return;
  27. }
  28. phkcamera = new hkcamera();
  29. phkcamera->inicamera(jsonData);
  30. //初始化对话框界面参数
  31. QStringList list = phkcamera->getallcameralocations();
  32. ui->LocationcomboBox->addItems(list);
  33. QString strlocation = list.at(0);
  34. QJsonObject camerainfoobj = phkcamera->camerainfomap.value(strlocation);
  35. QString qstrip = camerainfoobj.value("ip").toString();
  36. // LONG port = camerainfoobj.value("port").toInt();
  37. QString qstraccount = camerainfoobj.value("account").toString();
  38. QString qstrpwd = camerainfoobj.value("password").toString();
  39. ui->IPLineEdit->setText(qstrip);
  40. ui->AcccountLineEdit->setText(qstraccount);
  41. ui->PwdLineEdit->setText(qstrpwd);
  42. phkcamera->m_userid = phkcamera->camerauseridmap.value(qstrip);
  43. //combobox内容选择变化时触发currentIndexChanged信号,注意这里用Qt4信号槽比较好,避免混淆
  44. connect(ui->LocationcomboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onComboxSelect(int)));
  45. }
  46. void Dialog::onComboxSelect(int index)
  47. {
  48. //获取combobox当前内容
  49. QString strlocation = ui->LocationcomboBox->currentText();
  50. QMap<QString, QJsonObject> camerainfomap = phkcamera->camerainfomap;
  51. QJsonObject camerainfoobj = camerainfomap.value(strlocation);
  52. QString qstrip = camerainfoobj.value("ip").toString();
  53. // LONG port = camerainfoobj.value("port").toInt();
  54. QString qstraccount = camerainfoobj.value("account").toString();
  55. QString qstrpwd = camerainfoobj.value("password").toString();
  56. ui->IPLineEdit->setText(qstrip);
  57. ui->AcccountLineEdit->setText(qstraccount);
  58. ui->PwdLineEdit->setText(qstrpwd);
  59. phkcamera->m_userid = phkcamera->camerauseridmap.value(qstrip);
  60. // QMessageBox::information(this, "title", "your select item is " + text, NULL);
  61. }
  62. Dialog::~Dialog()
  63. {
  64. delete ui;
  65. }
  66. void Dialog::closeEvent ( QCloseEvent* e )
  67. {
  68. if( QMessageBox::question(this,
  69. tr("Quit"),
  70. tr("Are you sure to quit this application?"),
  71. QMessageBox::Yes, QMessageBox::No )
  72. == QMessageBox::Yes){
  73. e->accept();//不会将事件传递给组件的父组件
  74. if(nullptr != phkcamera)
  75. {
  76. phkcamera->logout();
  77. delete phkcamera;
  78. }
  79. }
  80. else
  81. e->ignore();
  82. }
  83. //void Dialog::on_pushButton_clicked()
  84. //{
  85. //}
  86. void Dialog::on_OpenButton_clicked()
  87. {
  88. QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
  89. HWND hWnd = (HWND)ui->DisplayLabel->winId(); //成功时,即可实时显示在该label上
  90. BOOL bdisplaypic = phkcamera->displaypic(hWnd);
  91. if(!bdisplaypic){
  92. QApplication::restoreOverrideCursor();
  93. // QMessageBox::critical(this,"错误","连接相机成功,但预览失败");//默认是模态的
  94. return;
  95. }
  96. ui->OpenButton->setEnabled(false);
  97. ui->CloseButton->setEnabled(true);
  98. QApplication::restoreOverrideCursor();
  99. //QMessageBox::information(this,"成功","预览成功");//默认是模态的
  100. return;
  101. }
  102. void Dialog::on_SavePicButton_clicked()
  103. {
  104. //以当前时间作为文件名
  105. // QDateTime datetime= QDateTime::currentDateTime();//获取系统当前的时间
  106. // QString strtime = datetime.toString("yyyyMMddhhmmss");//格式化时间
  107. // QString qstrdir = ui->SavePicDirLineEdit->text();
  108. // qstrdir = QDir::fromNativeSeparators(qstrdir);
  109. // while(qstrdir.endsWith('/')) //此处检测 /
  110. // {
  111. // qstrdir.chop(1); //删除最后一字节
  112. // }
  113. // QString qstrfilename = qstrdir + "/" + strtime + ".jpg";
  114. // phkcamera->savepic("D:/goafhikimg/20230613173952.jpg");
  115. // phkcamera->PostRequestWithFile("D:/goafhikimg/test.jpg", "192.168.3.250", 4);
  116. // QFile file("D:/sxkyprjcfg/sxkyprj.cfg");//.cfg为文本文件
  117. // QByteArray jsonData;
  118. // if(file.open(QIODevice::ReadOnly)){
  119. // jsonData = file.readAll();
  120. // file.close();
  121. // }
  122. // QJsonDocument jsonDocu = QJsonDocument::fromJson(jsonData);
  123. // if(jsonDocu.isObject()){
  124. // QJsonObject obj_root = jsonDocu.object();
  125. // QStringList keys = obj_root.keys();
  126. // for(auto key : keys){
  127. // }
  128. // }
  129. // if(nullptr == pmqttgoaf){
  130. // pmqttgoaf = new mqttgoaf();
  131. // pmqttgoaf->mqttconnect("113.141.93.143", 8893);
  132. // }
  133. // else{
  134. // pmqttgoaf->mqttpublish("topic/camera/alarm", "hello goafmqtt");
  135. // }
  136. //
  137. }
  138. void Dialog::on_CloseButton_clicked()
  139. {
  140. phkcamera->stopdisplaypic();
  141. // if(nullptr != phkcamera)
  142. // {
  143. // phkcamera->logout();
  144. // delete phkcamera;
  145. // phkcamera = nullptr;
  146. // }
  147. ui->OpenButton->setEnabled(true);
  148. ui->CloseButton->setEnabled(false);
  149. }