Skip to content

Commit ad293ca

Browse files
committed
新增字数统计功能,添加字数统计标签并在文本变化时更新字数。更新UI布局以包含字数标签。
1 parent 33962b9 commit ad293ca

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

noteeditwidget.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <QVBoxLayout>
3232
#include <QSpacerItem>
3333
#include <QLabel>
34+
#include <QRegularExpression>
3435

3536
NoteEditWidget::NoteEditWidget(QWidget *parent) :
3637
QWidget(parent),
@@ -213,11 +214,18 @@ void NoteEditWidget::setupUI()
213214

214215
// 更新置顶按钮状态
215216
updateStayOnTopButton();
217+
218+
// 获取字数统计标签指针
219+
m_wordCountLabel = findChild<QLabel*>("wordCountLabel");
220+
if (m_wordCountLabel) {
221+
m_wordCountLabel->setText("字数:0");
222+
}
216223
}
217224

218225
void NoteEditWidget::setupConnections()
219226
{
220227
connect(ui->contentTextEdit, &QTextEdit::textChanged, this, &NoteEditWidget::onContentChanged);
228+
connect(ui->contentTextEdit, &QTextEdit::textChanged, this, &NoteEditWidget::updateWordCount);
221229
connect(ui->titleLineEdit, &QLineEdit::textChanged, this, &NoteEditWidget::onTitleChanged);
222230
connect(ui->boldButton, &QPushButton::clicked, this, &NoteEditWidget::onBoldButtonClicked);
223231
connect(ui->italicButton, &QPushButton::clicked, this, &NoteEditWidget::onItalicButtonClicked);
@@ -354,6 +362,9 @@ void NoteEditWidget::setNote(const Note &note)
354362
}
355363

356364
activateWindow(); // 确保窗口获得焦点
365+
366+
// 更新字数统计
367+
updateWordCount();
357368
}
358369

359370
void NoteEditWidget::createNewNote()
@@ -401,6 +412,9 @@ void NoteEditWidget::createNewNote()
401412
}
402413

403414
activateWindow(); // 确保窗口获得焦点
415+
416+
// 更新字数统计
417+
updateWordCount();
404418
}
405419

406420
Note NoteEditWidget::getCurrentNote() const
@@ -1501,4 +1515,34 @@ bool ImageEventFilter::eventFilter(QObject *watched, QEvent *event)
15011515
}
15021516

15031517
return QObject::eventFilter(watched, event);
1518+
}
1519+
1520+
void NoteEditWidget::updateWordCount()
1521+
{
1522+
if (!m_wordCountLabel) return;
1523+
// 获取正文纯文本内容
1524+
QString text = ui->contentTextEdit->toPlainText();
1525+
int count = 0;
1526+
// 统计汉字数量
1527+
QRegularExpression hanziRe("[\u4e00-\u9fa5]");
1528+
QRegularExpressionMatchIterator it = hanziRe.globalMatch(text);
1529+
while (it.hasNext()) {
1530+
it.next();
1531+
++count;
1532+
}
1533+
// 统计英文单词数量
1534+
QRegularExpression wordRe("[A-Za-z]+(?:'[A-Za-z]+)?");
1535+
it = wordRe.globalMatch(text);
1536+
while (it.hasNext()) {
1537+
it.next();
1538+
++count;
1539+
}
1540+
// 统计数字串数量(每个连续数字串算一个字)
1541+
QRegularExpression numRe("\\d+");
1542+
it = numRe.globalMatch(text);
1543+
while (it.hasNext()) {
1544+
it.next();
1545+
++count;
1546+
}
1547+
m_wordCountLabel->setText(QString("字数:%1").arg(count));
15041548
}

noteeditwidget.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ private slots:
113113
void onStayOnTopClicked();
114114
void onPaste();
115115
void showImageViewer(const QImage &image); // 显示图片查看器
116+
void updateWordCount(); // 新增:更新字数统计
116117

117118
private:
118119
Ui::NoteEditWidget *ui;
@@ -128,6 +129,8 @@ private slots:
128129
QMap<QString, QString> m_tempImages; // 临时图片映射(资源名->文件路径)
129130
QMap<QString, QImage> m_originalImages; // 保存原始图片,用于显示原图
130131
ImageEventFilter *m_imageEventFilter; // 图片事件过滤器
132+
// 新增:字数统计标签指针
133+
QLabel* m_wordCountLabel;
131134

132135
void updateFormattingButtons();
133136
void setupConnections();

noteeditwidget.ui

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,22 @@
177177
</property>
178178
</widget>
179179
</item>
180+
<item>
181+
<widget class="QLabel" name="wordCountLabel">
182+
<property name="minimumWidth">
183+
<number>80</number>
184+
</property>
185+
<property name="alignment">
186+
<set>Qt::AlignRight|Qt::AlignVCenter</set>
187+
</property>
188+
<property name="text">
189+
<string>字数:0</string>
190+
</property>
191+
<property name="styleSheet">
192+
<string notr="true">color: #888888; font-size: 10pt;</string>
193+
</property>
194+
</widget>
195+
</item>
180196
</layout>
181197
</item>
182198
</layout>

0 commit comments

Comments
 (0)