feat:自定义messageBox添加自动换行功能
This commit is contained in:
parent
ff521471c4
commit
14f5d740e7
|
|
@ -1,5 +1,42 @@
|
||||||
#include "messageDialog.h"
|
#include "messageDialog.h"
|
||||||
#include "ui_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)
|
MessageDialog::MessageDialog(QWidget *parent)
|
||||||
: QDialog(parent)
|
: QDialog(parent)
|
||||||
|
|
@ -45,7 +82,19 @@ void MessageDialog::setType(MessageDialogType type)
|
||||||
void MessageDialog::setMessage(MessageDialogType type,const QString& strTitle,const QString& strContent)
|
void MessageDialog::setMessage(MessageDialogType type,const QString& strTitle,const QString& strContent)
|
||||||
{
|
{
|
||||||
setType(type);
|
setType(type);
|
||||||
|
int maxLength = 25;
|
||||||
ui->labelTitle->setText(strTitle);
|
ui->labelTitle->setText(strTitle);
|
||||||
|
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);
|
ui->labelContent->setText(strContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -121,9 +121,9 @@ border-right:0px;</string>
|
||||||
<property name="geometry">
|
<property name="geometry">
|
||||||
<rect>
|
<rect>
|
||||||
<x>100</x>
|
<x>100</x>
|
||||||
<y>70</y>
|
<y>60</y>
|
||||||
<width>431</width>
|
<width>391</width>
|
||||||
<height>51</height>
|
<height>81</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
<property name="styleSheet">
|
<property name="styleSheet">
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue