msgwidget.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "msgwidget.h"
  2. #include "ui_msgwidget.h"
  3. #include <QDebug>
  4. #include "pdu_ctrl.h"
  5. MSGWidget::MSGWidget(QWidget *parent) :
  6. QDialog(parent),
  7. ui(new Ui::MSGWidget)
  8. {
  9. ui->setupUi(this);
  10. setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
  11. setAttribute(Qt::WA_TranslucentBackground);
  12. ui->label_Data->setWordWrap(true);
  13. QString strBKG="";
  14. #ifdef RS485Type//()
  15. strBKG="QWidget{background-color: rgb(0, 225, 225);color: rgb(0, 0, 0);}";
  16. #else
  17. strBKG="QWidget{background-color: rgb(30, 30, 255);color: rgb(255, 255, 255);}";
  18. #endif
  19. this->setStyleSheet(strBKG);
  20. }
  21. MSGWidget::~MSGWidget()
  22. {
  23. delete ui;
  24. }
  25. void MSGWidget::SetText(QString strShow)
  26. {
  27. ui->label_Data->setText(strShow);
  28. }
  29. void MSGWidget::setFW(int nW)
  30. {
  31. int nHightData=ui->label_Data->heightForWidth(nW-10);
  32. int nHight=ui->label_Warning->heightForWidth(nW-10);
  33. int nSum=nHightData+nHight+2;
  34. // 强制处理所有待处理的事件,确保尺寸更新
  35. QApplication::processEvents();
  36. ui->label_Data->setFixedSize(nW-10,nHightData);
  37. ui->label_Warning->setFixedSize(nW-10,nHight);
  38. //this->setFixedSize(nW-5,nSum+10);
  39. // 强制更新布局
  40. this->layout()->activate();
  41. this->updateGeometry();
  42. update(); // 可选:强制重绘整个窗口
  43. qDebug()<<"MSGWidget label_Size"<<ui->label_Data->size().width()<<"X"<<ui->label_Data->size().height();
  44. qDebug()<<"MSGWidget label2_Size"<<ui->label_Warning->size().width()<<"X"<<ui->label_Warning->size().height();
  45. qDebug()<<"MSGWidget heightForWidth"<<nHightData;
  46. qDebug()<<"MSGWidget setFixedSize"<<nW-5<<"X"<<nSum+10;
  47. //qDebug()<<"MSGWidget FinalFixedSize"<<this->size().width()<<"X"<<this->size().width();
  48. /*QTimer::singleShot(50, this, [this]() {
  49. this->update();
  50. ui->label_Data->update();
  51. ui->label_Warning->update();
  52. });*/
  53. }