feat:自定义messageBox添加自动换行功能
This commit is contained in:
parent
ff521471c4
commit
14f5d740e7
|
|
@ -1,5 +1,42 @@
|
|||
#include "messageDialog.h"
|
||||
#include "ui_messageDialog.h"
|
||||
#include <QtMath>
|
||||
|
||||
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<QString&>(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()
|
||||
|
|
|
|||
|
|
@ -121,9 +121,9 @@ border-right:0px;</string>
|
|||
<property name="geometry">
|
||||
<rect>
|
||||
<x>100</x>
|
||||
<y>70</y>
|
||||
<width>431</width>
|
||||
<height>51</height>
|
||||
<y>60</y>
|
||||
<width>391</width>
|
||||
<height>81</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="styleSheet">
|
||||
|
|
|
|||
Loading…
Reference in New Issue