From 14f5d740e7644252ff491152700de31039d5660e Mon Sep 17 00:00:00 2001 From: duanshengchao <519970194@qq.com> Date: Tue, 25 Nov 2025 15:58:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E8=87=AA=E5=AE=9A=E4=B9=89messageBox?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=8A=A8=E6=8D=A2=E8=A1=8C=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/messageDialog.cpp | 51 +++++++++++++++++++++++++++++++++++++++- ui/messageDialog.ui | 6 ++--- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/source/messageDialog.cpp b/source/messageDialog.cpp index a6a9b29..b3a996f 100644 --- a/source/messageDialog.cpp +++ b/source/messageDialog.cpp @@ -1,5 +1,42 @@ #include "messageDialog.h" #include "ui_messageDialog.h" +#include + +int getStringLength(const QString& str) +{ + int chineseCharCount = 0; + int englishCharCount = 0; + for(QChar ch : str) + { + ushort unicode = ch.unicode(); + if(unicode >= 0x4E00 && unicode <= 0x9FFF) + chineseCharCount++; + else if (ch.isLetter()) + englishCharCount++; + } + + return chineseCharCount + englishCharCount * 0.6; //一个英文字符大概是0.6个中文字符长度 +} +int getIndexForLength(const QString& str, int maxLength) +{ + float length = 0.0; + int index = 0; + for(QChar ch : str) + { + ushort unicode = ch.unicode(); + if(unicode >= 0x4E00 && unicode <= 0x9FFF) + length = length + 1.0; + else if (ch.isLetter()) + length = length + 0.6; + + if(qCeil(length) >= maxLength) + break; + else + index++; + } + + return index; +} MessageDialog::MessageDialog(QWidget *parent) : QDialog(parent) @@ -45,8 +82,20 @@ void MessageDialog::setType(MessageDialogType type) void MessageDialog::setMessage(MessageDialogType type,const QString& strTitle,const QString& strContent) { setType(type); + int maxLength = 25; ui->labelTitle->setText(strTitle); - ui->labelContent->setText(strContent); + qInfo() << getStringLength(strContent); + if(getStringLength(strContent) > maxLength) + { + //QString& strText = const_cast(strContent); + QString strText(strContent); + int index = getIndexForLength(strContent, maxLength); + qInfo() << index; + strText.insert(index, "\n"); //实现简单自动换行 + ui->labelContent->setText(strText); + } + else + ui->labelContent->setText(strContent); } void MessageDialog::onBtnClicked_confirm() diff --git a/ui/messageDialog.ui b/ui/messageDialog.ui index 92fa819..1c2feb5 100644 --- a/ui/messageDialog.ui +++ b/ui/messageDialog.ui @@ -121,9 +121,9 @@ border-right:0px; 100 - 70 - 431 - 51 + 60 + 391 + 81