Skip to content

Commit 55370b3

Browse files
committed
Fix: don't create a flashing selection square QTimer without -P
Without `-P`, `QEmojiGridWidget::_selectedItemFlashTimer` isn't initialized, therefore timing out as fast as possible when it starts (unconditionally in QEmojiGridWidget::_selectEmojiGraphicsItem()), leading to an unfortunate 100% CPU usage. Only allocate a `QTimer` when needed. Signed-off-by: Philippe Proulx <[email protected]>
1 parent 4f6622c commit 55370b3

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

jome/q-emoji-grid-widget.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ QEmojiGridWidget::QEmojiGridWidget(QWidget * const parent, const EmojiDb& emojiD
3737
this->_setGraphicsSceneStyle(_findEmojisGraphicsScene);
3838

3939
if (selectedEmojiFlashPeriod) {
40-
QObject::connect(&_selectedItemFlashTimer, &QTimer::timeout,
40+
_selectedItemFlashTimer = new QTimer {this};
41+
QObject::connect(&*_selectedItemFlashTimer, &QTimer::timeout,
4142
this, &QEmojiGridWidget::_selectedItemFlashTimerTimeout);
42-
_selectedItemFlashTimer.setInterval(*selectedEmojiFlashPeriod / 2);
43-
_selectedItemFlashTimer.start();
43+
_selectedItemFlashTimer->setInterval(*selectedEmojiFlashPeriod / 2);
44+
_selectedItemFlashTimer->start();
4445
}
4546

4647
this->setAlignment(Qt::AlignLeft | Qt::AlignTop);
@@ -237,7 +238,10 @@ void QEmojiGridWidget::_selectEmojiGraphicsItem(const boost::optional<unsigned i
237238
_selectedEmojiGraphicsItemIndex = index;
238239

239240
if (!index) {
240-
_selectedItemFlashTimer.stop();
241+
if (_selectedItemFlashTimer) {
242+
_selectedItemFlashTimer->stop();
243+
}
244+
241245
selectedItem->hide();
242246
emit this->selectionChanged(nullptr);
243247
return;
@@ -248,7 +252,11 @@ void QEmojiGridWidget::_selectEmojiGraphicsItem(const boost::optional<unsigned i
248252
const auto& emojiGraphicsItem = *_curEmojiGraphicsItems[*index];
249253

250254
selectedItem->show();
251-
_selectedItemFlashTimer.start();
255+
256+
if (_selectedItemFlashTimer) {
257+
_selectedItemFlashTimer->start();
258+
}
259+
252260
selectedItem->setPos(emojiGraphicsItem.pos().x() - 4.,
253261
emojiGraphicsItem.pos().y() - 4.);
254262

jome/q-emoji-grid-widget.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private slots:
185185
QGraphicsPixmapItem *_findEmojisGraphicsSceneSelectedItem = nullptr;
186186

187187
// timer to make the selection square flash if requested
188-
QTimer _selectedItemFlashTimer;
188+
QTimer *_selectedItemFlashTimer = nullptr;
189189

190190
// true to use a dark background
191191
bool _darkBg;

0 commit comments

Comments
 (0)