Kaynağa Gözat

上传qt弹出报警的测试整改完成版本

chenyuanjie 1 yıl önce
ebeveyn
işleme
9067e2fda9

+ 11 - 12
Qt/Qt_Test/dataupdatethread.cpp

@@ -331,6 +331,7 @@ int DataUpdateThread::isT_ACType()
 
 void DataUpdateThread::ReadMSG()
 {
+    //return;
     if(nReadType!=1&&!m_shmData)
     {
         return;
@@ -344,19 +345,19 @@ void DataUpdateThread::ReadMSG()
             {
                 bool isHaveMSG=false;
                 //未检测到更新从当前位置循环遍历一遍
-                qDebug()<<"MSG_Find"<<nIndexMSG<<"=Null";
+                //qDebug()<<"MSG_Find"<<nIndexMSG<<"=Null";
                 for(int nIndex=0;nIndex<10;nIndex++)//监测其他的点是否有被更新依次刷新过去
                 {
                     nIndexMSG=NextIndex();
                     if(m_shmData->nTypeCtrlType[nIndexMSG]==MSGCtrl_Write)//检测到新的点。
                     {
-                        qDebug()<<nIndexMSG<<"=MSGCtrl_Write";
+                        //qDebug()<<nIndexMSG<<"=MSGCtrl_Write";
                         isHaveMSG=true;
                         QString strShow=QString::fromUtf8(m_shmData->strMSG[nIndexMSG]);
                         QString strShow8bit=QString::fromLocal8Bit(m_shmData->strMSG[nIndexMSG]);
-                        qDebug()<<"MSG_Update char*MSG="<<m_shmData->strMSG[nIndexMSG];
-                        qDebug()<<"MSG_Update fromUtf8="<<strShow;
-                        qDebug()<<"MSG_Update fromLocal8bit="<<strShow8bit;
+                        //qDebug()<<"MSG_Update char*MSG="<<m_shmData->strMSG[nIndexMSG];
+                        //qDebug()<<"MSG_Update fromUtf8="<<strShow;
+                        //qDebug()<<"MSG_Update fromLocal8bit="<<strShow8bit;
                         ShowMSGWidget(strShow);
                         break;
                     }else
@@ -378,16 +379,16 @@ void DataUpdateThread::ReadMSG()
                 ShowMSGWidget(strShow);*/
                 QString strShow=QString::fromUtf8(m_shmData->strMSG[nIndexMSG]);
                 QString strShow8bit=QString::fromLocal8Bit(m_shmData->strMSG[nIndexMSG]);
-                qDebug()<<"MSG_Update char*MSG="<<m_shmData->strMSG[nIndexMSG];
-                qDebug()<<"MSG_Update fromUtf8="<<strShow;
-                qDebug()<<"MSG_Update fromLocal8bit="<<strShow8bit;
+                //qDebug()<<"MSG_Update char*MSG="<<m_shmData->strMSG[nIndexMSG];
+                //qDebug()<<"MSG_Update fromUtf8="<<strShow;
+                //qDebug()<<"MSG_Update fromLocal8bit="<<strShow8bit;
                 ShowMSGWidget(strShow);
             }
             break;
             default:
                 break;
         }
-        qDebug()<<"MSG_Switch_end nIndexMSG ="<<nIndexMSG;
+        //qDebug()<<"MSG_Switch_end nIndexMSG ="<<nIndexMSG;
     }
 }
 
@@ -412,7 +413,7 @@ void DataUpdateThread::ShowMSGWidget(QString strShowData)
             strNowMSG=strShowData;
             emit signal_ShowMSGWidget(strShowData);
             //pwidget->ShowMSGWidget(strShowData);
-            qDebug()<<"emit ShowMSGWidget_Data:"<<strNowMSG;
+            //qDebug()<<"emit ShowMSGWidget_Data:"<<strNowMSG;
         }
     }
 }
@@ -427,8 +428,6 @@ void DataUpdateThread::slot_ClickMSG()
     if(0<=nIndexMSG&&nIndexMSG<10)
     {
         m_shmData->nTypeCtrlType[nIndexMSG]=MSGCtrl_Click;
-        qDebug()<<"slot_ClickMSGIndex=:"<<nIndexMSG;
-        qDebug()<<"slot_ClickMSGIndexData=:"<<m_shmData->nTypeCtrlType[nIndexMSG];
     }
     nIndexMSG=NextIndex();
 

+ 1 - 1
Qt/Qt_Test/main.cpp

@@ -57,7 +57,7 @@ int main(int argc, char *argv[])
     thread_Button.InitConnect();
     thread_Button.start();
     //thread_Button.InitConnect();
-
+    //w.ShowMSGWidget("Temperature and humidity sensor 湿度 超过上限");
     int ret=a.exec();
     //析构
     thread_Update.isRunStats=false;

+ 21 - 3
Qt/Qt_Test/msgwidget.cpp

@@ -1,6 +1,6 @@
 #include "msgwidget.h"
 #include "ui_msgwidget.h"
-
+#include <QDebug>
 MSGWidget::MSGWidget(QWidget *parent) :
     QDialog(parent),
     ui(new Ui::MSGWidget)
@@ -8,7 +8,7 @@ MSGWidget::MSGWidget(QWidget *parent) :
     ui->setupUi(this);
     setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
     setAttribute(Qt::WA_TranslucentBackground);
-    ui->label->setWordWrap(true);
+    ui->label_Data->setWordWrap(true);
 }
 
 MSGWidget::~MSGWidget()
@@ -18,5 +18,23 @@ MSGWidget::~MSGWidget()
 
 void MSGWidget::SetText(QString strShow)
 {
-    ui->label->setText(strShow);
+    ui->label_Data->setText(strShow);
+}
+
+void MSGWidget::setFW(int nW)
+{
+    int nHightData=ui->label_Data->heightForWidth(nW-10);
+    int nHight=ui->label_Warning->heightForWidth(nW-10);
+    int nSum=nHightData+nHight+2;
+
+    //ui->label_Data->adjustSize();
+    //ui->label_Data->updateGeometry();
+    ui->label_Data->setFixedSize(nW-10,nHightData);
+    ui->label_Warning->setFixedSize(nW-10,nHight);
+    //this->setFixedSize(nW,nW);
+    update(); // 可选:强制重绘整个窗口
+    qDebug()<<"MSGWidget label_Size"<<ui->label_Data->size().width()<<"X"<<ui->label_Data->size().height();
+    qDebug()<<"MSGWidget label2_Size"<<ui->label_Warning->size().width()<<"X"<<ui->label_Warning->size().height();
+    qDebug()<<"MSGWidget heightForWidth"<<nHightData;
+    qDebug()<<"MSGWidget setFixedSize"<<nW<<"X"<<nSum;
 }

+ 2 - 0
Qt/Qt_Test/msgwidget.h

@@ -15,6 +15,8 @@ public:
     explicit MSGWidget(QWidget *parent = 0);
     ~MSGWidget();
     void SetText(QString strShow);
+    //根据设置的宽度更新设置的高度
+    void setFW(int nW);
 private:
     Ui::MSGWidget *ui;
 };

+ 44 - 3
Qt/Qt_Test/msgwidget.ui

@@ -10,6 +10,12 @@
     <height>71</height>
    </rect>
   </property>
+  <property name="maximumSize">
+   <size>
+    <width>16777215</width>
+    <height>16777215</height>
+   </size>
+  </property>
   <property name="windowTitle">
    <string/>
   </property>
@@ -40,7 +46,20 @@ QLabel
     <number>2</number>
    </property>
    <item>
-    <widget class="QLabel" name="label_2">
+    <spacer name="verticalSpacer">
+     <property name="orientation">
+      <enum>Qt::Vertical</enum>
+     </property>
+     <property name="sizeHint" stdset="0">
+      <size>
+       <width>20</width>
+       <height>40</height>
+      </size>
+     </property>
+    </spacer>
+   </item>
+   <item>
+    <widget class="QLabel" name="label_Warning">
      <property name="font">
       <font>
        <pointsize>12</pointsize>
@@ -58,20 +77,42 @@ QLabel
     </widget>
    </item>
    <item>
-    <widget class="QLabel" name="label">
+    <widget class="QLabel" name="label_Data">
+     <property name="sizePolicy">
+      <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
+       <horstretch>0</horstretch>
+       <verstretch>0</verstretch>
+      </sizepolicy>
+     </property>
      <property name="font">
       <font>
        <pointsize>10</pointsize>
       </font>
      </property>
      <property name="text">
-      <string>xxxxxxxxxxxxxxxxxxxxxxx</string>
+      <string>xxxxx</string>
+     </property>
+     <property name="alignment">
+      <set>Qt::AlignCenter</set>
      </property>
      <property name="wordWrap">
       <bool>true</bool>
      </property>
     </widget>
    </item>
+   <item>
+    <spacer name="verticalSpacer_2">
+     <property name="orientation">
+      <enum>Qt::Vertical</enum>
+     </property>
+     <property name="sizeHint" stdset="0">
+      <size>
+       <width>20</width>
+       <height>40</height>
+      </size>
+     </property>
+    </spacer>
+   </item>
   </layout>
  </widget>
  <resources/>

+ 1 - 1
Qt/Qt_Test/pdu_ctrl.cpp

@@ -97,7 +97,7 @@ void pdu_Ctrl::slot_setAngel(int nAngle)
             }
             qDebug()<<"x:"<<m_pWidget->geometry().x()<<"y:"<<m_pWidget->geometry().y()<<"Width:"<<m_pWidget->size().width()<<"Height:"<<m_pWidget->size().height();
         }
-
+        m_pWidget->m_nAngle=nAngle;
 #endif
         m_pView->update();
     }

+ 113 - 4
Qt/Qt_Test/widget.cpp

@@ -6,6 +6,7 @@
 #include "buttonclickthread.h"
 #include <QPainter>
 #include "pdu_ctrl.h"
+#include <QResizeEvent>
 
 Widget::Widget(QWidget *parent) :
     QWidget(parent),
@@ -46,6 +47,8 @@ Widget::Widget(QWidget *parent) :
     //ui->stackedWidget_Main->setCurrentWidget(ui->page_7);
     m_pMSGWidget=new MSGWidget(this);
     m_pMSGWidget->hide();
+
+
 }
 
 Widget::~Widget()
@@ -443,10 +446,11 @@ void Widget::ShowMSGWidget(QString strShowData)
     if(m_pMSGWidget&&m_pMSGWidget->isHidden())
     {
         m_pMSGWidget->SetText(strShowData);
-        QRect parentRect = this->geometry();
-        m_pMSGWidget->setFixedWidth(parentRect.width()-4);
-        m_pMSGWidget->move(parentRect.center() - m_pMSGWidget->rect().center());
-        m_pMSGWidget->show();
+        adjustMSGWidgetPosition(false);
+        if (m_pMSGWidget->isHidden()) {
+            m_pMSGWidget->show();
+        }
+
         qDebug()<<"MSGWidget_Show geometry("<<m_pMSGWidget->geometry().x()<<","<<m_pMSGWidget->geometry().y()<<","<<m_pMSGWidget->geometry().width()<<","<<m_pMSGWidget->geometry().height()<<")";
     }
 }
@@ -459,3 +463,108 @@ void Widget::ClickMSGBtn()
         emit signal_ClickMSG();
     }
 }
+
+void Widget::adjustMSGWidgetPosition(bool isAuto)
+{
+    if(isAuto)
+    {
+        if(!(m_pMSGWidget&&!m_pMSGWidget->isHidden()))
+        {
+            return;
+        }
+    }
+        // 获取主窗口的逻辑尺寸
+        QRect mainWindowSize = this->geometry();
+        int mainWidth = mainWindowSize.width();
+        int mainHeight = mainWindowSize.height();
+        qDebug()<<"Widget geometry("<<mainWindowSize.x()<<","<<mainWindowSize.y()<<","<<mainWindowSize.width()<<","<<mainWindowSize.height()<<")";
+        // 根据旋转角度确定实际显示尺寸
+        int displayWidth, displayHeight;
+        //if(m_nAngle == 90 || m_nAngle == 270) {
+            displayWidth = mainWidth;
+            displayHeight = mainHeight;
+        //} else {
+            // 90或270度旋转时,宽高交换
+        //   displayWidth = mainHeight;
+        //    displayHeight = mainWidth;
+        //}
+        // 设置弹窗宽度(略小于显示宽度)
+        m_pMSGWidget->setFW(displayWidth - 4);
+
+        // 根据旋转角度计算弹窗位置
+        QPoint popupPos;
+        int offset=5;
+        if(m_nAngle==0||m_nAngle==180)
+        {
+            if(m_nAngle==0)
+            {
+                popupPos = QPoint(offset,(displayHeight - m_pMSGWidget->height()) / 2);
+            }
+            else
+            {
+                popupPos = QPoint(offset,-(displayHeight - m_pMSGWidget->height()) / 2);
+            }
+        }else
+        {
+            if(m_nAngle==90)
+            {
+                popupPos = QPoint(-(displayHeight - m_pMSGWidget->height()) / 2,offset);
+            }else
+            {
+                //popupPos = QPoint(2,-(displayHeight - m_pMSGWidget->height()) / 2);
+                popupPos = QPoint((displayHeight - m_pMSGWidget->height()) / 2,offset);
+            }
+        }
+        popupPos += QPoint(mainWindowSize.x(), mainWindowSize.y());
+        /*
+        switch(m_nAngle) {
+        case 0: // 0度旋转
+            popupPos = QPoint(
+                2,
+                (displayHeight - m_pMSGWidget->height()) / 2
+            );
+            break;
+
+        case 90: // 90度旋转
+            popupPos = QPoint(
+                2,
+                (displayHeight - m_pMSGWidget->height()) / 2
+            );
+            // 调整坐标系偏移
+            popupPos += QPoint(mainWidth, 0);
+            break;
+        case 180: // 180度旋转
+            popupPos = QPoint(
+                2,
+                (displayHeight - m_pMSGWidget->height()) / 2
+            );
+            // 调整坐标系偏移
+            popupPos += QPoint(mainWidth, mainHeight);
+            break;
+
+        case 270: // 270度旋转
+            popupPos = QPoint(
+                2,
+                (displayHeight - m_pMSGWidget->height()) / 2
+            );
+            // 调整坐标系偏移
+            popupPos += QPoint(0, mainHeight);
+            break;
+        }
+        */
+
+        // 应用位置
+        m_pMSGWidget->move(popupPos);
+        // 调试输出
+        qDebug() << "Rotation:" << m_nAngle;
+        qDebug() << "MSGWidget position:" << popupPos;
+        qDebug() << "MSGWidget geometry:" << m_pMSGWidget->geometry();
+}
+
+void Widget::resizeEvent(QResizeEvent *event)
+{
+    if(m_pMSGWidget&&!m_pMSGWidget->isHidden())
+    {
+        adjustMSGWidgetPosition();
+    }
+}

+ 7 - 1
Qt/Qt_Test/widget.h

@@ -87,6 +87,10 @@ public:
     }
     //点击任意按钮确认
     void ClickMSGBtn();
+    //
+    void adjustMSGWidgetPosition(bool isAuto=true);
+protected:
+    void resizeEvent(QResizeEvent *event) override;
 signals:
     void signal_ClickMSG();//报警确认
 
@@ -104,7 +108,7 @@ private:
     //当前显示页面ID
     int nChidIndex;
     int nChidPHIndex;
-    int m_nAngle;
+
     QGraphicsProxyWidget* m_pAngleCtrl;
     QGraphicsView* m_pView;
     int m_nWidth;
@@ -113,6 +117,8 @@ private:
 
     MSGWidget* m_pMSGWidget;//提示界面
     int nIndexShow;
+public:
+    int m_nAngle;
 };
 
 #endif // WIDGET_H

BIN
cust_data/qt/IP/Qt_Test


BIN
cust_data/qt/Qt_Test