#include "msgwidget.h" #include "ui_msgwidget.h" #include #include "pdu_ctrl.h" MSGWidget::MSGWidget(QWidget *parent) : QDialog(parent), ui(new Ui::MSGWidget) { ui->setupUi(this); setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint); setAttribute(Qt::WA_TranslucentBackground); ui->label_Data->setWordWrap(true); QString strBKG=""; #ifdef RS485Type//() strBKG="QWidget{background-color: rgb(0, 225, 225);color: rgb(0, 0, 0);}"; #else strBKG="QWidget{background-color: rgb(30, 30, 255);color: rgb(255, 255, 255);}"; #endif this->setStyleSheet(strBKG); } MSGWidget::~MSGWidget() { delete ui; } void MSGWidget::SetText(QString 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; // 强制处理所有待处理的事件,确保尺寸更新 QApplication::processEvents(); ui->label_Data->setFixedSize(nW-10,nHightData); ui->label_Warning->setFixedSize(nW-10,nHight); //this->setFixedSize(nW-5,nSum+10); // 强制更新布局 this->layout()->activate(); this->updateGeometry(); update(); // 可选:强制重绘整个窗口 qDebug()<<"MSGWidget label_Size"<label_Data->size().width()<<"X"<label_Data->size().height(); qDebug()<<"MSGWidget label2_Size"<label_Warning->size().width()<<"X"<label_Warning->size().height(); qDebug()<<"MSGWidget heightForWidth"<size().width()<<"X"<size().width(); /*QTimer::singleShot(50, this, [this]() { this->update(); ui->label_Data->update(); ui->label_Warning->update(); });*/ }